import cgi
from datetime import datetime
import os
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import markdown2 # from http://code.google.com/p/python-markdown2/
from model import *
class ListEvents(webapp.RequestHandler):
def get(self):
configuration = Configuration.all().get() or Configuration()
active_event = configuration.active_event
events = Event.all().order('-date');
template_values = {
'events': events,
'active_event': active_event,
}
path = os.path.join(os.path.dirname(__file__), 'admin_list_events.html')
self.response.out.write(template.render(path, template_values))
class PreviewEvent(webapp.RequestHandler):
def get(self):
event = db.get(self.request.get('key'))
template_values = {
'preview': True,
'title': event.title,
'text': markdown2.markdown(event.text),
'attendees': event.attendees.order('signup_date'),
}
path = os.path.join(os.path.dirname(__file__), 'main.html')
self.response.out.write(template.render(path, template_values))
class DownloadEventCSV(webapp.RequestHandler):
def get(self):
event = db.get(self.request.get('key'))
mimetype = 'text/csv; charset=utf-8; header=present'
disposition = 'attachment; filename=osjam-%s.csv' % event.date.isoformat()
self.response.headers['Content-Type'] = mimetype
self.response.headers['Content-Disposition'] = disposition
self.response.out.write('name,email\n')
for attendee in event.attendees.order('signup_date'):
self.response.out.write("\"%s\",\"%s\"\n" % (
attendee.name.replace('"', '""'),
attendee.email.replace('"', '""')))
class EditEvent(webapp.RequestHandler):
DEFAULT_EVENT_TEXT_ = """
# Google London Open Source Jam
## When
**Thursday 21st February 2008. 6pm - 9.30pm**.
## Where
At the **London Google Engineering Office**
Belgrave House, 76 Buckingham Palace Road, London, SW1W 9TQ. [Map][]
[Map]: http://maps.google.co.uk/maps?q=sw1w9tq&z=16&ll=51.495492,-0.14647&spn=0.004823,0.017166
## Topic
This time, our topic of interest is **How much is the doggie in the window**.
* Valuing dogs, a value proposition?
* Accessibility.
## Hosts
Joe Walnes, Neil Dunn, Malcolm Rowe
## How to Get Here
Take the tube (Victoria, District and Circle lines)
or bus (multiple routes, including the 38 and 73
from the West End) to Victoria Station, and we're 3
minutes' walk from the station.
## What to Do When You Get Here
When you arrive, in the main ground floor
reception, tell the receptionist that you're
visiting Google. You register there, then take the
lift to the 5th floor, where you can sign in to the
Google reception; when you arrive up there, please
ask for the Open Source Jam event.
"""
def get(self):
configuration = Configuration.all().get() or Configuration()
active_event = configuration.active_event
key = self.request.get('key')
if key:
event = db.get(key)
page_title = 'Edit Event'
submit_button_label = 'Save'
event_is_active = active_event and active_event.key() == event.key()
else:
event = Event(date=datetime.today().date(),
text=self.DEFAULT_EVENT_TEXT_,
allow_signup=True)
page_title = 'New Event'
submit_button_label = 'Create Event'
event_is_active = False
template_values = {
'event': event,
'event_is_active': event_is_active,
'page_title': page_title,
'submit_button_label': submit_button_label,
}
path = os.path.join(os.path.dirname(__file__), 'admin_edit_event.html')
self.response.out.write(template.render(path, template_values))
def post(self):
configuration = Configuration.all().get() or Configuration()
key = self.request.get('key')
if key:
event = db.get(key)
else:
event = Event(date=datetime.today().date())
event.date = datetime.strptime(self.request.get('date'), '%Y-%m-%d').date()
event.title = self.request.get('title')
event.text = self.request.get('text')
event.max_places = int(self.request.get('max_places'))
event.allow_signup = bool(self.request.get('allow_signup'))
event.put()
if self.request.get('activate'):
configuration.active_event = event
configuration.put()
self.redirect('/admin')
# Inteded for use when bulk loading. Not linked anywhere.
class ClearEvent(webapp.RequestHandler):
def get(self):
event = db.get(self.request.get('key'))
if not self.request.get('ireallymeanit'):
self.response.out.write(
'Retry with &ireallymeanit=1 if you really want to clear attendees')
return
for attendee in event.attendees:
attendee.delete()
self.redirect('/admin')
def main():
application = webapp.WSGIApplication(
[('/admin', ListEvents),
('/admin/', ListEvents),
('/admin/event', EditEvent),
('/admin/preview', PreviewEvent),
('/admin/csv', DownloadEventCSV),
('/admin/clearattendees', ClearEvent)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()