-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
247 lines (194 loc) · 7.54 KB
/
Copy pathmain.py
File metadata and controls
247 lines (194 loc) · 7.54 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""Main application module."""
import json
import logging
import os
from google.appengine.api import images
from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext import ndb
import jinja2
import webapp2
import models
# Configure jinja
TEMPLATES_LOADER = jinja2.FileSystemLoader([
os.path.dirname(__file__),
os.path.join(os.path.dirname(__file__), 'templates')
])
JINJA_ENVIRONMENT = jinja2.Environment(
loader=TEMPLATES_LOADER,
extensions=['jinja2.ext.autoescape'],
variable_start_string='[[',
variable_end_string=']]',
autoescape=True)
#------------------------ Module functions ------------------------------------
def return_json(response, data, encoder=None):
"""Creates a JSON response from in the request handler.
Params:
response: webapp2.Response
data: Any JSON-encodable data type
encoder: (Optional) a custom JSONEncoder object
"""
response.headers['Content-Type'] = 'application/json'
if encoder:
response.write(encoder.encode(data))
else:
response.write(json.dumps(data))
def login_required(action):
"""Action decorator to ensure the user is authenticated."""
def authenticated(request_handler, *args, **kwargs):
request_handler.user = users.get_current_user()
if not request_handler.user:
request_handler.abort(403)
request_handler.user_id = request_handler.user.user_id()
action(request_handler, *args, **kwargs)
return authenticated
def get_user_profile(user_id):
profile_key = ndb.Key('Profile', user_id)
# Get the profile from memcache, then the datastore, or create
profile = memcache.get(profile_key.urlsafe())
if not profile:
profile = profile_key.get()
memcache.set(profile_key.urlsafe(), profile)
if not profile:
profile = models.Profile(key=profile_key)
profile.put()
memcache.set(profile_key.urlsafe(), profile)
return profile
#------------------------ Exceptions ------------------------------------------
class EntityNotFoundException(Exception):
def __init__(self, kind, entity_id):
self.kind = kind
self.entity_id = entity_id
def __str__(self):
return '{} with id={} does not navbar-rightexist!'.format(self.kind,
self.entity_id)
class EntityExistsException(Exception):
def __init__(self, kind, entity_id):
self.kind = kind
self.entity_id = entity_id
def __str__(self):
return '{} with id={} already exists!'.format(self.kind, self.entity_id)
#------------------------ Request handlers ------------------------------------
class BaseHandler(webapp2.RequestHandler):
def handle_exception(self, exception, debug):
# Log the error.
logging.exception(exception)
# If the exception is a HTTPException, use its error code.
# Otherwise use a generic 500 error code.
if isinstance(exception, webapp2.HTTPException):
self.response.set_status(exception.code)
else:
self.response.set_status(500)
# Return JSON
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.dumps(
{'error': True, 'errorMessage': str(exception)}
))
class ProfileHandler(BaseHandler):
"""Profile request handler."""
@login_required
def get(self):
profile = ndb.Key('Profile', self.user_id).get()
if profile and profile.avatar:
profile.avatar = '/uploads/' + profile.key.urlsafe() + '.png'
return_json(self.response, profile, models.NdbModelEncoder())
@login_required
def post(self):
profile_key = ndb.Key('Profile', self.user_id)
profile = profile_key.get()
if not profile:
profile = models.Profile(key=profile_key)
profile.avatar = images.resize(self.request.get('avatar'), 72, 72)
profile.put()
profile.avatar = '/uploads/' + profile.key.urlsafe() + '.png'
return_json(self.response, profile, models.NdbModelEncoder())
class TopicsHandler(BaseHandler):
"""Profile request handler."""
def get(self):
# Get the user's profile and store in memcache
query = models.Topic.query().order(-models.Topic.created)
response = {
'topics': [],
'can_vote': False,
'my_topics': {},
'my_votes': {}
}
for t in query.fetch(1000):
if t.image:
t.image = '/uploads/' + t.key.urlsafe() + '.png'
response['topics'].append(t)
user = users.get_current_user()
if user:
profile = get_user_profile(user.user_id())
response['can_vote'] = True
response['my_topics'] = {t.id(): True for t in profile.topics}
response['my_votes'] = {v.topic.id(): v.vote for v in profile.votes}
return_json(self.response, response, models.NdbModelEncoder())
@login_required
def put(self, id, vote):
topic = ndb.Key('Topic', int(id)).get()
if not topic:
self.abort(404)
if vote == 'up':
topic.up_votes += 1
elif vote == 'down':
topic.down_votes += 1
topic.put()
if topic and topic.image:
topic.image = '/uploads/' + topic.key.urlsafe() + '.png'
# Update profile
profile = get_user_profile(self.user_id)
profile.votes.append(models.Vote(topic=topic.key, vote=vote))
profile.put()
memcache.set(profile.key.urlsafe(), profile)
response = {
'topic': topic,
'my_votes': {v.topic.id(): v.vote for v in profile.votes}
}
return_json(self.response, response, models.NdbModelEncoder())
@login_required
def post(self):
profile = get_user_profile(self.user_id)
topic = models.Topic(name=self.request.get('name'))
if self.request.get('tags'):
topic.tags = self.request.get('tags').split(',')
if self.request.get('image'):
topic.image = images.resize(self.request.get('image'), 400)
topic_key = topic.put()
profile.topics.append(topic_key)
profile.put()
memcache.set(profile.key.urlsafe(), profile)
if topic and topic.image:
topic.image = '/uploads/' + topic.key.urlsafe() + '.png'
return_json(self.response, topic, models.NdbModelEncoder())
class ImageHandler(BaseHandler):
def get(self, urlsafe_key):
entity = ndb.Key(urlsafe=urlsafe_key[:-4]).get()
if not entity:
self.abort(404)
self.response.headers['Content-Type'] = 'image/png'
if entity.key.kind() == 'Topic':
self.response.out.write(entity.image)
elif entity.key.kind() == 'Profile':
self.response.out.write(entity.avatar)
class IndexHandler(BaseHandler):
"""Index page request handler."""
def get(self):
user = users.get_current_user()
view_vars = {
'username': '',
'login': ''
}
if user:
view_vars['username'] = user.nickname()
else:
view_vars['login'] = users.create_login_url()
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(view_vars))
application = webapp2.WSGIApplication([
('/', IndexHandler),
('/profile', ProfileHandler),
('/topics', TopicsHandler),
('/topics/(.+)/(up|down)', TopicsHandler),
('/uploads/(.+)', ImageHandler)
], debug=os.environ['SERVER_SOFTWARE'].startswith('Development'))