Mercurial > hg > TagInTheMiddle
annotate taginthemiddle/handlers.py @ 0:1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 04 May 2010 08:37:15 -0700 |
parents | |
children | 1182315b18ac |
rev | line source |
---|---|
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 request handlers: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 these are instantiated for every request, then called |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 from urlparse import urlparse |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 from webob import Response, exc |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class HandlerMatchException(Exception): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 """the handler doesn't match the request""" |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 class Handler(object): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 methods = set(['GET']) # methods to listen to |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 handler_path = [] # path elements to match |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 @classmethod |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 def match(cls, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 # check the method |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 if request.method not in cls.methods: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 # check the path |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 try: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 return cls(app, request) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 except HandlerMatchException: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 def __init__(self, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 self.app = app |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 self.request = request |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 self.application_path = urlparse(request.application_url)[2] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 def link(self, path=(), permanant=False): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 if isinstance(path, basestring): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 path = [ path ] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 path = [ i.strip('/') for i in path ] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 if permanant: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 application_url = [ self.request.application_url ] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 else: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 application_url = [ self.application_path ] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 path = application_url + path |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 return '/'.join(path) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 def redirect(self, location): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 raise exc.HTTPSeeOther(location=location) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 class GenshiHandler(Handler): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 def __init__(self, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 Handler.__init__(self, app, request) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 self.data = { 'request': request, |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 'link': self.link } |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 def __call__(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 return getattr(self, self.request.method.title())() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 def Get(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 # needs to have self.template set |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 template = self.app.loader.load(self.template) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 return Response(content_type='text/html', |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 body=template.generate(**self.data).render('html')) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 class TagCloud(GenshiHandler): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 template = 'cloud.html' |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 methods = set(['GET']) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 @classmethod |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 def match( |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 class ViewTags(GenshiHandler): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 # TODO: RSS |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 template = 'tags.html' |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 methods = set(['GET']) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 def match(cls, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 pass |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
82 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
83 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 class PostTags(Handler): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
85 methods = set(['POST']) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
86 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
87 @classmethod |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 def match(cls, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
90 # check the method |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
91 if request.method not in cls.methods: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
92 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
93 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
94 # check auth |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
95 if not request.remote_user: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
96 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
97 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
98 # check the path |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
99 if request.path_info.endswith('/%s' % app.post_url): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
100 try: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
101 return cls(app, request) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
102 except HandlerMatchException: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
103 return None |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
104 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
105 def __call__(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
106 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
107 # get the url |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
108 url = self.request.path_info.rsplit('/' + self.app.post_url, 1)[0] |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
109 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
110 # add and remove tags |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
111 # TODO |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
112 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
113 # redirect to original resource |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
114 return exc.HTTPSeeOther(location=url) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
115 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
116 class Index(GenshiHandler): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
117 template = 'index.html' |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
118 methods=set(['GET', 'POST']) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
119 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
120 def __init__(self, app, request): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
121 GenshiHandler.__init__(self, app, request) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
122 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
123 def Get(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
124 self.data['name'] = self.request.remote_user or self.app.name |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
125 return GenshiHandler.Get(self) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
126 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
127 def Post(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
128 self.app.name = self.request.POST.get('name', self.app.name) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
129 self.redirect(self.link(self.handler_path)) |