-
Notifications
You must be signed in to change notification settings - Fork 7
/
account.py
executable file
·97 lines (79 loc) · 3.51 KB
/
account.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
from google.appengine.dist import use_library
use_library('django', '1.2')
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.db import Key
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app, login_required
from models import *
from utils import *
class AccountCollectionHandler(webapp.RequestHandler):
@login_required
def get(self):
#user = users.get_current_user()
#account = Account.all().filter('user =', user).get()
account = get_account()
if account:
# account exists
self.redirect('/account/' + str(account.key()))
else:
# no account for this user
self.response.out.write(template.render('templates/account_create.html', None))
def post(self):
user = users.get_current_user()
nickname = self.request.get('nickname')
if not len(nickname):
# no nickname entered
self.response.out.write(template.render('templates/account_create.html',
{'error_msg': 'Please enter a nickname.'}
))
else:
account = create_account(user, nickname)
self.redirect('/account/' + str(account.key()))
class AccountHandler(webapp.RequestHandler):
@login_required
def get(self, account_key):
account = get_account(account_key)
if account is None:
# account doesn't exist
self.error(404)
self.response.out.write("no such account")
else:
self.response.out.write(template.render('templates/account.html',
{'account': account}
))
class AccountEditHandler(webapp.RequestHandler):
@login_required
def get(self, account_key):
account = Account.get(account_key)
if not account_is_current(account):
self.error(403)
self.response.out.write("cannot edit settings for other users")
else:
self.response.out.write(template.render('templates/account_edit.html',
{ 'form': AccountForm(instance=account) }
))
def post(self, account_key):
account = Account.get(account_key)
if not account_is_current(account):
self.error(403)
self.response.out.write("cannot edit settings for other users")
else:
form = AccountForm(data=self.request.POST, instance=account)
if form.is_valid():
# Save the data, and redirect to the view page
entity = form.save()
self.redirect('/account/' + account_key)
else:
# Reprint the form
self.response.out.write(template.render('templates/account_edit.html',
{ 'form': form }
))
application = webapp.WSGIApplication([('/account/', AccountCollectionHandler),
(r'/account/([^/]+)', AccountHandler),
(r'/account/([^/]+)/edit', AccountEditHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()