1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Cookbook.
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.
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.
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/>.
18 """Serve cookbooks over HTTP.
24 from xml.sax import saxutils
27 from jinja2 import Environment, FileSystemLoader
30 class Server (object):
31 """Cookbook web interface."""
33 def __init__(self, cookbook, template_root):
34 self.cookbook = cookbook
35 self.cookbook.make_index()
36 self.env = Environment(loader=FileSystemLoader(template_root))
39 #self.cookbook.save('new-recipe')
43 def index(self, status='open', assignee='all', target='all'):
46 Recipes can be filtered or sorted using a variety of criteria.
48 template = self.env.get_template('recipes.html')
49 return template.render(cookbook=self.cookbook,
50 recipes=list(self.cookbook))
53 def recipe(self, name=None):
54 """Single recipe page.
57 recipe = random.choice(self.cookbook)
59 if type(name) == types.StringType:
60 name = unicode(name, encoding='utf-8')
61 recipe = self.cookbook.index[name]
62 template = self.env.get_template('recipe.html')
63 return template.render(cookbook=self.cookbook, recipe=recipe)
66 def add_tag(self, name, tag):
67 """Add a tag to a single recipe."""
68 if type(name) == types.StringType:
69 name = unicode(name, encoding='utf-8')
70 recipe = self.cookbook.index[name]
71 if recipe.tags == None:
73 if tag not in recipe.tags:
74 recipe.tags.append(tag)
75 with open(recipe.path, 'w') as f:
77 raise cherrypy.HTTPRedirect(
78 'recipe?name=%s' % recipe.clean_name(), status=302)
81 def remove_tag(self, name, tag):
82 """Remove a tag from a single recipe."""
83 if type(name) == types.StringType:
84 name = unicode(name, encoding='utf-8')
85 recipe = self.cookbook.index[name]
86 if recipe.tags == None:
88 if tag in recipe.tags:
89 recipe.tags.remove(tag)
90 with open(recipe.path, 'w') as f:
92 raise cherrypy.HTTPRedirect(
93 'recipe?name=%s' % recipe.clean_name(), status=302)