/
user_app.py
283 lines (261 loc) · 9.43 KB
/
user_app.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
import web
import template
import util
from google.appengine.api import users
from google.appengine.api.memcache import delete as mdel
try:
from google.appengine.api.labs import taskqueue
except ImportError:
from google.appengine.api import taskqueue
from google.appengine.ext import db
from models import User
from forms import profile_form
from forms import prefs_form
from forms import contact_form
urls = (
'', 'redir',
'/([^a-zA-Z].+|us)/contact/?', 'contact',
'/([^a-zA-Z].+|us)/?', 'index',
'/profile/?', 'profile',
'/preferences/?', 'preferences',
)
t = template.env.get_template('form.html')
class contact:
'''/user/[user_id]/contact handler'''
def GET(self, user_id):
if util.user_exists(user_id.lower()) or user_id.lower() == 'us':
t = template.env.get_template('contact.html')
f = contact_form()
user = users.get_current_user()
if user:
return t.render(util.data(
title='Get in touch!',
instructions='''You will always reveal your email address
when you send a message!''',
form=f,
subject=' '.join([user.nickname(), 'wants to get in touch!']),
))
else:
return t.render(util.data(
title='Not allowed!',
instructions='You must be signed in to send messages!',
))
else:
raise web.notfound()
def POST(self, user_id):
user = users.get_current_user()
if user:
d = web.input()
f = contact_form(message=d.message)
if f.validate() and util.user_exists(user_id.lower()):
taskqueue.add(
url='/task/send_mail',
queue_name='email-throttle',
params={
'sender_id': user.user_id(),
'recipient_id': user_id,
'message': f.message.data,
},
)
raise web.seeother('/' + user_id + '#message_sent')
elif f.validate() and user_id.lower() == 'us':
taskqueue.add(
url='/task/send_mail',
queue_name='email-throttle',
params={
'sender_id': user.user_id(),
'recipient_id': 'us',
'message': f.message.data,
}
)
raise web.seeother('/' + user_id + '#message_sent')
else:
return t.render(util.data(
title='Get in touch!',
instructions='''You will always reveal your email address
when you send a message!''',
form=f,
subject=' '.join([user.nickname(), 'wants to get in touch!']),
))
else:
return t.render(util.data(
title='Not allowed!',
instructions='You must be signed in to send messages!',
))
class profile:
'''User profile handler'''
def GET(self):
user = users.get_current_user()
if user:
e = util.get_user(user=user)
f = profile_form()
if e.bio:
f = profile_form(
nickname=e.nickname,
first_name=e.bio.first_name,
middle_name=e.bio.middle_name,
last_name=e.bio.last_name,
city=e.bio.city,
state=e.bio.state,
postal_code=e.bio.postal_code,
country=e.bio.country,
bio=e.bio.bio,
)
return t.render(util.data(
form=f,
title='Edit Profile',
instructions='''Please enter whatever information you feel comfortable
sharing. (Please note that your information is not public until you
grant us permission to share it in your Preferences)''',
))
else:
return t.render(util.data(
title='Not Logged In!',
instructions='Please Log in to Edit Your Profile',
))
def POST(self):
user = users.get_current_user()
d = web.input()
f = profile_form(
nickname=d.nickname,
first_name=d.first_name,
middle_name=d.middle_name,
last_name=d.last_name,
city=d.city,
state=d.state,
postal_code=d.postal_code,
country=d.country,
bio=d.bio,
)
if not f.validate():
return t.render(util.data(
form=f,
title='Edit Profile',
instructions='''Please enter whatever information you feel comfortable
sharing. (Please note that your information is not shared.public until you
grant us permission to share it in your Preferences)''',
))
else:
e = util.get_user(user=user)
if e.nickname:
e.nickname = f.nickname.data
db.put(e)
e.bio.first_name = f.first_name.data or ''
e.bio.middle_name = f.middle_name.data or ''
e.bio.last_name = f.last_name.data or ''
e.bio.city = f.city.data or ''
e.bio.state = f.state.data or ''
e.bio.postal_code = f.postal_code.data or ''
e.bio.country = f.country.data or ''
e.bio.bio = f.bio.data or ''
e.bio.put()
mdel(key=user.user_id(), namespace='profile_data')
raise web.seeother('/profile')
class preferences:
'''User preferences handler'''
def GET(self):
user = users.get_current_user()
if user:
e = util.get_user(user=user)
if e.shared is not None:
f = prefs_form(
first_name='first_name' in e.shared.public,
middle_name='middle_name' in e.shared.public,
last_name='last_name' in e.shared.public,
city='city' in e.shared.public,
state='state' in e.shared.public,
postal_code='postal_code' in e.shared.public,
country='country' in e.shared.public,
bio='bio' in e.shared.public,
)
else: #If e.shared if empty then set default to False.
f = prefs_form(
first_name=False,
middle_name=False,
last_name=False,
city=False,
state=False,
postal_code=False,
country=False,
bio=False
)
return t.render(util.data(
form=f,
title='Preferences',
instructions='Please indicate which items you wish to make public.',
))
else:
return t.render(util.data(
title='Not Logged In',
instructions='Please log in to have preferences!',
))
def POST(self):
user = users.get_current_user()
d = web.input(
first_name=False,
middle_name=False,
last_name=False,
city=False,
state=False,
postal_code=False,
country=False,
bio=False,
)
f = profile_form(
first_name=d.first_name,
middle_name=d.middle_name,
last_name=d.last_name,
city=d.city,
state=d.state,
postal_code=d.postal_code,
country=d.country,
bio=d.bio,
)
if not f.validate():
return t.render(util.data(
form=f,
title='Preferences',
instructions='Please indicate which items you wish to make public.',
))
else:
prefs = [i.name for i in f if i.data]
e = util.get_user(user=user)
e.shared.public = prefs
e.shared.put()
mdel(key=user.user_id(), namespace='profile_data')
raise web.seeother('/preferences')
class index:
'''Display a users profile'''
def GET(self, user_id):
t = template.env.get_template('profile.html')
if user_id.lower() == 'us':
user_info = {
'nickname': 'Mr. Roboto',
'first_name': 'The',
'middle_name': 'Connection',
'last_name': 'Machine',
'city': 'Google App Engine',
'state': 'The Internet',
}
else:
try:
e = util.get_user(user_id=user_id.lower())
user_info = util.strip_private_data(e)
except AttributeError:
user_info = {
'nickname': '[deleted]',
'first_name': 'No',
'middle_name': 'such',
'last_name': 'user',
'city': 'reddit.com',
'state': 'The Internet',
}
return t.render(util.data(
info=user_info,
user_id=user_id,
))
class redir:
def GET(self):
raise web.seeother('/')
app = web.application(urls, locals())