annotate discusssions/handlers.py @ 0:c904249afb04

initial commit of discussions
author k0s <k0scist@gmail.com>
date Sat, 02 Jan 2010 13:36:23 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
2 request handlers:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
3 these are instantiated for every request, then called
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
4 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
5
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
6 from urlparse import urlparse
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
7 from webob import Response, exc
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
8
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
9 class HandlerMatchException(Exception):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
10 """the handler doesn't match the request"""
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
11
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
12 class Handler(object):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
13
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
14 methods = set(['GET']) # methods to listen to
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
15 handler_path = [] # path elements to match
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
16
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
17 @classmethod
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
18 def match(cls, app, request):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
19
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
20 # check the method
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
21 if request.method not in cls.methods:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
22 return None
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
23
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
24 # check the path
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
25 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
26 return None
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
27
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
28 try:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
29 return cls(app, request)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
30 except HandlerMatchException:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
31 return None
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
32
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
33 def __init__(self, app, request):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
34 self.app = app
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
35 self.request = request
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
36 self.application_path = urlparse(request.application_url)[2]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
37
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
38 def link(self, path=(), permanant=False):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
39 if isinstance(path, basestring):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
40 path = [ path ]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
41 path = [ i.strip('/') for i in path ]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
42 if permanant:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
43 application_url = [ self.request.application_url ]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
44 else:
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
45 application_url = [ self.application_path ]
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
46 path = application_url + path
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
47 return '/'.join(path)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
48
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
49 def redirect(self, location):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
50 raise exc.HTTPSeeOther(location=location)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
51
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
52 class GenshiHandler(Handler):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
53
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
54 def __init__(self, app, request):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
55 Handler.__init__(self, app, request)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
56 self.data = { 'request': request,
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
57 'link': self.link }
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
58
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
59 def __call__(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
60 return getattr(self, self.request.method.title())()
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
61
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
62 def Get(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
63 # needs to have self.template set
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
64 template = self.app.loader.load(self.template)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
65 return Response(content_type='text/html',
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
66 body=template.generate(**self.data).render('html'))
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
67
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
68 class Discussions(GenshiHandler):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
69 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
70 ?format=json,mbox,rss,text,snippet
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
71 """
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
72
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
73 template = 'index.html'
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
74 methods=set(['GET', 'POST'])
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
75
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
76 def __init__(self, app, request):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
77 GenshiHandler.__init__(self, app, request)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
78
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
79 def Get(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
80 self.data['name'] = self.request.remote_user or self.app.name
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
81 return GenshiHandler.Get(self)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
82
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
83 def Post(self):
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
84 self.app.name = self.request.POST.get('name', self.app.name)
c904249afb04 initial commit of discussions
k0s <k0scist@gmail.com>
parents:
diff changeset
85 self.redirect(self.link(self.handler_path))