From 324ea2adc265de0c02ea56328f5f0a420dbaa999 Mon Sep 17 00:00:00 2001 From: Nick Retallack Date: Tue, 6 Oct 2009 01:08:06 -0700 Subject: [PATCH] shaped up routing: enabled explicit mode, and created some named routes --- floof/config/routing.py | 11 ++++++++--- floof/controllers/art.py | 18 +++++++++++------- floof/lib/base.py | 2 +- floof/templates/base.mako | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/floof/config/routing.py b/floof/config/routing.py index 969376a..6ec64e4 100644 --- a/floof/config/routing.py +++ b/floof/config/routing.py @@ -10,8 +10,13 @@ from routes import Mapper def make_map(): """Create, configure and return the routes Mapper""" map = Mapper(directory=config['pylons.paths']['controllers'], - always_scan=config['debug']) + always_scan=config['debug'], explicit=True) map.minimization = False + # explicit = True disables a broken feature called "route memory", + # where it adds everything matched in the current request as default variables + # for the next one. This is wrong because it doesn't invalidate things lower down in + # the hierarchy when higher up things change. Rails port failure. + # NOTE: this also disables actions defaulting to index, sorry. require_POST = dict(conditions={'method': ['POST']}) @@ -36,12 +41,12 @@ def make_map(): # Art stuff map.connect('/art/new', controller='art', action='new') map.connect('/art/upload', controller='art', action='upload') - map.connect('/art/{id}', controller='art', action='show') + map.connect('show_art', '/art/{id}', controller='art', action='show') map.connect('/art/{id}/tag', controller='art', action='tag') map.connect('/tag/{id}/delete', controller='tag', action='delete') - map.connect('/search', controller='search', action='index') + map.connect('search', '/search', controller='search', action='index') map.connect('/search/list', controller='search', action='list') diff --git a/floof/controllers/art.py b/floof/controllers/art.py index 6c5e454..f08cd60 100644 --- a/floof/controllers/art.py +++ b/floof/controllers/art.py @@ -11,6 +11,11 @@ import elixir from floof.model.art import Art class ArtController(BaseController): + def __before__(self, id=None): + super(ArtController, self).__before__() + # Awesome refactoring! + if id: + c.art = h.get_object_or_404(Art, id=id) # def index(): # c.artwork = Art.query.order_by(Art.id.desc()).all() @@ -20,29 +25,28 @@ class ArtController(BaseController): """ New Art! """ return render("/art/new.mako") - + # TODO: login required def upload(self): - print "PARAMS", request.params Art(uploaded_by=c.user, **request.params) elixir.session.commit() redirect_to(controller="main", action="index") def show(self, id): - c.art = h.get_object_or_404(Art, id=id) + # c.art = h.get_object_or_404(Art, id=id) if c.user: c.your_score = c.art.user_score(c.user) return render("/art/show.mako") # TODO: login required def tag(self, id): - c.art = h.get_object_or_404(Art, id=id) + # c.art = h.get_object_or_404(Art, id=id) c.art.add_tags(request.params["tags"], c.user) elixir.session.commit() - redirect_to(action="show", id=c.art.id) + redirect_to('show_art', id=c.art.id) # TODO: login required def rate(self, id): - c.art = h.get_object_or_404(Art, id=id) + # c.art = h.get_object_or_404(Art, id=id) c.art.rate(request.params["score"], c.user) elixir.session.commit() - redirect_to(action="show", id=c.art.id) + redirect_to('show_art', id=c.art.id) diff --git a/floof/lib/base.py b/floof/lib/base.py index cb7c746..d05c24d 100644 --- a/floof/lib/base.py +++ b/floof/lib/base.py @@ -13,7 +13,7 @@ from floof.model.users import User class BaseController(WSGIController): # NOTE: This could have been implemented as a middleware =] - def __before__(self, action, **params): + def __before__(self): # Fetch current user object try: c.user = User.query.get(session['user_id']) diff --git a/floof/templates/base.mako b/floof/templates/base.mako index f15b0c6..654b8b3 100644 --- a/floof/templates/base.mako +++ b/floof/templates/base.mako @@ -16,7 +16,7 @@ ## | Your Page % endif -${h.form(h.url_for(controller='search'), method='GET')} +${h.form(h.url_for('search'), method='GET')} ${h.text('query', c.query)} ${h.submit('button', 'Search')} -- 2.7.4