Skip to content
Newer
Older
100755 227 lines (201 sloc) 8.5 KB
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
1 from google.appengine.ext import webapp, db
2 from google.appengine.ext.webapp import util, template
3 from google.appengine.api import urlfetch, memcache, users
4 from django.utils import simplejson
5 from datetime import datetime, timedelta
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
6 from django.template.defaultfilters import slugify
7 import time
8 from datetime import datetime
9 import logging, urllib
10 from icalendar import Calendar, Event as CalendarEvent, UTC # timezone
11
12 # Hacker Dojo Domain API helper with caching
13 def dojo(path):
14 base_url = 'http://hackerdojo-domain.appspot.com'
15 cache_ttl = 3600
16 resp = memcache.get(path)
17 if not resp:
18 resp = urlfetch.fetch(base_url + path)
19 if 'Refreshing' in resp.content:
20 time.sleep(2)
21 return urlfetch.fetch(base_url + path)
22 try:
23 resp = simplejson.loads(resp.content)
24 except Exception, e:
25 resp = []
26 cache_ttl = 10
27 memcache.set(path, resp, cache_ttl)
28 return resp
29
30 def username(user):
31 return user.nickname().split('@')[0] if user else None
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
32
33 ROOM_OPTIONS = ['cave', 'hall', 'savanna', 'sunroom', 'greenroom', 'frontarea', '140b']
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
34 GUESTS_PER_STAFF = 25
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
35
36 class Event(db.Model):
37 status = db.StringProperty(required=True, default='pending', choices=set(
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
38 ['pending', 'understaffed', 'approved', 'canceled', 'onhold', 'expired']))
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
39 member = db.UserProperty(auto_current_user_add=True)
40 name = db.StringProperty(required=True)
41 start_time = db.DateTimeProperty(required=True)
42 end_time = db.DateTimeProperty()
43 staff = db.ListProperty(users.User)
44 rooms = db.StringListProperty() #choices=set(ROOM_OPTIONS)
45
46 details = db.StringProperty()
47 url = db.StringProperty()
48 fee = db.StringProperty()
49 notes = db.StringProperty()
50 type = db.StringProperty(required=True)
51 estimated_size = db.StringProperty(required=True)
52
53 contact_name = db.StringProperty(required=True)
54 contact_phone = db.StringProperty(required=True)
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
55
56 created = db.DateTimeProperty(auto_now_add=True)
57 updated = db.DateTimeProperty(auto_now=True)
58
59 def is_staffed(self):
60 return len(self.staff) >= int(self.estimated_size) / GUESTS_PER_STAFF
61
62 def is_canceled(self):
63 return self.status == 'canceled'
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
64
65 def start_date(self):
66 return self.start_time.date()
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
67
68 def approve(self):
69 if self.is_staffed():
70 self.status = 'approved'
71 else:
72 self.status = 'understaffed'
73 self.put()
74
75 def cancel(self):
76 self.status = 'canceled'
77 self.put()
78
79 def add_staff(self, user):
80 self.staff.append(user)
81 if self.is_staffed() and self.status == 'understaffed':
82 self.status = 'approved'
83 self.put()
84
85 def to_ical(self):
86 event = CalendarEvent()
87 event.add('summary', self.name if self.status == 'approved' else self.name + ' (%s)' % self.status.upper())
88 event.add('dtstart', self.start_time)
89 return event
90
91 class EventsHandler(webapp.RequestHandler):
92 def get(self, format):
93 if format == 'ics':
94 events = Event.all().filter('status IN', ['approved', 'canceled']).order('start_time')
95 cal = Calendar()
96 for event in events:
97 cal.add_component(event.to_ical())
98 self.response.headers['content-type'] = 'text/calendar'
99 self.response.out.write(cal.as_string())
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
100
101 class EventHandler(webapp.RequestHandler):
102 def get(self, id):
103 event = Event.get_by_id(int(id))
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
104 user = users.get_current_user()
105 if user:
106 is_admin = username(user) in dojo('/groups/events')
107 is_staff = username(user) in dojo('/groups/staff')
108 can_approve = (event.status in ['pending'] and is_admin)
109 can_staff = (event.status in ['pending', 'understaffed', 'approved'] and is_staff and not user in event.staff)
110 logout_url = users.create_logout_url('/')
111 else:
112 login_url = users.create_login_url('/')
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
113 self.response.out.write(template.render('templates/event.html', locals()))
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
114
115 def post(self, id):
116 event = Event.get_by_id(int(id))
117 user = users.get_current_user()
118 is_admin = username(user) in dojo('/groups/events')
119 is_staff = username(user) in dojo('/groups/staff')
120 state = self.request.get('state')
121 if state:
122 if state.lower() == 'approve' and is_admin:
123 event.approve()
124 if state.lower() == 'staff' and is_staff:
125 event.add_staff(user)
126 if state.lower() == 'cancel' and is_admin:
127 event.cancel()
128 self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
129
130 class ApprovedHandler(webapp.RequestHandler):
131 def get(self):
132 user = users.get_current_user()
133 if user:
134 logout_url = users.create_logout_url('/')
135 else:
136 login_url = users.create_login_url('/')
137 events = Event.all().filter('status IN', ['approved', 'canceled']).order('start_time')
138 today = datetime.today()
139 tomorrow = today + timedelta(days=1)
140 self.response.out.write(template.render('templates/approved.html', locals()))
141
a82b43e @billsaysthis add myevents, pastevents code, refined list display on event page
billsaysthis authored
142 class MyEventsHandler(webapp.RequestHandler):
143 @util.login_required
144 def get(self):
145 user = users.get_current_user()
146 if user:
147 logout_url = users.create_logout_url('/')
148 else:
149 login_url = users.create_login_url('/')
150 events = Event.all().filter('member = ', user).order('start_time')
151 today = datetime.today()
152 tomorrow = today + timedelta(days=1)
153 is_admin = username(user) in dojo('/groups/events')
154 self.response.out.write(template.render('templates/myevents.html', locals()))
155
156 class PastHandler(webapp.RequestHandler):
157 def get(self):
158 user = users.get_current_user()
159 if user:
160 logout_url = users.create_logout_url('/')
161 else:
162 login_url = users.create_login_url('/')
163 today = datetime.today()
164 events = Event.all().filter('start_date < ', today).order('start_time DESC')
165 is_admin = username(user) in dojo('/groups/events')
166 self.response.out.write(template.render('templates/past.html', locals()))
167
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
168 class PendingHandler(webapp.RequestHandler):
169 def get(self):
170 user = users.get_current_user()
171 if user:
172 logout_url = users.create_logout_url('/')
173 else:
174 login_url = users.create_login_url('/')
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
175 events = Event.all().filter('status IN', ['pending', 'understaffed', 'onhold', 'expired']).order('start_time')
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
176 today = datetime.today()
177 tomorrow = today + timedelta(days=1)
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
178 is_admin = username(user) in dojo('/groups/events')
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
179 self.response.out.write(template.render('templates/pending.html', locals()))
180
181 class NewHandler(webapp.RequestHandler):
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
182 @util.login_required
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
183 def get(self):
184 user = users.get_current_user()
185 if user:
186 logout_url = users.create_logout_url('/')
187 else:
188 login_url = users.create_login_url('/')
189 rooms = ROOM_OPTIONS
190 self.response.out.write(template.render('templates/new.html', locals()))
191
192 def post(self):
193 user = users.get_current_user()
194 start_time = datetime.strptime("%s %s:%s %s" % (
195 self.request.get('start_date'),
196 self.request.get('start_time_hour'),
197 self.request.get('start_time_minute'),
198 self.request.get('start_time_ampm')), "%d/%m/%Y %I:%M %p")
199 event = Event(
200 name = self.request.get('name'),
201 start_time = start_time,
202 type = self.request.get('type'),
203 estimated_size = self.request.get('estimated_size'),
204 contact_name = self.request.get('contact_name'),
205 contact_phone = self.request.get('contact_phone'),
206 details = self.request.get('details'),
207 url = self.request.get('url'),
208 fee = self.request.get('fee'),
209 notes = self.request.get('notes'),
210 rooms = self.request.get_all('rooms'))
211 event.put()
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
212 self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
213
214 def main():
215 application = webapp.WSGIApplication([
216 ('/', ApprovedHandler),
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
217 ('/events\.(.+)', EventsHandler),
a82b43e @billsaysthis add myevents, pastevents code, refined list display on event page
billsaysthis authored
218 ('/past\.(.+)', PastHandler),
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
219 ('/pending', PendingHandler),
a82b43e @billsaysthis add myevents, pastevents code, refined list display on event page
billsaysthis authored
220 ('/myevents', MyEventsHandler),
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
221 ('/new', NewHandler),
222 ('/event/(\d+).*', EventHandler), ],debug=True)
223 util.run_wsgi_app(application)
224
225 if __name__ == '__main__':
226 main()
Something went wrong with that request. Please try again.