Ran update_copyright.py
[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, status='open', assignee='all', target='all'):
44         """Recipe index page.
45
46         Recipes can be filtered or sorted using a variety of criteria.
47         """
48         template = self.env.get_template('recipes.html')
49         return template.render(cookbook=self.cookbook,
50                                recipes=list(self.cookbook))
51
52     @cherrypy.expose
53     def recipe(self, name=None):
54         """Single recipe page.
55         """
56         if name == None:
57             recipe = random.choice(self.cookbook)
58         else:
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)
64 #
65 #    @cherrypy.expose
66 #    def create(self, summary):
67 #        """The view that handles the creation of a new bug."""
68 #        if summary.strip() != '':
69 #            bugdir = self.storage_callbacks.get_bugdir()
70 #            bugdir.new_bug(summary=summary)
71 #            self.commit(u"Created bug: %s" % summary)
72 #        raise cherrypy.HTTPRedirect('/', status=302)
73 #
74 #    @cherrypy.expose
75 #    def comment(self, id, body):
76 #        """The view that handles adding a comment."""
77 #        if body.strip() != '':
78 #            bugdir = self.storage_callbacks.get_bugdir()
79 #            bug = bugdir.bug_from_uuid(id)
80 #            bug.comment_root.new_reply(body=body)
81 #            self.commit(u"Added reply Re: %s" % bug.summary)
82 #            raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
83 #        raise cherrypy.HTTPRedirect('/', status=302)
84 #
85 #    @cherrypy.expose
86 #    def edit(self, id, status=None, target=None, assignee=None, severity=None, summary=None):
87 #        """The view that handles editing bug details."""
88 #        bugdir = self.storage_callbacks.get_bugdir()
89 #        bug = bugdir.bug_from_uuid(id)
90 #
91 #        if summary != None:
92 #            old_summary = bug.summary
93 #            bug.summary = summary
94 #            self.bd.vcs.commit("Changed bug summary: %s -> %s" % (old_summary, bug.summary))
95 #        else:
96 #            bug.status = status if status != 'None' else None
97 #            if target != 'None':
98 #                libbe.target.add_target(bugdir, bug, target)
99 #            bug.assigned = assignee if assignee != 'None' else None
100 #            bug.severity = severity if severity != 'None' else None
101 #            self.bd.vcs.commit("Changed bug values: %s" % bug.summary)
102 #
103 #        raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
104
105 def test():
106     import doctest
107     doctest.testmod()