Import with statement so recent server.py changes work with Python 2.5
[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 from __future__ import with_statement
22
23 import os
24 import random
25 import types
26 from xml.sax import saxutils
27
28 import cherrypy
29 from jinja2 import Environment, FileSystemLoader
30
31
32 class Server (object):
33     """Cookbook web interface."""
34
35     def __init__(self, cookbook, template_root):
36         self.cookbook = cookbook
37         self.cookbook.make_index()
38         self.env = Environment(loader=FileSystemLoader(template_root))
39
40     def cleanup(self):
41         #self.cookbook.save('new-recipe')
42         pass
43
44     @cherrypy.expose
45     def index(self, tag=None):
46         """Recipe index page.
47
48         Recipes can be filtered by tag.
49         """
50         if isinstance(tag, types.StringTypes):
51             tag = [tag]
52         template = self.env.get_template('recipes.html')
53         return template.render(cookbook=self.cookbook,
54                                recipes=list(self.cookbook.tagged(tag)),
55                                selected_tags=tag)
56
57     @cherrypy.expose
58     def recipe(self, name=None):
59         """Single recipe page.
60         """
61         if name == None:
62             recipe = random.choice(self.cookbook)
63         else:
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)
69
70     @cherrypy.expose
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:
77             recipe.tags = []
78         if tag not in recipe.tags:
79             recipe.tags.append(tag)
80             with open(recipe.path, 'w') as f:
81                 recipe.save(f)
82         raise cherrypy.HTTPRedirect(
83             'recipe?name=%s' % recipe.clean_name(), status=302)
84
85     @cherrypy.expose
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:
92             return
93         if tag in recipe.tags:
94             recipe.tags.remove(tag)
95             with open(recipe.path, 'w') as f:
96                 recipe.save(f)
97         raise cherrypy.HTTPRedirect(
98             'recipe?name=%s' % recipe.clean_name(), status=302)
99
100
101 def test():
102     import doctest
103     doctest.testmod()