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.
21 from __future__ import with_statement
26 from xml.sax import saxutils
29 from jinja2 import Environment, FileSystemLoader
32 class Server (object):
33 """Cookbook web interface."""
35 def __init__(self, cookbook, template_root):
36 self.cookbook = cookbook
37 self.cookbook.make_index()
38 self.env = Environment(loader=FileSystemLoader(template_root))
41 #self.cookbook.save('new-recipe')
45 def index(self, tag=None):
48 Recipes can be filtered by tag.
50 if isinstance(tag, types.StringTypes):
52 template = self.env.get_template('recipes.html')
53 return template.render(cookbook=self.cookbook,
54 recipes=list(self.cookbook.tagged(tag)),
58 def recipe(self, name=None):
59 """Single recipe page.
62 recipe = random.choice(self.cookbook)
64 if type(name) == types.StringType:
65 name = unicode(name, encoding='utf-8')
66 recipe = self.cookbook.index[name]
67 template = self.env.get_template('recipe.html')
68 return template.render(cookbook=self.cookbook, recipe=recipe)
71 def add_tag(self, name, tag):
72 """Add a tag to a single recipe."""
73 if type(name) == types.StringType:
74 name = unicode(name, encoding='utf-8')
75 recipe = self.cookbook.index[name]
76 if recipe.tags == None:
78 if tag not in recipe.tags:
79 recipe.tags.append(tag)
80 with open(recipe.path, 'w') as f:
82 raise cherrypy.HTTPRedirect(
83 'recipe?name=%s' % recipe.clean_name(), status=302)
86 def remove_tag(self, name, tag):
87 """Remove a tag from a single recipe."""
88 if type(name) == types.StringType:
89 name = unicode(name, encoding='utf-8')
90 recipe = self.cookbook.index[name]
91 if recipe.tags == None:
93 if tag in recipe.tags:
94 recipe.tags.remove(tag)
95 with open(recipe.path, 'w') as f:
97 raise cherrypy.HTTPRedirect(
98 'recipe?name=%s' % recipe.clean_name(), status=302)