Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

start of maintaining form state on errors

  • Loading branch information...
commit 77425924e37fa12bf09c770f4f0a119557e6ddf5 1 parent be29c2f
@progrium progrium authored
View
75 main.py
@@ -57,6 +57,9 @@ def notify_owner_expiring(event):
def notify_owner_expired(event):
pass
+def set_cookie(headers, name, value):
+ headers.add_header('Set-Cookie', '%s=%s;' % (name, simplejson.dumps(value)))
+
class Event(db.Model):
status = db.StringProperty(required=True, default='pending', choices=set(
['pending', 'understaffed', 'approved', 'canceled', 'onhold', 'expired']))
@@ -264,40 +267,44 @@ def get(self):
def post(self):
user = users.get_current_user()
- start_time = datetime.strptime("%s %s:%s %s" % (
- self.request.get('date'),
- self.request.get('start_time_hour'),
- self.request.get('start_time_minute'),
- self.request.get('start_time_ampm')), "%d/%m/%Y %I:%M %p")
- end_time = datetime.strptime("%s %s:%s %s" % (
- self.request.get('date'),
- self.request.get('end_time_hour'),
- self.request.get('end_time_minute'),
- self.request.get('end_time_ampm')), "%d/%m/%Y %I:%M %p")
- if (end_time-start_time).days < 0:
- raise ValueError("End time must be after start time")
- else:
- event = Event(
- name = self.request.get('name'),
- start_time = start_time,
- end_time = end_time,
- type = self.request.get('type'),
- estimated_size = self.request.get('estimated_size'),
- contact_name = self.request.get('contact_name'),
- contact_phone = self.request.get('contact_phone'),
- details = self.request.get('details'),
- url = self.request.get('url'),
- fee = self.request.get('fee'),
- notes = self.request.get('notes'),
- rooms = self.request.get_all('rooms'),
- expired = datetime.today() + timedelta(days=PENDING_LIFETIME), # Set expected expiration date
- )
- event.put()
- notify_owner_confirmation(event)
- if not event.is_staffed():
- notify_staff_needed(event)
- notify_new_event(event)
- self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
+ try:
+ start_time = datetime.strptime("%s %s:%s %s" % (
+ self.request.get('date'),
+ self.request.get('start_time_hour'),
+ self.request.get('start_time_minute'),
+ self.request.get('start_time_ampm')), "%d/%m/%Y %I:%M %p")
+ end_time = datetime.strptime("%s %s:%s %s" % (
+ self.request.get('date'),
+ self.request.get('end_time_hour'),
+ self.request.get('end_time_minute'),
+ self.request.get('end_time_ampm')), "%d/%m/%Y %I:%M %p")
+ if (end_time-start_time).days < 0:
+ raise ValueError("End time must be after start time")
+ else:
+ event = Event(
+ name = self.request.get('name'),
+ start_time = start_time,
+ end_time = end_time,
+ type = self.request.get('type'),
+ estimated_size = self.request.get('estimated_size'),
+ contact_name = self.request.get('contact_name'),
+ contact_phone = self.request.get('contact_phone'),
+ details = self.request.get('details'),
+ url = self.request.get('url'),
+ fee = self.request.get('fee'),
+ notes = self.request.get('notes'),
+ rooms = self.request.get_all('rooms'),
+ expired = datetime.today() + timedelta(days=PENDING_LIFETIME), # Set expected expiration date
+ )
+ event.put()
+ notify_owner_confirmation(event)
+ if not event.is_staffed():
+ notify_staff_needed(event)
+ notify_new_event(event)
+ self.redirect('/event/%s-%s' % (event.key().id(), slugify(event.name)))
+ except Exception:
+ set_cookie(self.response.headers, 'formvalues', dict(self.request.POST))
+ self.redirect('/new')
def main():
application = webapp.WSGIApplication([
View
96 static/js/jquery.cookies.js
@@ -0,0 +1,96 @@
+/**
+ * Cookie plugin
+ *
+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
+ * Dual licensed under the MIT and GPL licenses:
+ * http://www.opensource.org/licenses/mit-license.php
+ * http://www.gnu.org/licenses/gpl.html
+ *
+ */
+
+/**
+ * Create a cookie with the given name and value and other optional parameters.
+ *
+ * @example $.cookie('the_cookie', 'the_value');
+ * @desc Set the value of a cookie.
+ * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
+ * @desc Create a cookie with all available options.
+ * @example $.cookie('the_cookie', 'the_value');
+ * @desc Create a session cookie.
+ * @example $.cookie('the_cookie', null);
+ * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
+ * used when the cookie was set.
+ *
+ * @param String name The name of the cookie.
+ * @param String value The value of the cookie.
+ * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
+ * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
+ * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
+ * If set to null or omitted, the cookie will be a session cookie and will not be retained
+ * when the the browser exits.
+ * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
+ * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
+ * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
+ * require a secure protocol (like HTTPS).
+ * @type undefined
+ *
+ * @name $.cookie
+ * @cat Plugins/Cookie
+ * @author Klaus Hartl/klaus.hartl@stilbuero.de
+ */
+
+/**
+ * Get the value of a cookie with the given name.
+ *
+ * @example $.cookie('the_cookie');
+ * @desc Get the value of a cookie.
+ *
+ * @param String name The name of the cookie.
+ * @return The value of the cookie.
+ * @type String
+ *
+ * @name $.cookie
+ * @cat Plugins/Cookie
+ * @author Klaus Hartl/klaus.hartl@stilbuero.de
+ */
+jQuery.cookie = function(name, value, options) {
+ if (typeof value != 'undefined') { // name and value given, set cookie
+ options = options || {};
+ if (value === null) {
+ value = '';
+ options.expires = -1;
+ }
+ var expires = '';
+ if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
+ var date;
+ if (typeof options.expires == 'number') {
+ date = new Date();
+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
+ } else {
+ date = options.expires;
+ }
+ expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
+ }
+ // CAUTION: Needed to parenthesize options.path and options.domain
+ // in the following expressions, otherwise they evaluate to undefined
+ // in the packed version for some reason...
+ var path = options.path ? '; path=' + (options.path) : '';
+ var domain = options.domain ? '; domain=' + (options.domain) : '';
+ var secure = options.secure ? '; secure' : '';
+ document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
+ } else { // only name given, get cookie
+ var cookieValue = null;
+ if (document.cookie && document.cookie != '') {
+ var cookies = document.cookie.split(';');
+ for (var i = 0; i < cookies.length; i++) {
+ var cookie = jQuery.trim(cookies[i]);
+ // Does this cookie string begin with the name we want?
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+ break;
+ }
+ }
+ }
+ return cookieValue;
+ }
+};
View
1  templates/base.html
@@ -4,6 +4,7 @@
<link href="/static/style.css" type="text/css" rel="stylesheet" />
<link href="/static/datePicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
+ <script type="text/javascript" src="/static/js/jquery.cookies.js"></script>
<script type="text/javascript" src="/static/js/date.js"></script>
<script type="text/javascript" src="/static/js/jquery.datePicker.js"></script>
<script type="text/javascript" src="/static/js/DD_roundies_0.0.2a-min.js"></script>
View
9 templates/new.html
@@ -3,6 +3,15 @@
<script type="text/javascript" charset="utf-8">
$(function() {
$('.date-pick').datePicker();
+ var formvalues = $.cookie('formvalues');
+ if (formvalues) {
+ formvalues = JSON.parse(formvalues);
+ for (var key in formvalues) {
+ $('[name='+key+']').val(formvalues[key]);
+ }
+ $('select[name=type]').val(formvalues['type']);
+ }
+ //$.cookie('formvalues', null);
});
</script>
<style type="text/css">
Please sign in to comment.
Something went wrong with that request. Please try again.