Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cleaning things up and adding json representations for jon

  • Loading branch information...
commit 2e08a66cebc40d0ba3c4e6b6936d367788634a89 1 parent 6ea876a
Jeff Lindsay progrium authored
Showing with 66 additions and 41 deletions.
  1. +22 −13 main.py
  2. +43 −28 models.py
  3. +1 −0  utils.py
35 main.py
View
@@ -39,30 +39,38 @@ def post(self):
class EventsHandler(webapp.RequestHandler):
def get(self, format):
+ events = Event.all().filter('status IN', ['approved', 'canceled']).order('start_time')
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())
+ elif format == 'json':
+ self.response.headers['content-type'] = 'application/json'
+ events = map(lambda x: x.to_dict(summarize=True), events)
+ self.response.out.write(simplejson.dumps(events))
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 and not user == event.member)
- can_staff = (event.status in ['pending', 'understaffed', 'approved'] and is_staff and not user in event.staff)
- can_unstaff = (not event.status in ['canceled', 'deleted'] and is_staff and user in event.staff)
- logout_url = users.create_logout_url('/')
+ if self.request.path.endswith('json'):
+ self.response.headers['content-type'] = 'application/json'
+ self.response.out.write(simplejson.dumps(event.to_dict()))
else:
- login_url = users.create_login_url('/')
- event.details = db.Text(event.details.replace("\n","<br/>"))
- event.notes = db.Text(event.notes.replace("\n","<br/>"))
- self.response.out.write(template.render('templates/event.html', locals()))
+ 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 and not user == event.member)
+ can_staff = (event.status in ['pending', 'understaffed', 'approved'] and is_staff and not user in event.staff)
+ can_unstaff = (not event.status in ['canceled', 'deleted'] and is_staff and user in event.staff)
+ logout_url = users.create_logout_url('/')
+ else:
+ login_url = users.create_login_url('/')
+ event.details = db.Text(event.details.replace("\n","<br/>"))
+ event.notes = db.Text(event.notes.replace("\n","<br/>"))
+ self.response.out.write(template.render('templates/event.html', locals()))
def post(self, id):
event = Event.get_by_id(int(id))
@@ -248,6 +256,7 @@ def main():
('/myevents', MyEventsHandler),
('/new', NewHandler),
('/event/(\d+).*', EventHandler),
+ ('/event/(\d+)\.json', EventHandler),
('/expire', ExpireCron),
('/expiring', ExpireReminderCron),
('/feedback/new/(\d+).*', FeedbackHandler) ],debug=True)
71 models.py
View
@@ -4,6 +4,7 @@
from icalendar import Calendar, Event as CalendarEvent
from utils import human_username, local_today, to_sentence_list
import logging
+import pytz
ROOM_OPTIONS = (
('Cave', 15),
@@ -19,24 +20,24 @@
PENDING_LIFETIME = 30 # days
class Event(db.Model):
- status = db.StringProperty(required=True, default='pending', choices=set(
- ['pending', 'understaffed', 'approved', 'canceled', 'onhold', 'expired', 'deleted']))
- member = db.UserProperty(auto_current_user_add=True)
- name = db.StringProperty(required=True)
- start_time = db.DateTimeProperty(required=True)
- end_time = db.DateTimeProperty()
- staff = db.ListProperty(users.User)
- rooms = db.StringListProperty() #choices=set(ROOM_OPTIONS)
-
- details = db.TextProperty()
- url = db.StringProperty()
- fee = db.StringProperty()
- notes = db.TextProperty()
- type = db.StringProperty(required=True)
+ status = db.StringProperty(required=True, default='pending', choices=set(
+ ['pending', 'understaffed', 'approved', 'canceled', 'onhold', 'expired', 'deleted']))
+ member = db.UserProperty(auto_current_user_add=True)
+ name = db.StringProperty(required=True)
+ start_time = db.DateTimeProperty(required=True)
+ end_time = db.DateTimeProperty()
+ staff = db.ListProperty(users.User)
+ rooms = db.StringListProperty() #choices=set(ROOM_OPTIONS)
+
+ details = db.TextProperty()
+ url = db.StringProperty()
+ fee = db.StringProperty()
+ notes = db.TextProperty()
+ type = db.StringProperty(required=True)
estimated_size = db.StringProperty(required=True)
- contact_name = db.StringProperty(required=True)
- contact_phone = db.StringProperty(required=True)
+ contact_name = db.StringProperty(required=True)
+ contact_phone = db.StringProperty(required=True)
expired = db.DateTimeProperty()
created = db.DateTimeProperty(auto_now_add=True)
@@ -73,13 +74,8 @@ def staff_needed(self):
return 2
def is_approved(self):
- '''Has the events team approved the event? Note: This does not
- necessarily imply that the event is in state 'approved'.'''
- return self.status in ('understaffed', 'approved', 'canceled')
-
- def is_approved(self):
- '''Has the events team approved the event? Note: This does not
- necessarily imply that the event is in state 'approved'.'''
+ """Has the events team approved the event? Note: This does not
+ necessarily imply that the event is in state 'approved'."""
return self.status in ('understaffed', 'approved', 'cancelled')
def is_canceled(self):
@@ -159,13 +155,32 @@ def remove_staff(self, user):
def to_ical(self):
event = CalendarEvent()
event.add('summary', self.name if self.status == 'approved' else self.name + ' (%s)' % self.status.upper())
- event.add('dtstart', self.start_time.replace(tzinfo=timezone('US/Pacific')))
- event.add('dtend', self.end_time.replace(tzinfo=timezone('US/Pacific')))
+ event.add('dtstart', self.start_time.replace(tzinfo=pytz.timezone('US/Pacific')))
+ event.add('dtend', self.end_time.replace(tzinfo=pytz.timezone('US/Pacific')))
return event
+ def to_dict(self, summarize=False):
+ d = dict()
+ if summarize:
+ props = ['member', 'start_time', 'name', 'type', 'estimated_size']
+ else:
+ props = Event.properties().keys()
+ for prop in props:
+ if prop == 'member':
+ d[prop] = getattr(self, prop).email()
+ elif prop == 'staff':
+ d[prop] = map(lambda x: x.email(), getattr(self, prop))
+ elif prop in ['start_time', 'end_time', 'created', 'expired', 'updated']:
+ if getattr(self, prop):
+ d[prop] = getattr(self, prop).replace(tzinfo=pytz.timezone('US/Pacific')).strftime('%Y-%m-%dT%H:%M:%S')
+ else:
+ d[prop] = getattr(self, prop)
+ d['id'] = self.key().id()
+ return d
+
class Feedback(db.Model):
- user = db.UserProperty(auto_current_user_add=True)
- event = db.ReferenceProperty(Event)
- rating = db.IntegerProperty()
+ user = db.UserProperty(auto_current_user_add=True)
+ event = db.ReferenceProperty(Event)
+ rating = db.IntegerProperty()
comment = db.StringProperty(multiline=True)
created = db.DateTimeProperty(auto_now_add=True)
1  utils.py
View
@@ -47,6 +47,7 @@ def human_username(user):
def set_cookie(headers, name, value):
headers.add_header('Set-Cookie', '%s=%s;' % (name, simplejson.dumps(value)))
+
def local_today():
'''Return a datetime object representing the start of today, local time.'''
utc_now = pytz.utc.localize(datetime.utcnow())
Please sign in to comment.
Something went wrong with that request. Please try again.