Add ability to filter recipe lists by tag
[cookbook.git] / cookbook / server.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Cookbook.
4 #
5 # Cookbook is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # Cookbook is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Cookbook.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Serve cookbooks over HTTP.
19 """
20
21 import os
22 import random
23 import types
24 from xml.sax import saxutils
25
26 import cherrypy
27 from jinja2 import Environment, FileSystemLoader
28
29
30 class Server (object):
31     """Cookbook web interface."""
32
33     def __init__(self, cookbook, template_root):
34         self.cookbook = cookbook
35         self.cookbook.make_index()
36         self.env = Environment(loader=FileSystemLoader(template_root))
37
38     def cleanup(self):
39         #self.cookbook.save('new-recipe')
40         pass
41
42     @cherrypy.expose
43     def index(self, tag=None):
44         """Recipe index page.
45
46         Recipes can be filtered by tag.
47         """
48         if isinstance(tag, types.StringTypes):
49             tag = [tag]
50         template = self.env.get_template('recipes.html')
51         return template.render(cookbook=self.cookbook,
52                                recipes=list(self.cookbook.tagged(tag)),
53                                selected_tags=tag)
54
55     @cherrypy.expose
56     def recipe(self, name=None):
57         """Single recipe page.
58         """
59         if name == None:
60             recipe = random.choice(self.cookbook)
61         else:
62             if type(name) == types.StringType:
63                 name = unicode(name, encoding='utf-8')
64             recipe = self.cookbook.index[name]
65         template = self.env.get_template('recipe.html')
66         return template.render(cookbook=self.cookbook, recipe=recipe)
67
68     @cherrypy.expose
69     def add_tag(self, name, tag):
70         """Add a tag to a single recipe."""
71         if type(name) == types.StringType:
72             name = unicode(name, encoding='utf-8')
73         recipe = self.cookbook.index[name]
74         if recipe.tags == None:
75             recipe.tags = []
76         if tag not in recipe.tags:
77             recipe.tags.append(tag)
78             with open(recipe.path, 'w') as f:
79                 recipe.save(f)
80         raise cherrypy.HTTPRedirect(
81             'recipe?name=%s' % recipe.clean_name(), status=302)
82
83     @cherrypy.expose
84     def remove_tag(self, name, tag):
85         """Remove a tag from a single recipe."""
86         if type(name) == types.StringType:
87             name = unicode(name, encoding='utf-8')
88         recipe = self.cookbook.index[name]
89         if recipe.tags == None:
90             return
91         if tag in recipe.tags:
92             recipe.tags.remove(tag)
93             with open(recipe.path, 'w') as f:
94                 recipe.save(f)
95         raise cherrypy.HTTPRedirect(
96             'recipe?name=%s' % recipe.clean_name(), status=302)
97
98
99 def test():
100     import doctest
101     doctest.testmod()