Added tag display to the recipe page with tag-editing forms
authorW. Trevor King <wking@drexel.edu>
Wed, 21 Jul 2010 15:56:32 +0000 (11:56 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 21 Jul 2010 15:56:32 +0000 (11:56 -0400)
cookbook/cookbook.py
cookbook/server.py
cookbook/template/recipe.html

index 07843fb7dd891acfc68e3058372dbb1a6b1340b3..b3e3ed0c045f7b100dc0103002604d7986768029 100644 (file)
@@ -509,8 +509,10 @@ class Cookbook (list):
     def load(self, dir='recipe'):
         for path in os.listdir(dir):
             r = Recipe()
-            with open(os.path.join(dir, path), 'r') as f:
+            p = os.path.join(dir, path)
+            with open(p, 'r') as f:
                 r.load(f)
+            r.path = p
             self.append(r)
 
     def make_index(self):
index 004c39a6e93fd33e1c57e7b8d2f046ba75a1c6e3..49ddb9ec08e91e048dcf8dfd5fe26160d137e7af 100644 (file)
@@ -61,46 +61,37 @@ class Server (object):
             recipe = self.cookbook.index[name]
         template = self.env.get_template('recipe.html')
         return template.render(cookbook=self.cookbook, recipe=recipe)
-#
-#    @cherrypy.expose
-#    def create(self, summary):
-#        """The view that handles the creation of a new bug."""
-#        if summary.strip() != '':
-#            bugdir = self.storage_callbacks.get_bugdir()
-#            bugdir.new_bug(summary=summary)
-#            self.commit(u"Created bug: %s" % summary)
-#        raise cherrypy.HTTPRedirect('/', status=302)
-#
-#    @cherrypy.expose
-#    def comment(self, id, body):
-#        """The view that handles adding a comment."""
-#        if body.strip() != '':
-#            bugdir = self.storage_callbacks.get_bugdir()
-#            bug = bugdir.bug_from_uuid(id)
-#            bug.comment_root.new_reply(body=body)
-#            self.commit(u"Added reply Re: %s" % bug.summary)
-#            raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
-#        raise cherrypy.HTTPRedirect('/', status=302)
-#
-#    @cherrypy.expose
-#    def edit(self, id, status=None, target=None, assignee=None, severity=None, summary=None):
-#        """The view that handles editing bug details."""
-#        bugdir = self.storage_callbacks.get_bugdir()
-#        bug = bugdir.bug_from_uuid(id)
-#
-#        if summary != None:
-#            old_summary = bug.summary
-#            bug.summary = summary
-#            self.bd.vcs.commit("Changed bug summary: %s -> %s" % (old_summary, bug.summary))
-#        else:
-#            bug.status = status if status != 'None' else None
-#            if target != 'None':
-#                libbe.target.add_target(bugdir, bug, target)
-#            bug.assigned = assignee if assignee != 'None' else None
-#            bug.severity = severity if severity != 'None' else None
-#            self.bd.vcs.commit("Changed bug values: %s" % bug.summary)
-#
-#        raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
+
+    @cherrypy.expose
+    def add_tag(self, name, tag):
+        """Add a tag to a single recipe."""
+        if type(name) == types.StringType:
+            name = unicode(name, encoding='utf-8')
+        recipe = self.cookbook.index[name]
+        if recipe.tags == None:
+            recipe.tags = []
+        if tag not in recipe.tags:
+            recipe.tags.append(tag)
+            with open(recipe.path, 'w') as f:
+                recipe.save(f)
+        raise cherrypy.HTTPRedirect(
+            'recipe?name=%s' % recipe.clean_name(), status=302)
+
+    @cherrypy.expose
+    def remove_tag(self, name, tag):
+        """Remove a tag from a single recipe."""
+        if type(name) == types.StringType:
+            name = unicode(name, encoding='utf-8')
+        recipe = self.cookbook.index[name]
+        if recipe.tags == None:
+            return
+        if tag in recipe.tags:
+            recipe.tags.remove(tag)
+            with open(recipe.path, 'w') as f:
+                recipe.save(f)
+        raise cherrypy.HTTPRedirect(
+            'recipe?name=%s' % recipe.clean_name(), status=302)
+
 
 def test():
     import doctest
index 23cd5643b197dd67d037248c9add834a661cc5fe..e65a15e5aff84d3d6c92a5630d334ef043391e25 100644 (file)
         <span class="detail-field-header">Source:</span>
         <span class="detail-field-contents">{{ recipe.source }}</span><br />
     {% endif %}
+    {% if recipe.tags %}
+        <span class="detail-field-header">Tags:</span>
+        <span class="detail-field-contents">{{ ', '.join(recipe.tags) }}
+       </span><br />
+    {% endif %}
+    </p>
+    <form action="add_tag" method="get">
+      <input type="hidden" name="name" value="{{ recipe.name }}"/>
+      <input type="text" name="tag" value=""/>
+      <input type="submit" value="Add tag" />
+    </form>
+    <form action="remove_tag" method="get">
+      <input type="hidden" name="name" value="{{ recipe.name }}"/>
+      <input type="text" name="tag" value=""/>
+      <input type="submit" value="Remove tag" />
+    </form>
 
     <div id="recipe-ingredient-blocks">
     {% for ingredient_block in recipe.ingredient_blocks %}