Skip to content
Newer
Older
100755 199 lines (175 sloc) 7.38 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
142 class PendingHandler(webapp.RequestHandler):
143 def get(self):
144 user = users.get_current_user()
145 if user:
146 logout_url = users.create_logout_url('/')
147 else:
148 login_url = users.create_login_url('/')
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
149 events = Event.all().filter('status IN', ['pending', 'understaffed', 'onhold', 'expired']).order('start_time')
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
150 today = datetime.today()
151 tomorrow = today + timedelta(days=1)
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
152 is_admin = username(user) in dojo('/groups/events')
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
153 self.response.out.write(template.render('templates/pending.html', locals()))
154
155 class NewHandler(webapp.RequestHandler):
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
156 @util.login_required
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
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 rooms = ROOM_OPTIONS
164 self.response.out.write(template.render('templates/new.html', locals()))
165
166 def post(self):
167 user = users.get_current_user()
168 start_time = datetime.strptime("%s %s:%s %s" % (
169 self.request.get('start_date'),
170 self.request.get('start_time_hour'),
171 self.request.get('start_time_minute'),
172 self.request.get('start_time_ampm')), "%d/%m/%Y %I:%M %p")
173 event = Event(
174 name = self.request.get('name'),
175 start_time = start_time,
176 type = self.request.get('type'),
177 estimated_size = self.request.get('estimated_size'),
178 contact_name = self.request.get('contact_name'),
179 contact_phone = self.request.get('contact_phone'),
180 details = self.request.get('details'),
181 url = self.request.get('url'),
182 fee = self.request.get('fee'),
183 notes = self.request.get('notes'),
184 rooms = self.request.get_all('rooms'))
185 event.put()
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
186 self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
187
188 def main():
189 application = webapp.WSGIApplication([
190 ('/', ApprovedHandler),
fcf7602 @progrium working approval process and ical view of approved events
progrium authored
191 ('/events\.(.+)', EventsHandler),
9b632dd @progrium initial commit -- basic form, rough listing
progrium authored
192 ('/pending', PendingHandler),
193 ('/new', NewHandler),
194 ('/event/(\d+).*', EventHandler), ],debug=True)
195 util.run_wsgi_app(application)
196
197 if __name__ == '__main__':
198 main()
Something went wrong with that request. Please try again.