Reunite UTF-8 hack comment with sys.setdefaultencoding call it labels.
[cookbook.git] / cookbook / server.py
index 76dded630ff4d3dafd4c90b9fdc9c66fbf63d51a..e94e7693e95977d03441d8e6b424ceafd82eb799 100644 (file)
@@ -25,6 +25,7 @@ import os
 import random
 import re
 import types
+from urllib import urlencode
 import uuid
 from xml.sax import saxutils
 
@@ -82,8 +83,12 @@ class Server (object):
             recipe = random.choice(self.cookbook)
         else:
             recipe = self.cookbook.index[name]
+        tag_links = [
+            '<a href="./?%s">%s</a>' % (urlencode({'tag':t}), t)
+            for t in (recipe.tags or [])]
         template = self.env.get_template('recipe.html')
-        return template.render(cookbook=self.cookbook, recipe=recipe)
+        return template.render(cookbook=self.cookbook, recipe=recipe,
+                               tag_links=tag_links)
 
     @cherrypy.expose
     def add_tag(self, name, tag):
@@ -132,7 +137,13 @@ class Server (object):
     def edit(self, name=None, **kwargs):
         """Remove a tag from a single recipe."""
         name,recipe,action = self._normalize_edit_params(name, **kwargs)
-        if action.startswith('edit'):
+        if action == 'remove':
+            recipe = self.cookbook.index[name]
+            os.remove(recipe.path)
+            self.cookbook.remove(recipe)
+            self.cookbook.make_index()
+            raise cherrypy.HTTPRedirect(u'.', status=302)
+        elif action.startswith('edit'):
             self._update_recipe(name, recipe)
             if action == 'edit and redirect':  # done editing this recipe
                 raise cherrypy.HTTPRedirect(
@@ -154,6 +165,7 @@ class Server (object):
             print 'new'
             self.cookbook.append(recipe)
             self.cookbook.make_index()
+            self.cookbook.sort(key=lambda r: r.path)
         elif name != recipe.name:  # renamed recipe
             print 'rename'
             os.remove(recipe.path)
@@ -205,9 +217,9 @@ class Server (object):
                 m = regexp.match(key)
                 if m != None:
                     handler = getattr(self, '_action_%s' % k)
-                    action = handler(recipe, action, value, *m.groups(),
-                                     ingredient_block_map=ingredient_block_map,
-                                     ingredient_map=ingredient_map)
+                    kws = {'ingredient_block_map': ingredient_block_map,
+                           'ingredient_map': ingredient_map}
+                    action = handler(recipe, action, value, *m.groups(), **kws)
                     break
         # button updates
         action = kwargs.get('action', action)
@@ -219,9 +231,9 @@ class Server (object):
             m = regexp.match(action)
             if m != None:
                 handler = getattr(self, '_action_%s' % a)
-                action = handler(recipe, action, *m.groups(),
-                                 ingredient_block_map=ingredient_block_map,
-                                 ingredient_map=ingredient_map)
+                kws = {'ingredient_block_map': ingredient_block_map,
+                       'ingredient_map': ingredient_map}
+                action = handler(recipe, action, *m.groups(), **kws)
                 break
         return (name, recipe, action)