-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.py
executable file
·239 lines (219 loc) · 9.72 KB
/
main.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
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util, template
from google.appengine.api import urlfetch, memcache, users, mail
from django.utils import simplejson
from django.template.defaultfilters import slugify
from icalendar import Calendar
import logging, urllib
from datetime import datetime, timedelta, time, date
from pytz import timezone
import pytz
from models import Event, Feedback, ROOM_OPTIONS, GUESTS_PER_STAFF, PENDING_LIFETIME
from utils import dojo, username, human_username, set_cookie
from notices import *
class ExpireCron(webapp.RequestHandler):
def post(self):
# Expire events marked to expire today
today = datetime.combine(datetime.today(), time())
events = Event.all() \
.filter('status IN', ['pending', 'understaffed']) \
.filter('expired >=', today) \
.filter('expired <', today + timedelta(days=1))
for event in events:
event.expire()
notify_owner_expired(event)
class ExpireReminderCron(webapp.RequestHandler):
def post(self):
# Find events expiring in 10 days to warn owner
ten_days = datetime.combine(datetime.today(), time()) + timedelta(days=10)
events = Event.all() \
.filter('status IN', ['pending', 'understaffed']) \
.filter('expired >=', ten_days) \
.filter('expired <', ten_days + timedelta(days=1))
for event in events:
notify_owner_expiring(event)
class EventsHandler(webapp.RequestHandler):
def get(self, format):
if format == 'ics':
events = Event.all().filter('status IN', ['approved', 'canceled']).order('start_time')
cal = Calendar()
for event in events:
cal.add_component(event.to_ical())
self.response.headers['content-type'] = 'text/calendar'
self.response.out.write(cal.as_string())
class EventHandler(webapp.RequestHandler):
def get(self, id):
event = Event.get_by_id(int(id))
user = users.get_current_user()
if user:
is_admin = username(user) in dojo('/groups/events')
is_staff = username(user) in dojo('/groups/staff')
can_approve = (event.status in ['pending'] and is_admin)
can_staff = (event.status in ['pending', 'understaffed', 'approved'] and is_staff and not user in event.staff)
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
self.response.out.write(template.render('templates/event.html', locals()))
def post(self, id):
event = Event.get_by_id(int(id))
user = users.get_current_user()
is_admin = username(user) in dojo('/groups/events')
is_staff = username(user) in dojo('/groups/staff')
state = self.request.get('state')
if state:
if state.lower() == 'approve' and is_admin:
event.approve()
if state.lower() == 'staff' and is_staff:
event.add_staff(user)
if state.lower() == 'cancel' and is_admin:
event.cancel()
if state.lower() == 'expire' and is_admin:
event.expire()
if event.status == 'approved':
notify_owner_approved(event)
self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
class ApprovedHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
today = datetime.today()
events = Event.get_approved_list()
tomorrow = today + timedelta(days=1)
self.response.out.write(template.render('templates/approved.html', locals()))
class MyEventsHandler(webapp.RequestHandler):
@util.login_required
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
events = Event.all().filter('member = ', user).order('start_time')
today = datetime.today()
tomorrow = today + timedelta(days=1)
is_admin = username(user) in dojo('/groups/events')
self.response.out.write(template.render('templates/myevents.html', locals()))
class PastHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
today = datetime.today()
events = Event.all().filter('start_time < ', today).order('-start_time')
is_admin = username(user) in dojo('/groups/events')
self.response.out.write(template.render('templates/past.html', locals()))
class PendingHandler(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
events = Event.get_pending_list()
today = datetime.today()
tomorrow = today + timedelta(days=1)
is_admin = username(user) in dojo('/groups/events')
self.response.out.write(template.render('templates/pending.html', locals()))
class NewHandler(webapp.RequestHandler):
@util.login_required
def get(self):
user = users.get_current_user()
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
rooms = ROOM_OPTIONS
self.response.out.write(template.render('templates/new.html', locals()))
def post(self):
user = users.get_current_user()
try:
start_time = datetime.strptime("%s %s:%s %s" % (
self.request.get('date'),
self.request.get('start_time_hour'),
self.request.get('start_time_minute'),
self.request.get('start_time_ampm')), "%d/%m/%Y %I:%M %p")
end_time = datetime.strptime("%s %s:%s %s" % (
self.request.get('date'),
self.request.get('end_time_hour'),
self.request.get('end_time_minute'),
self.request.get('end_time_ampm')), "%d/%m/%Y %I:%M %p")
if (end_time-start_time).days < 0:
raise ValueError("End time must be after start time")
else:
event = Event(
name = self.request.get('name'),
start_time = start_time,
end_time = end_time,
type = self.request.get('type'),
estimated_size = self.request.get('estimated_size'),
contact_name = self.request.get('contact_name'),
contact_phone = self.request.get('contact_phone'),
details = self.request.get('details'),
url = self.request.get('url'),
fee = self.request.get('fee'),
notes = self.request.get('notes'),
rooms = self.request.get_all('rooms'),
expired = datetime.today() + timedelta(days=PENDING_LIFETIME), # Set expected expiration date
)
event.put()
notify_owner_confirmation(event)
if not event.is_staffed():
notify_staff_needed(event)
notify_new_event(event)
set_cookie(self.response.headers, 'formvalues', None)
self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
except Exception, e:
message = str(e)
if 'match format' in message:
message = "Date is required."
if message.startswith('Property'):
message = message[9:].replace('_', ' ').capitalize()
set_cookie(self.response.headers, 'formerror', message)
set_cookie(self.response.headers, 'formvalues', dict(self.request.POST))
self.redirect('/new')
class FeedbackHandler(webapp.RequestHandler):
@util.login_required
def get(self, event_id):
user = users.get_current_user()
event = Event.get_by_id(int(event_id))
if user:
logout_url = users.create_logout_url('/')
else:
login_url = users.create_login_url('/')
self.response.out.write(template.render('templates/feedback.html', locals()))
def post(self, event_id):
user = users.get_current_user()
event = Event.get_by_id(int(event_id))
try:
if self.request.get('rating'):
feedback = Feedback(
event = event,
rating = int(self.request.get('rating')),
comment = self.request.get('comment'))
feedback.put()
self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
else:
raise ValueError("Please select a rating")
except Exception:
set_cookie(self.response.headers, 'formvalues', dict(self.request.POST))
self.redirect('/feedback/new/'+event_id)
def main():
application = webapp.WSGIApplication([
('/', ApprovedHandler),
('/events\.(.+)', EventsHandler),
('/past', PastHandler),
('/pending', PendingHandler),
('/myevents', MyEventsHandler),
('/new', NewHandler),
('/event/(\d+).*', EventHandler),
('/expire', ExpireCron),
('/expiring', ExpireReminderCron),
('/feedback/new/(\d+).*', FeedbackHandler) ],debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()