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