-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyday.py
530 lines (467 loc) · 19.6 KB
/
pyday.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# -*- coding: utf-8 *-*
import os
import cgi
import datetime
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import forms
import utils
from db import db
from db.model import TwitterProfile
from gaesessions import get_current_session
template.register_template_library('customtags.tags')
MESSAGE_REGISTER = (u'Voy a asistir al %23PyDayCba el 15 de septiembre - '
u'http://pydaycba.com.ar ¡Inscribite vos también!')
MESSAGE_PROPOSE = (u'Propuse la charla %s para el %%23PyDayCba - '
u'http://pydaycba.com.ar ¡Sumate!')
FACEBOOK_MESSAGE = (u'http://www.facebook.com/sharer/sharer.php?'
u'u=http://pydaycba.com.ar/')
NOW = datetime.datetime.utcnow() - datetime.timedelta(hours=3)
providers = {
'google': 'www.google.com/accounts/o8/id',
'yahoo': 'yahoo.com',
'aol': 'aol.com',
'openid': 'myopenid.com',
}
def get_twitter_message(message):
return (u'https://twitter.com/intent/tweet?text=%s' %
message.replace(' ', '+'))
class PyDayHandler(webapp.RequestHandler):
def user_login(self):
result = {}
user = users.get_current_user()
is_profile = False
if user is None:
session = get_current_session()
twitter_user = session.get("twitter_user")
if twitter_user is not None:
user = TwitterProfile.get_by_key_name(twitter_user)
is_profile = True
message = utils.days_left_message()
result['daysleft0'] = message[0]
result['daysleft1'] = message[1]
result['daysleft2'] = message[2]
result['propose_ended'] = datetime.datetime(2012, 8, 25) < NOW
result['register_ended'] = datetime.datetime(2012, 9, 14) < NOW
result['tshirt_req_ended'] = datetime.datetime(2012, 8, 27) < NOW
if user: # signed in already
result['user'] = user
if is_profile:
result['logout'] = '/oauth/signout'
else:
result['logout'] = users.create_logout_url(self.request.uri)
result['username'] = user.nickname()
result['is_attendee'] = db.user_is_attendee(
result.get('user', None))
else: # let user choose authenticator
result['user'] = None
result['login'] = '/login'
return result
def go_to_login(self, data):
for name, uri in providers.items():
data[name] = users.create_login_url(federated_identity=uri)
path = os.path.join(os.path.dirname(__file__),
"templates/others/login.html")
self.response.out.write(template.render(path, data))
def show_error(self, page_base, message, data):
data['showerror'] = 'block'
data['errormessage'] = message
if 'form' in data:
data['form'].errors.clear()
path = os.path.join(os.path.dirname(__file__), page_base)
self.response.out.write(template.render(path, data))
class NotFoundPageHandler(PyDayHandler):
def get(self):
result = self.user_login()
path = os.path.join(os.path.dirname(__file__),
"templates/error_base.html")
result['title'] = 'Error 404'
result['message'] = 'La página a la que intentó acceder no existe.'
self.response.out.write(template.render(path, result))
class MainPage(PyDayHandler):
def get(self):
result = self.user_login()
path = os.path.join(os.path.dirname(__file__), "templates/index.html")
self.response.out.write(template.render(path, result))
class Schedule(PyDayHandler):
def get(self):
result = self.user_login()
path = os.path.join(os.path.dirname(__file__),
"templates/conference/schedule.html")
self.response.out.write(template.render(path, result))
class Register(PyDayHandler):
def get(self):
result = self.user_login()
if result['register_ended']:
path = os.path.join(os.path.dirname(__file__),
"templates/error_base.html")
result['title'] = 'Error'
result['message'] = u'Ya finalizó el período de inscripción.'
self.response.out.write(template.render(path, result))
else:
if result.get('user', None):
result['showerror'] = 'none'
form = forms.AttendeeForm()
form.initial = {'allow_contact': True,
'in_attendees': True}
result['form'] = form
path = os.path.join(os.path.dirname(__file__),
"templates/user/register.html")
self.response.out.write(template.render(path, result))
else:
self.go_to_login(result)
def post(self):
result = self.user_login()
values = forms.AttendeeForm(data=self.request.POST)
result['form'] = values
if result.get('user', None):
if not values.is_valid():
#error page
self.show_error("templates/user/register.html",
u'Falta completar alguno de los datos requeridos.', result)
return
attendee = values.save(commit=False)
user = result['user']
if isinstance(user, users.User):
attendee.userId = result['user']
else:
attendee.profile = result['user']
registered = db.add_attendee(attendee)
if registered:
data = {}
data['title'] = (
u'Te registraste exitosamente en el PyDay Córdoba 2012.')
data['message'] = (
u'Podés ver <a href="/attendees">quiénes van'
' a asistir</a> o compartirlo en:')
data['share_twitter'] = get_twitter_message(MESSAGE_REGISTER)
data['share_facebook'] = FACEBOOK_MESSAGE
data.update(result)
path = os.path.join(os.path.dirname(__file__),
"templates/user/success.html")
self.response.out.write(template.render(path, data))
else:
self.show_error("templates/user/register.html",
u'Hubo un problema al intentar procesar la inscripción '
u'o el usuario ingresado ya existe.',
result)
# show error
else:
#show error page
self.show_error("templates/user/register.html",
u'No hay una sesión iniciada.', result)
class Propose(PyDayHandler):
def get(self):
result = self.user_login()
if result['propose_ended']:
path = os.path.join(os.path.dirname(__file__),
"templates/error_base.html")
result['title'] = 'Error'
result['message'] = 'Ya no se reciben propuestas de charlas.'
self.response.out.write(template.render(path, result))
else:
registered = db.user_is_attendee(result.get('user', None))
if not registered:
self.redirect('/register')
return
if result.get('user', None):
form = forms.TalkForm()
result['form'] = form
path = os.path.join(os.path.dirname(__file__),
"templates/others/propose.html")
self.response.out.write(template.render(path, result))
else:
self.redirect(result['login'])
def post(self):
result = self.user_login()
# Collect data
values = forms.TalkForm(data=self.request.POST)
result['form'] = values
if result.get('user', None):
if not values.is_valid():
#error page
self.show_error("templates/others/propose.html",
u'Falta completar alguno de los datos requeridos.', result)
return
talk = values.save(commit=False)
user = result['user']
if isinstance(user, users.User):
talk.userId = user
else:
talk.profile = user
saved = db.add_talk(talk)
if saved:
data = {}
data['title'] = u'Tu propuesta fue cargada con éxito.'
data['message'] = u'Podés compartirlo en:'
data['share_twitter'] = get_twitter_message(
MESSAGE_PROPOSE % talk.title)
data['share_facebook'] = FACEBOOK_MESSAGE
data.update(result)
path = os.path.join(os.path.dirname(__file__),
"templates/user/success.html")
self.response.out.write(template.render(path, data))
else:
#show error page
self.show_error("templates/others/propose.html",
u'Hubo un problema al intentar registrar la charla.', result)
else:
#show error page
self.show_error("templates/others/propose.html",
u'Hubo un problema al intentar registrar la charla.', result)
class About(PyDayHandler):
def get(self):
result = self.user_login()
path = os.path.join(os.path.dirname(__file__),
"templates/conference/about.html")
self.response.out.write(template.render(path, result))
class Venue(PyDayHandler):
def get(self):
result = self.user_login()
path = os.path.join(os.path.dirname(__file__),
"templates/conference/venue.html")
self.response.out.write(template.render(path, result))
class Attendees(PyDayHandler):
def get(self):
result = self.user_login()
attendees = db.get_attendees()
len_attendees = attendees.count()
attendees.filter('in_attendees =', True)
result['attendees'] = attendees
result['len_attendees'] = len_attendees
path = os.path.join(os.path.dirname(__file__),
"templates/others/attendees.html")
self.response.out.write(template.render(path, result))
class Profile(PyDayHandler):
def get(self):
result = self.user_login()
if result.get('user', None):
attendee = db.get_profile(result['user'])
if attendee is None:
self.redirect('/register')
return
in_attendees = u'Sí' if attendee.in_attendees else 'No'
allow_contact = u'Sí' if attendee.allow_contact else 'No'
assist_dinner = u'Sí' if attendee.assist_dinner else 'No'
data = {
'name': attendee.name,
'surname': attendee.surname,
'nick': attendee.nick,
'email': attendee.email,
'level': attendee.level,
'country': attendee.country,
'state': attendee.state,
'tel': attendee.tel,
'personal_page': attendee.personal_page,
'company': attendee.company,
'company_page': attendee.company_page,
'in_attendees': in_attendees,
'allow_contact': allow_contact,
'biography': attendee.biography,
'assist_dinner': assist_dinner,
}
if attendee.cv:
data['cv'] = attendee.cv
result.update(data)
talks = db.get_user_talks(result['user'])
if talks:
result['talks'] = talks
tshirt = db.get_tshirts_requested(result['user'])
if tshirt:
result['tshirt'] = tshirt
path = os.path.join(os.path.dirname(__file__),
"templates/user/profile.html")
self.response.out.write(template.render(path, result))
else:
self.redirect('/register')
class Login(PyDayHandler):
def get(self):
result = self.user_login()
if result['register_ended']:
path = os.path.join(os.path.dirname(__file__),
"templates/error_base.html")
result['title'] = 'Error'
result['message'] = u'Ya finalizó el período de inscripción.'
self.response.out.write(template.render(path, result))
else:
if result.get('user', None):
self.redirect('/')
return
self.go_to_login(result)
class ModifyProfile(PyDayHandler):
def get(self):
result = self.user_login()
if result.get('user', None):
attendee = db.get_profile(result['user'])
if attendee is None:
self.redirect('/register')
return
result['form'] = forms.AttendeeForm(instance=attendee)
path = os.path.join(os.path.dirname(__file__),
"templates/user/register.html")
self.response.out.write(template.render(path, result))
def post(self):
result = self.user_login()
values = forms.AttendeeForm(data=self.request.POST)
result['form'] = values
if result.get('user', None):
if not values.is_valid():
#error page
self.show_error("templates/user/register.html",
u'Falta completar alguno de los datos requeridos.', result)
return
attendee = values.save(commit=False)
user = result['user']
if isinstance(user, users.User):
attendee.userId = user
else:
attendee.profile = user
registered = db.update_attendee(attendee)
if registered:
data = {}
data['title'] = (
u'Registración actualizada exitosamente para'
u' el PyDay Córdoba 2012.')
data['message'] = (
u'Podés ver <a href="/attendees">quiénes van'
' a asistir</a> o compartirlo en:')
data['share_twitter'] = get_twitter_message(MESSAGE_REGISTER)
data['share_facebook'] = FACEBOOK_MESSAGE
data.update(result)
path = os.path.join(os.path.dirname(__file__),
"templates/user/success.html")
self.response.out.write(template.render(path, data))
else:
self.show_error("templates/user/register.html",
u'Hubo un problema al intentar procesar la inscripción.',
result)
# show error
else:
#show error page
self.show_error("templates/user/register.html",
u'No hay una sesión iniciada.', result)
class ModifyTalk(PyDayHandler):
def get(self):
result = self.user_login()
key = cgi.escape(self.request.get('talk'))
talk = db.get_talk(result['user'], key)
if result.get('user', None) and talk:
result['form'] = forms.TalkForm(instance=talk)
path = os.path.join(os.path.dirname(__file__),
"templates/others/propose.html")
self.response.out.write(template.render(path, result))
else:
self.redirect(result['login'])
def post(self):
result = self.user_login()
# Collect data
key = cgi.escape(self.request.get('talk'))
values = forms.TalkForm(data=self.request.POST)
result['form'] = values
if result.get('user', None):
if not values.is_valid():
#error page
self.show_error("templates/others/propose.html",
u'Falta completar alguno de los datos requeridos.', result)
return
talk = values.save(commit=False)
user = result['user']
if isinstance(user, users.User):
talk.userId = user
else:
talk.profile = user
saved = db.update_talk(key, talk)
if saved:
data = {}
data['title'] = u'Tu propuesta fue cargada con éxito.'
data['message'] = u'Podés compartirlo en:'
data['share_twitter'] = get_twitter_message(
MESSAGE_PROPOSE % talk.title)
data['share_facebook'] = FACEBOOK_MESSAGE
data.update(result)
path = os.path.join(os.path.dirname(__file__),
"templates/user/success.html")
self.response.out.write(template.render(path, data))
else:
#show error page
self.show_error("templates/others/propose.html",
u'Hubo un problema al intentar registrar la charla.', result)
class Prospectus(PyDayHandler):
def get(self):
result = self.user_login()
result['call_sponsors_ended'] = datetime.datetime(2012, 9, 3) < NOW
path = os.path.join(os.path.dirname(__file__),
"templates/others/prospectus.html")
self.response.out.write(template.render(path, result))
class Tshirt(PyDayHandler):
def get(self):
result = self.user_login()
if result['tshirt_req_ended']:
path = os.path.join(os.path.dirname(__file__),
"templates/error_base.html")
result['title'] = 'Error'
result['message'] = 'Ya no se reciben solicitudes de remeras.'
self.response.out.write(template.render(path, result))
else:
registered = db.user_is_attendee(result.get('user', None))
if not registered:
self.redirect('/register')
return
if result.get('user', None):
form = forms.TshirtForm()
result['form'] = form
path = os.path.join(os.path.dirname(__file__),
"templates/user/tshirt.html")
self.response.out.write(template.render(path, result))
else:
self.redirect(result['login'])
def post(self):
result = self.user_login()
# Collect data
values = forms.TshirtForm(data=self.request.POST)
result['form'] = values
if result.get('user', None):
if not values.is_valid():
#error page
self.show_error("templates/user/tshirt.html",
u'Falta completar alguno de los datos requeridos.', result)
return
tshirt = values.save(commit=False)
user = result['user']
if isinstance(user, users.User):
tshirt.userId = user
else:
tshirt.profile = user
saved = db.request_tshirt(tshirt)
if saved:
self.redirect('/profile')
else:
#show error page
self.show_error("templates/user/tshirt.html",
u'Hubo un problema al intentar registrar la charla.', result)
else:
#show error page
self.show_error("templates/user/tshirt.html",
u'Hubo un problema al intentar registrar la charla.', result)
def main():
application = webapp.WSGIApplication([
('/', MainPage),
('/register', Register),
('/about', About),
('/profile', Profile),
('/attendees', Attendees),
('/venue', Venue),
('/propose', Propose),
('/login', Login),
('/modify_profile', ModifyProfile),
('/modify_talk', ModifyTalk),
('/prospectus', Prospectus),
('/tshirt', Tshirt),
('/schedule', Schedule),
('/.*', NotFoundPageHandler),
], debug=True)
run_wsgi_app(application)
if __name__ == "__main__":
main()