From 1d97ea7270221fdb05d7fde35c343f9a763c18b1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 21 Jul 2010 11:56:32 -0400 Subject: [PATCH] Added tag display to the recipe page with tag-editing forms --- cookbook/cookbook.py | 4 +- cookbook/server.py | 71 +++++++++++++++-------------------- cookbook/template/recipe.html | 16 ++++++++ 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/cookbook/cookbook.py b/cookbook/cookbook.py index 07843fb..b3e3ed0 100644 --- a/cookbook/cookbook.py +++ b/cookbook/cookbook.py @@ -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): diff --git a/cookbook/server.py b/cookbook/server.py index 004c39a..49ddb9e 100644 --- a/cookbook/server.py +++ b/cookbook/server.py @@ -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 diff --git a/cookbook/template/recipe.html b/cookbook/template/recipe.html index 23cd564..e65a15e 100644 --- a/cookbook/template/recipe.html +++ b/cookbook/template/recipe.html @@ -22,6 +22,22 @@ Source: {{ recipe.source }}
{% endif %} + {% if recipe.tags %} + Tags: + {{ ', '.join(recipe.tags) }} +
+ {% endif %} +

+
+ + + +
+
+ + + +
{% for ingredient_block in recipe.ingredient_blocks %} -- 2.26.2