-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme.py
66 lines (52 loc) · 2.02 KB
/
me.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
import webapp2
import json
from google.appengine.api import users
from google.appengine.ext.webapp import util
import utils
from model import User
class MeHandler(webapp2.RequestHandler):
def get(self):
""" Return info about current logged in user
Automatically create internal user models for admin google users.
"""
user = utils.get_current_user()
if not user:
google_user = users.get_current_user()
if not google_user:
self.error(403)
return
if users.is_current_user_admin():
user = utils.create_user(google_user)
else:
self.error(401)
return
data = user.to_struct()
data['is_admin'] = users.is_current_user_admin()
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write(json.dumps(data))
def post(self):
""" Changes the current users nickname """
new_nickname = self.request.get('nickname')
if not new_nickname:
self.error(409)
return
new_nickname_lower = new_nickname.lower()
user = utils.get_current_user()
if not user:
self.error(403)
return
if new_nickname != user.nickname:
others = User.all().filter('nickname_lower =', new_nickname_lower).get()
if others and others.key() != user.key():
self.error(409)
return
user.nickname_lower = new_nickname_lower
user.nickname = new_nickname
user.save()
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write(json.dumps(user.to_struct()))
app = webapp2.WSGIApplication([
('/me', MeHandler),
], debug=True)