import cgi import wsgiref.handlers import os import string from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext.webapp import template import markdown2 from model import * class MainPage(webapp.RequestHandler): def get(self): configuration = Configuration.all().get() or Configuration() event = configuration.active_event if not event: self.response.out.write('Oops! No active event!') return attendees = event.attendees.order('signup_date') places_left = max(event.max_places - attendees.count(), 0) user = users.get_current_user() if user: signinout_label = 'Sign out' signinout_url = users.create_logout_url(self.request.uri) default_user_name = user.nickname() # In the future, nickname() will return something sane, but for the # moment, it just returns an email address. Let's convert # 'foo.bar.baz@blah' to 'Foo Bar Baz'. if default_user_name.find('@') != -1: default_user_name = default_user_name.rsplit('@', 2)[0] default_user_name = default_user_name.replace('.', ' ') default_user_name = string.capwords(default_user_name) default_user_email = user.email() else: signinout_label = 'Sign in (for administrators)' signinout_url = users.create_login_url(self.request.uri) default_user_name = '' default_user_email = '' template_values = { 'preview': False, 'title': event.title, 'places_left': places_left, 'allow_signup': event.allow_signup, 'attendees': attendees, 'text': markdown2.markdown(event.text), 'default_user_name': default_user_name, 'default_user_email': default_user_email, 'is_current_user_admin': users.is_current_user_admin(), 'signinout_label': signinout_label, 'signinout_url': signinout_url, } path = os.path.join(os.path.dirname(__file__), 'main.html') self.response.out.write(template.render(path, template_values)) class SignUp(webapp.RequestHandler): def post(self): configuration = Configuration.all().get() or Configuration() event = configuration.active_event # Assumption: user wants to sign up for the active event. places_left = max(event.max_places - event.attendees.count(), 0) if not places_left or not event.allow_signup: self.response.out.write( """Sorry!

Sorry - no places left. Back.

""") return # Yes, there's a race here, between checking whether the event is full # and adding an attendee. Watch me not care. # # Watching? Good. # # (Fixing this properly would probably entail transactionally updating # a places_left property on the Event object while putting a new Attendee. # However, that would also make it possible for the count and list to get # out of sync). name = self.request.get("name") email = self.request.get("email") interests = self.request.get("interests").replace('\n', ' ') publish_name = bool(self.request.get("publish_name")) attendee = Attendee(event=event, name=name, email=email, interests=interests, publish_name=publish_name) attendee.put() self.response.out.write( """Thanks for registering!

KTHXBYE. Continue.

""") def main(): application = webapp.WSGIApplication( [('/', MainPage), ('/signup', SignUp)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()