|
1 |
| -#!/usr/bin/env python |
2 |
| -# |
3 |
| -# Copyright 2007 Google Inc. |
4 |
| -# |
5 |
| -# Licensed under the Apache License, Version 2.0 (the "License"); |
6 |
| -# you may not use this file except in compliance with the License. |
7 |
| -# You may obtain a copy of the License at |
8 |
| -# |
9 |
| -# http://www.apache.org/licenses/LICENSE-2.0 |
10 |
| -# |
11 |
| -# Unless required by applicable law or agreed to in writing, software |
12 |
| -# distributed under the License is distributed on an "AS IS" BASIS, |
13 |
| -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
| -# See the License for the specific language governing permissions and |
15 |
| -# limitations under the License. |
16 |
| -# |
17 | 1 | import os
|
| 2 | +import sys |
18 | 3 |
|
19 |
| -from google.appengine.api import users |
20 |
| -from google.appengine.ext import webapp |
21 |
| -from google.appengine.ext.webapp import template |
22 |
| -from google.appengine.ext.webapp import util |
| 4 | +from django.conf import settings |
23 | 5 |
|
24 |
| -from emails import * |
25 |
| -from model import * |
| 6 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'contrib')) |
26 | 7 |
|
27 |
| -import functools |
28 |
| -import urllib |
| 8 | +import appengine_config |
29 | 9 |
|
30 |
| -def authenticated(method): |
31 |
| - @functools.wraps(method) |
32 |
| - def wrapper(self, *args, **kwargs): |
33 |
| - # TODO: handle post requests separately |
34 |
| - user = users.get_current_user() |
35 |
| - if not user: |
36 |
| - self.redirect(users.create_login_url(self.request.uri)) |
37 |
| - return None |
38 |
| - return method(self, *args, **kwargs) |
39 |
| - return wrapper |
| 10 | +from routes import ROUTES |
40 | 11 |
|
| 12 | +import webapp2 |
41 | 13 |
|
42 |
| -class BaseHandler(webapp.RequestHandler): |
43 |
| - def get_user(self): |
44 |
| - '''Returns the user object on authenticated requests''' |
45 |
| - user = users.get_current_user() |
46 |
| - assert user |
| 14 | +""" |
| 15 | + .__ |
| 16 | + _____ _____ |__| ____ |
| 17 | + / \\__ \ | |/ \ |
| 18 | +| Y Y \/ __ \| | | \ |
| 19 | +|__|_| (____ /__|___| / |
| 20 | + \/ \/ \/ |
47 | 21 |
|
48 |
| - userObj = User.all().filter("email =", user.email()).fetch(1) |
49 |
| - if not userObj: |
50 |
| - userObj = User(email=user.email()) |
51 |
| - userObj.put() |
52 |
| - else: |
53 |
| - userObj = userObj[0] |
54 |
| - return userObj |
55 |
| - |
56 |
| - def render(self, template_name, template_values): |
57 |
| - #self.response.headers['Content-Type'] = 'text/html' |
58 |
| - path = os.path.join(os.path.dirname(__file__), 'templates/%s.html' % template_name) |
59 |
| - self.response.out.write(template.render(path, template_values)) |
60 |
| - |
| 22 | +""" |
61 | 23 |
|
62 |
| -class UserHandler(BaseHandler): |
63 |
| - """Show a given user's snippets.""" |
64 |
| - |
65 |
| - @authenticated |
66 |
| - def get(self, email): |
67 |
| - user = self.get_user() |
68 |
| - email = urllib.unquote_plus(email) |
69 |
| - desired_user = user_from_email(email) |
70 |
| - snippets = desired_user.snippet_set |
71 |
| - snippets = sorted(snippets, key=lambda s: s.date, reverse=True) |
72 |
| - following = email in user.following |
73 |
| - tags = [(t, t in user.tags_following) for t in desired_user.tags] |
74 |
| - |
75 |
| - template_values = { |
76 |
| - 'current_user' : user, |
77 |
| - 'user': desired_user, |
78 |
| - 'snippets': snippets, |
79 |
| - 'following': following, |
80 |
| - 'tags': tags |
81 |
| - } |
82 |
| - self.render('user', template_values) |
83 |
| - |
84 |
| - |
85 |
| -class FollowHandler(BaseHandler): |
86 |
| - """Follow a user or tag.""" |
87 |
| - @authenticated |
88 |
| - def get(self): |
89 |
| - user = self.get_user() |
90 |
| - desired_tag = self.request.get('tag') |
91 |
| - desired_user = self.request.get('user') |
92 |
| - continue_url = self.request.get('continue') |
93 |
| - |
94 |
| - if desired_tag and (desired_tag not in user.tags_following): |
95 |
| - user.tags_following.append(desired_tag) |
96 |
| - user.put() |
97 |
| - if desired_user and (desired_user not in user.following): |
98 |
| - user.following.append(desired_user) |
99 |
| - user.put() |
100 |
| - |
101 |
| - self.redirect(continue_url) |
102 |
| - |
103 |
| - |
104 |
| -class UnfollowHandler(BaseHandler): |
105 |
| - """Unfollow a user or tag.""" |
106 |
| - @authenticated |
107 |
| - def get(self): |
108 |
| - user = self.get_user() |
109 |
| - desired_tag = self.request.get('tag') |
110 |
| - desired_user = self.request.get('user') |
111 |
| - continue_url = self.request.get('continue') |
112 |
| - |
113 |
| - if desired_tag and (desired_tag in user.tags_following): |
114 |
| - user.tags_following.remove(desired_tag) |
115 |
| - user.put() |
116 |
| - if desired_user and (desired_user in user.following): |
117 |
| - user.following.remove(desired_user) |
118 |
| - user.put() |
119 |
| - |
120 |
| - self.redirect(continue_url) |
121 |
| - |
122 |
| - |
123 |
| -class TagHandler(BaseHandler): |
124 |
| - """View this week's snippets in a given tag.""" |
125 |
| - @authenticated |
126 |
| - def get(self, tag): |
127 |
| - user = self.get_user() |
128 |
| - d = date_for_retrieval() |
129 |
| - all_snippets = Snippet.all().filter("date =", d).fetch(500) |
130 |
| - if (tag != 'all'): |
131 |
| - all_snippets = [s for s in all_snippets if tag in s.user.tags] |
132 |
| - following = tag in user.tags_following |
133 |
| - |
134 |
| - template_values = { |
135 |
| - 'current_user' : user, |
136 |
| - 'snippets': all_snippets, |
137 |
| - 'following': following, |
138 |
| - 'tag': tag |
139 |
| - } |
140 |
| - self.render('tag', template_values) |
141 |
| - |
142 |
| - |
143 |
| -class MainHandler(BaseHandler): |
144 |
| - """Show list of all users and acting user's settings.""" |
145 |
| - |
146 |
| - @authenticated |
147 |
| - def get(self): |
148 |
| - user = self.get_user() |
149 |
| - # Update enabled state if requested |
150 |
| - set_enabled = self.request.get('setenabled') |
151 |
| - if set_enabled == '1': |
152 |
| - user.enabled = True |
153 |
| - user.put() |
154 |
| - elif set_enabled == '0': |
155 |
| - user.enabled = False |
156 |
| - user.put() |
157 |
| - |
158 |
| - # Update tags if sent |
159 |
| - tags = self.request.get('tags') |
160 |
| - if tags: |
161 |
| - user.tags = [s.strip() for s in tags.split(',')] |
162 |
| - user.put() |
163 |
| - |
164 |
| - # Fetch user list and display |
165 |
| - raw_users = User.all().order('email').fetch(500) |
166 |
| - following = compute_following(user, raw_users) |
167 |
| - all_users = [(u, u.email in following) for u in raw_users] |
168 |
| - all_tags = set() |
169 |
| - for u in raw_users: |
170 |
| - all_tags.update(u.tags) |
171 |
| - all_tags = [(t, t in user.tags_following) for t in all_tags] |
172 |
| - |
173 |
| - template_values = { |
174 |
| - 'current_user' : user, |
175 |
| - 'all_users': all_users, |
176 |
| - 'all_tags': all_tags |
177 |
| - } |
178 |
| - self.render('index', template_values) |
| 24 | +config = {} |
| 25 | +config['webapp2_extras.sessions'] = { |
| 26 | + 'secret_key': settings.SESSION_SECRET_KEY, |
| 27 | +} |
179 | 28 |
|
| 29 | +application = webapp2.WSGIApplication(ROUTES, config=config, debug=settings.DEBUG) |
180 | 30 |
|
| 31 | +""" |
181 | 32 | def main():
|
182 |
| - application = webapp.WSGIApplication( |
183 |
| - [('/', MainHandler), |
184 |
| - ('/user/(.*)', UserHandler), |
185 |
| - ('/tag/(.*)', TagHandler), |
186 |
| - ('/follow', FollowHandler), |
187 |
| - ('/unfollow', UnfollowHandler), |
188 |
| - ('/reminderemail', ReminderEmail), |
189 |
| - ('/digestemail', DigestEmail), |
190 |
| - ('/onereminder', OneReminderEmail), |
191 |
| - ('/onedigest', OneDigestEmail)], |
192 |
| - debug=True) |
193 |
| - util.run_wsgi_app(application) |
| 33 | + run_wsgi_app(application) |
194 | 34 |
|
| 35 | +if __name__ == "__main__": |
| 36 | + main() |
195 | 37 |
|
196 |
| -if __name__ == '__main__': |
197 |
| - main() |
| 38 | +""" |
0 commit comments