Skip to content
Browse files

Let edit events and refactor error handling

  • Loading branch information...
1 parent a244c65 commit ca8c7f2dbfe517b27254216cfc6fdc7873c3d969 @dustball dustball committed Sep 20, 2010
Showing with 102 additions and 7 deletions.
  1. +54 −3 main.py
  2. +23 −0 templates/edit.html
  3. +16 −0 templates/error.html
  4. +3 −0 templates/event.html
  5. +6 −4 templates/new.html
View
57 main.py
@@ -77,6 +77,49 @@ def get(self, format):
self.response.out.write(simplejson.dumps(events))
+class EditHandler(webapp.RequestHandler):
+ def get(self, id):
+ event = Event.get_by_id(int(id))
+ user = users.get_current_user()
+ access_rights = UserRights(user, event)
+ if access_rights.can_edit:
+ logout_url = users.create_logout_url('/')
+ rooms = '\n'.join(event.rooms)
+ self.response.out.write(template.render('templates/edit.html', locals()))
+ else:
+ self.response.out.write("Access denied")
+
+ def post(self, id):
+ event = Event.get_by_id(int(id))
+ user = users.get_current_user()
+ access_rights = UserRights(user, event)
+ if access_rights.can_edit:
+ try:
+ if not self.request.get('estimated_size').isdigit():
+ raise ValueError('Estimated number of people must be a number')
+ if not int(self.request.get('estimated_size')) > 0:
+ raise ValueError('Estimated number of people must be greater then zero')
+ if ( self.request.get( 'contact_phone' ) and not is_phone_valid( self.request.get( 'contact_phone' ) ) ):
+ raise ValueError( 'Phone number does not appear to be valid' )
+ else:
+ event.name = self.request.get('name')
+ event.estimated_size = cgi.escape(self.request.get('estimated_size'))
+ event.contact_name = cgi.escape(self.request.get('contact_name'))
+ event.contact_phone = cgi.escape(self.request.get('contact_phone'))
+ event.details = cgi.escape(self.request.get('details'))
+ event.url = cgi.escape(self.request.get('url'))
+ event.fee = cgi.escape(self.request.get('fee'))
+ event.notes = cgi.escape(self.request.get('notes'))
+ event.rooms = self.request.get('rooms').strip().split("\n")
+ event.put()
+ self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
+ except Exception, e:
+ error = str(e)
+ self.response.out.write(template.render('templates/error.html', locals()))
+ else:
+ self.response.out.write("Access denied")
+
+
class EventHandler(webapp.RequestHandler):
def get(self, id):
event = Event.get_by_id(int(id))
@@ -244,9 +287,16 @@ def post(self):
message = 'Date is required.'
if message.startswith('Property'):
message = message[9:].replace('_', ' ').capitalize()
- set_cookie(self.response.headers, 'formerror', message)
- set_cookie(self.response.headers, 'formvalues', dict(self.request.POST))
- self.redirect('/new')
+ # This is NOT a reliable way to handle erorrs
+ #set_cookie(self.response.headers, 'formerror', message)
+ #set_cookie(self.response.headers, 'formvalues', dict(self.request.POST))
+ #self.redirect('/new')
+ error = message
+ self.response.out.write(template.render('templates/error.html', locals()))
+
+
+
+
class FeedbackHandler(webapp.RequestHandler):
@@ -286,6 +336,7 @@ def main():
('/cronbugowners', CronBugOwnersHandler),
('/myevents', MyEventsHandler),
('/new', NewHandler),
+ ('/edit/(\d+).*', EditHandler),
('/event/(\d+).*', EventHandler),
('/event/(\d+)\.json', EventHandler),
('/expire', ExpireCron),
View
23 templates/edit.html
@@ -0,0 +1,23 @@
+{% extends 'base.html' %}
+{% block content %}
+<style>
+.b-block input, .b-block textarea {width:75%}
+</style>
+
+<form method=POST>
+<div id="primary">
+ <h3>Edit: {{event.name|title}}</h3>
+ <div class='b-block'><div class='b-label'>Title:</div><div class='b-data'><input type="text" name="name" value="{{event.name|title}}"/></div></div>
+ <div class='b-block'><div class='b-label'>Rooms:</div><div class='b-data'><textarea rows=3 name="rooms">{{rooms}}</textarea></div></div>
+ <div class='b-block'><div class='b-label'>Details:</div><div class='b-data'><textarea rows=4 name="details">{{event.details}}</textarea></div></div>
+ <div class='b-block'><div class='b-label'>Notes:</div><div class='b-data'><textarea rows=3 name="notes">{{event.notes}}</textarea></div></div>
+ <div class='b-block'><div class='b-label'>Estimated Size:</div><div class='b-data'><input type="text" name="estimated_size" value="{{event.estimated_size|title}}"/></div></div>
+ <div class='b-block'><div class='b-label'>Contact Name:</div><div class='b-data'><input type="text" name="contact_name" value="{{event.contact_name|title}}"/></div></div>
+ <div class='b-block'><div class='b-label'>Contact Phone:</div><div class='b-data'><input type="text" name="contact_phone" value="{{event.contact_phone|title}}"/></div></div>
+ <div class='b-block'><div class='b-label'>URL:</div><div class='b-data'><input type="text" name="url" value="{{event.url|title}}"/></div></div>
+ <div class='b-block'><div class='b-label'>Fee:</div><div class='b-data'><input type="text" name="fee" value="{{event.fee|title}}"/></div></div>
+ <br/>
+ <input type=submit value="Save">
+</div>
+
+{% endblock %}
View
16 templates/error.html
@@ -0,0 +1,16 @@
+{% extends 'base.html' %}
+{% block content %}
+
+<h1>Error</h1>
+
+<div style="margin:4px; margin-bottom:1.5em; padding:8px; color: black; border:1px solid red; background:#fee;" id="error">
+{{error}}
+</div>
+
+<p><input type=button value="Back" onclick="javascript:history.back(1);"> </p>
+
+{% endblock %}
+
+
+
+
View
3 templates/event.html
@@ -15,6 +15,7 @@
{% if access_rights.can_approve %}<input type="submit" name="state" value="Approve" />{% endif %}
{% if access_rights.can_cancel and not event.is_canceled %}<input type="submit" name="state" value="Cancel" />{% endif %}
{% if access_rights.is_admin %}<input type="submit" name="state" value="Delete" />{% endif %}
+ {% if access_rights.can_edit %}<input type="button" value="Edit" onclick="document.location.href='/edit/{{event.key.id}}';" />{% endif %}
<!--{% if access_rights.can_staff %}<input type="submit" name="state" value="Staff" />{% endif %}
{% if access_rights.can_unstaff %}<input type="submit" name="state" value="Unstaff" />{% endif %}-->
{% endif %}
@@ -35,7 +36,9 @@
<div class='b-block'><div class='b-label'>Member:</div><div class='b-data'>{{event.member}}</div></div>
<div class='b-block'><div class='b-label'>Type:</div><div class='b-data'>{{event.type}}</div></div>
<div class='b-block'><div class='b-label'>Estimated size:</div><div class='b-data'>{{event.estimated_size}}</div></div>
+ {% if event.contact_name %}
<div class='b-block'><div class='b-label'>Contact:</div><div class='b-data'>{{event.contact_name}}{% if user %}, {{event.contact_phone}}{% endif %}</div></div>
+ {% endif %}
<div class='b-block'><div class='b-label'>URL:</div><div class='b-data'><a href='{{event.url}}'>{{event.url}}</a></div></div>
<div class='b-block'><div class='b-label'>Fee:</div><div class='b-data'>{{event.fee}}</div></div>
<div class='b-block'><div class='b-label'>Rooms:</div><div class='b-data'>{{event.roomlist}}</div></div>
View
10 templates/new.html
@@ -24,7 +24,8 @@
var formerror = $.cookie('formerror');
if (formerror) {
formerror = JSON.parse(formerror);
- $('#error').append(formerror);
+ $('#warn').hide();
+ $('#error').show("slow").append("<b>Error:</b> "+ formerror);
$.cookie('formerror', null);
}
} catch (err) {
@@ -49,8 +50,9 @@
<div id="primary">
<h3>New Event</h3>
- <div style="margin:4px; margin-bottom:1.5em; padding:8px; border:1px solid #ffe7af; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; background:#fffaed"><b>{{human}}</b> will be held personally responsible for this event. You must show up and ensure the rules below are followed.</div>
-
+ <div id="warn" style="margin:4px; margin-bottom:1.5em; padding:8px; border:1px solid #ffe7af; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; background:#fffaed"><b>{{human}}</b> will be held personally responsible for this event. You must show up and ensure the rules below are followed.</div>
+ <div style="margin:4px; margin-bottom:1.5em; padding:8px; color: black; display:none; border:1px solid red; background:#fee; " id="error"></div>
+
<div style="width: 46%; float: right; font-size: smaller; padding: 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px; background: #eee; line-height: normal;" id="terms">
<h3 style="margin-top: 0px;">Member Terms as Event Host:</h3>
<ol>
@@ -76,7 +78,7 @@ <h3 style="margin-top: 0px;">Member Terms as Event Host:</h3>
<div>You are held responsible for damages to Dojo facilities and equipment during your event.</div></li>
</ol>
</div>
- <span style="color: red; font-size: smaller;" id="error"></span>
+
<form method="post" style="width: 49%;">
<h4>Event Name</h4>
<input type="text" name="name" style="width: 300px; margin-left: 4px;" />

0 comments on commit ca8c7f2

Please sign in to comment.
Something went wrong with that request. Please try again.