Mix ingredients until salamander starts to smoke
<BLANKLINE>
Serve warm over rice
+ >>> r.matches_tags(None)
+ True
+ >>> r.matches_tags([])
+ True
+ >>> r.matches_tags(['dinner', 'apprentice'])
+ True
+ >>> r.matches_tags(['dinner', 'dragon'])
+ False
>>> pprint.pprint(r.to_yaml())
{'author': 'Merlin',
'directions': 'Mix ingredients until salamander starts to smoke\\n\\nServe warm over rice',
name = name.replace(from_, to)
return name
+ def matches_tags(self, tags):
+ """Return True if this recipe is tagges with each of the tags in tags.
+ """
+ if tags in [None, []]:
+ return True
+ if self.tags == None:
+ return False
+ for t in tags:
+ if t not in self.tags:
+ return False
+ return True
+
def __str__(self):
return str(self.__unicode__())
self.index[recipe.name] = recipe
self.index[recipe.clean_name()] = recipe
+ def tags(self):
+ """List all tags used in this cookbook.
+ """
+ tags = set()
+ for recipe in self:
+ if recipe.tags != None:
+ tags = tags.union(set(recipe.tags))
+ return sorted(tags)
+
+ def tagged(self, tags=None):
+ """Iterate through all recipes matching the given list of tags.
+ """
+ for recipe in self:
+ if recipe.matches_tags(tags):
+ yield recipe
+
def test():
import doctest
pass
@cherrypy.expose
- def index(self, status='open', assignee='all', target='all'):
+ def index(self, tag=None):
"""Recipe index page.
- Recipes can be filtered or sorted using a variety of criteria.
+ Recipes can be filtered by tag.
"""
+ if isinstance(tag, types.StringTypes):
+ tag = [tag]
template = self.env.get_template('recipes.html')
return template.render(cookbook=self.cookbook,
- recipes=list(self.cookbook))
+ recipes=list(self.cookbook.tagged(tag)),
+ selected_tags=tag)
@cherrypy.expose
def recipe(self, name=None):
{% extends "base.html" %}
{% block page_title %}
- Index
+ Index {% if selected_tags %}({{ ', '.join(selected_tags) }}){% endif %}
{% endblock %}
{% block content %}
+ <form action="">
+ <select name="tag" size="5" multiple="multiple">
+ {% for tag in cookbook.tags() %}
+ <option value="{{ tag }}">{{ tag }}</option>
+ {% endfor %}
+ </select>
+ <input type="submit" value="Show only selected tags" />
+ </form>
<ul id="recipe-list">
{% for recipe in recipes %}
<li><a href="recipe?name={{ recipe.clean_name() }}">