shaped up routing: enabled explicit mode, and created some named routes
authorNick Retallack <nickretallack@gmil.com>
Tue, 6 Oct 2009 08:08:06 +0000 (01:08 -0700)
committerNick Retallack <nickretallack@gmil.com>
Tue, 6 Oct 2009 08:08:06 +0000 (01:08 -0700)
floof/config/routing.py
floof/controllers/art.py
floof/lib/base.py
floof/templates/base.mako

index 969376a..6ec64e4 100644 (file)
@@ -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'],
 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
     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']})
 
 
     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')
     # 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('/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')
 
 
     map.connect('/search/list', controller='search', action='list')
 
 
index 6c5e454..f08cd60 100644 (file)
@@ -11,6 +11,11 @@ import elixir
 from floof.model.art import Art
 
 class ArtController(BaseController):
 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()
 
     # 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")
 
         """ New Art! """
         return render("/art/new.mako")
 
-
+    # TODO: login required
     def upload(self):
     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):
         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):
         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()
         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):
     
     # 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()
         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)
index cb7c746..d05c24d 100644 (file)
@@ -13,7 +13,7 @@ from floof.model.users import User
 class BaseController(WSGIController):
 
     # NOTE: This could have been implemented as a middleware =]
 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'])
         # Fetch current user object
         try:
             c.user = User.query.get(session['user_id'])
index f15b0c6..654b8b3 100644 (file)
@@ -16,7 +16,7 @@
 ## | <a href="${h.url_for("/users/"+c.user}">Your Page</a>
 % endif
 
 ## | <a href="${h.url_for("/users/"+c.user}">Your Page</a>
 % 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')} 
 
 ${h.text('query', c.query)} 
 ${h.submit('button', 'Search')}