Began versioning
[cookbook.git] / cookbook / server.py
1 # Copyright
2
3 """Serve cookbooks over HTTP.
4 """
5
6 import os
7 import random
8 import types
9 from xml.sax import saxutils
10
11 import cherrypy
12 from jinja2 import Environment, FileSystemLoader
13
14
15 class Server (object):
16     """Cookbook web interface."""
17
18     def __init__(self, cookbook, template_root):
19         self.cookbook = cookbook
20         self.cookbook.make_index()
21         self.env = Environment(loader=FileSystemLoader(template_root))
22
23     def cleanup(self):
24         #self.cookbook.save('new-recipe')
25         pass
26
27     @cherrypy.expose
28     def index(self, status='open', assignee='all', target='all'):
29         """Recipe index page.
30
31         Recipes can be filtered or sorted using a variety of criteria.
32         """
33         template = self.env.get_template('recipes.html')
34         return template.render(cookbook=self.cookbook,
35                                recipes=list(self.cookbook))
36
37     @cherrypy.expose
38     def recipe(self, name=None):
39         """Single recipe page.
40         """
41         if name == None:
42             recipe = random.choice(self.cookbook)
43         else:
44             if type(name) == types.StringType:
45                 name = unicode(name, encoding='utf-8')
46             recipe = self.cookbook.index[name]
47         template = self.env.get_template('recipe.html')
48         return template.render(cookbook=self.cookbook, recipe=recipe)
49 #
50 #    @cherrypy.expose
51 #    def create(self, summary):
52 #        """The view that handles the creation of a new bug."""
53 #        if summary.strip() != '':
54 #            bugdir = self.storage_callbacks.get_bugdir()
55 #            bugdir.new_bug(summary=summary)
56 #            self.commit(u"Created bug: %s" % summary)
57 #        raise cherrypy.HTTPRedirect('/', status=302)
58 #
59 #    @cherrypy.expose
60 #    def comment(self, id, body):
61 #        """The view that handles adding a comment."""
62 #        if body.strip() != '':
63 #            bugdir = self.storage_callbacks.get_bugdir()
64 #            bug = bugdir.bug_from_uuid(id)
65 #            bug.comment_root.new_reply(body=body)
66 #            self.commit(u"Added reply Re: %s" % bug.summary)
67 #            raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
68 #        raise cherrypy.HTTPRedirect('/', status=302)
69 #
70 #    @cherrypy.expose
71 #    def edit(self, id, status=None, target=None, assignee=None, severity=None, summary=None):
72 #        """The view that handles editing bug details."""
73 #        bugdir = self.storage_callbacks.get_bugdir()
74 #        bug = bugdir.bug_from_uuid(id)
75 #
76 #        if summary != None:
77 #            old_summary = bug.summary
78 #            bug.summary = summary
79 #            self.bd.vcs.commit("Changed bug summary: %s -> %s" % (old_summary, bug.summary))
80 #        else:
81 #            bug.status = status if status != 'None' else None
82 #            if target != 'None':
83 #                libbe.target.add_target(bugdir, bug, target)
84 #            bug.assigned = assignee if assignee != 'None' else None
85 #            bug.severity = severity if severity != 'None' else None
86 #            self.bd.vcs.commit("Changed bug values: %s" % bug.summary)
87 #
88 #        raise cherrypy.HTTPRedirect('/bug?id=%s' % (bug.uuid,), status=302)
89
90 def test():
91     import doctest
92     doctest.testmod()