Fix for loosing data when there is a form validation error. Issue #42.

Added info message when there is an validation error and items are not shown.
This commit is contained in:
Tom Price
2015-03-31 10:33:57 +01:00
parent 99208f4022
commit 8340fd49d4
3 changed files with 68 additions and 45 deletions

View File

@@ -23,6 +23,14 @@ class EventForm(forms.ModelForm):
items = {} items = {}
related_models = {
'person': models.Person,
'organisation': models.Organisation,
'venue': models.Venue,
'mic': models.Profile,
'checked_in_by': models.Profile,
}
@property @property
def _get_items_json(self): def _get_items_json(self):
items = {} items = {}
@@ -37,6 +45,10 @@ class EventForm(forms.ModelForm):
self.fields['items_json'].initial = self._get_items_json self.fields['items_json'].initial = self._get_items_json
def init_items(self):
self.items = self.process_items_json()
return self.items
def process_items_json(self, event=None): def process_items_json(self, event=None):
data = simplejson.loads(self.cleaned_data['items_json']) data = simplejson.loads(self.cleaned_data['items_json'])
items = {} items = {}

View File

@@ -17,6 +17,7 @@ from PyPDF2 import PdfFileMerger, PdfFileReader
from RIGS import models, forms from RIGS import models, forms
import datetime import datetime
import re
__author__ = 'ghost' __author__ = 'ghost'
@@ -44,6 +45,17 @@ class EventCreate(generic.CreateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(EventCreate, self).get_context_data(**kwargs) context = super(EventCreate, self).get_context_data(**kwargs)
context['edit'] = True context['edit'] = True
form = context['form']
if re.search('"-\d+"', form['items_json'].value()):
messages.info(self.request, "Your item changes have been saved. Please fix the errors and save the event.")
# Get some other objects to include in the form. Used when there are errors but also nice and quick.
for field, model in form.related_models.iteritems():
value = form[field].value()
if value is not None and value != '':
context[field] = model.objects.get(pk=value)
return context return context
def get_success_url(self): def get_success_url(self):
@@ -57,6 +69,14 @@ class EventUpdate(generic.UpdateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(EventUpdate, self).get_context_data(**kwargs) context = super(EventUpdate, self).get_context_data(**kwargs)
context['edit'] = True context['edit'] = True
form = context['form']
# Get some other objects to include in the form. Used when there are errors but also nice and quick.
for field, model in form.related_models.iteritems():
value = form[field].value()
if value is not None and value != '':
context[field] = model.objects.get(pk=value)
return context return context
def get_success_url(self): def get_success_url(self):

View File

@@ -18,18 +18,19 @@
<script> <script>
function setTime23Hours() { function setTime23Hours() {
$('.end_time').val('23:00'); $('#{{ form.end_time.id_for_label }}').val('23:00');
} }
function setTime02Hours() { function setTime02Hours() {
var id_start = "{{ form.start_date.id_for_label }}"
var id_end_date = "{{ form.end_date.id_for_label }}"
if ($('.start_date').val() == $('.end_date').val()) { var id_end_time = "{{ form.end_time.id_for_label }}"
var end_date = new Date($('.end_date').val()); if ($('#'+id_start).val() == $('#'+id_end_date).val()) {
var end_date = new Date($('#'+id_end_date).val());
end_date.setDate(end_date.getDate() + 1); end_date.setDate(end_date.getDate() + 1);
$('.end_date').val(end_date.getISOString()); $('#'+id_end_date).val(end_date.getISOString());
} }
$('.end_time').val('02:00'); $('#'+id_end_time).val('02:00');
} }
$(document).ready(function () { $(document).ready(function () {
@@ -71,20 +72,22 @@
{% block content %} {% block content %}
<form class="form-horizontal itemised_form" role="form" method="POST">{% csrf_token %} <form class="form-horizontal itemised_form" role="form" method="POST">{% csrf_token %}
<div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
<div class="col-sm-8"> <div class="col-sm-8">
<h2> <h2>
{% if object.pk %} {% if object.pk %}
Event N{{ object.pk|stringformat:"05d" }} Event N{{ object.pk|stringformat:"05d" }}
{% else %} {% else %}
New Event New Event
{% endif %} {% endif %}
</h2> </h2>
</div> </div>
<div class="col-sm-4 text-right"> <div class="col-sm-4 text-right">
<div class="btn-group btn-page"> <div class="btn-group btn-page">
<button type="submit" class="btn btn-default" title="Save"><span <button type="submit" class="btn btn-default" title="Save"><span
class="glyphicon glyphicon-floppy-disk"></span></button> class="glyphicon glyphicon-floppy-disk"></span></button>
</div>
</div> </div>
</div> </div>
</div> </div>
@@ -126,7 +129,7 @@
<div class="col-sm-9 col-md-7 col-lg-8"> <div class="col-sm-9 col-md-7 col-lg-8">
<input type="text" id="{{ form.person.id_for_label }}-input" <input type="text" id="{{ form.person.id_for_label }}-input"
class="form-control autocomplete-json autocomplete-update" class="form-control autocomplete-json autocomplete-update"
value="{{ object.person|default_if_none:"" }}" value="{{ person|default_if_none:"" }}"
data-sourceurl="{% url 'api_secure' model='person' %}" data-sourceurl="{% url 'api_secure' model='person' %}"
data-target="{{ form.person.id_for_label }}"/> data-target="{{ form.person.id_for_label }}"/>
</div> </div>
@@ -136,7 +139,7 @@
data-target="#{{ form.person.id_for_label }}"> data-target="#{{ form.person.id_for_label }}">
<span class="glyphicon glyphicon-plus"></span> <span class="glyphicon glyphicon-plus"></span>
</a> </a>
<a href="{% if object.person %}{% url 'person_update' object.person.pk %}{% endif %}" class="btn btn-default modal-href" id="{{ form.person.id_for_label }}-update"> <a href="{% if form.person.value %}{% url 'person_update' form.person.value %}{% endif %}" class="btn btn-default modal-href" id="{{ form.person.id_for_label }}-update">
<span class="glyphicon glyphicon-pencil"></span> <span class="glyphicon glyphicon-pencil"></span>
</a> </a>
</div> </div>
@@ -157,7 +160,7 @@
<div class="col-sm-9 col-md-7 col-lg-8"> <div class="col-sm-9 col-md-7 col-lg-8">
<input type="text" id="{{ form.organisation.id_for_label }}-input" <input type="text" id="{{ form.organisation.id_for_label }}-input"
class="form-control autocomplete-json autocomplete-update" class="form-control autocomplete-json autocomplete-update"
value="{{ object.organisation|default_if_none:"" }}" value="{{ organisation|default_if_none:"" }}"
data-sourceurl="{% url 'api_secure' model='organisation' %}" data-sourceurl="{% url 'api_secure' model='organisation' %}"
data-target="{{ form.organisation.id_for_label }}"/> data-target="{{ form.organisation.id_for_label }}"/>
</div> </div>
@@ -167,7 +170,7 @@
data-target="#{{ form.organisation.id_for_label }}"> data-target="#{{ form.organisation.id_for_label }}">
<span class="glyphicon glyphicon-plus"></span> <span class="glyphicon glyphicon-plus"></span>
</a> </a>
<a href="{% if object.organisation %}{% url 'organisation_update' object.organisation.pk %}{% endif %}" class="btn btn-default modal-href" id="{{ form.organisation.id_for_label }}-update"> <a href="{% if form.organisation.value %}{% url 'organisation_update' form.organisation.value %}{% endif %}" class="btn btn-default modal-href" id="{{ form.organisation.id_for_label }}-update">
<span class="glyphicon glyphicon-pencil"></span> <span class="glyphicon glyphicon-pencil"></span>
</a> </a>
</div> </div>
@@ -219,7 +222,7 @@
<div class="col-sm-9 col-md-7 col-lg-8"> <div class="col-sm-9 col-md-7 col-lg-8">
<input type="text" id="{{ form.venue.id_for_label }}-input" <input type="text" id="{{ form.venue.id_for_label }}-input"
class="form-control autocomplete-json autocomplete-update" class="form-control autocomplete-json autocomplete-update"
value="{{ object.venue|default_if_none:"" }}" value="{{ venue|default_if_none:"" }}"
data-sourceurl="{% url 'api_secure' model='venue' %}" data-sourceurl="{% url 'api_secure' model='venue' %}"
data-target="{{ form.venue.id_for_label }}"/> data-target="{{ form.venue.id_for_label }}"/>
</div> </div>
@@ -244,10 +247,7 @@
<div class="col-sm-8"> <div class="col-sm-8">
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-7"> <div class="col-sm-12 col-md-7">
<input type="date" name="{{ form.start_date.name }}" {% render_field form.start_date type="date" class+="form-control" %}
id="{{ form.start_date.id_for_label }}"
class="form-control start_date" required
value="{{ form.start_date.value|date:"Y-m-d" }}"/>
</div> </div>
<div class="col-sm-12 col-md-5"> <div class="col-sm-12 col-md-5">
{% render_field form.start_time type="time" class+="form-control" %} {% render_field form.start_time type="time" class+="form-control" %}
@@ -262,13 +262,10 @@
<div class="col-sm-8"> <div class="col-sm-8">
<div class="row"> <div class="row">
<div class="col-sm-12 col-md-7"> <div class="col-sm-12 col-md-7">
<input type="date" name="{{ form.end_date.name }}" {% render_field form.end_date type="date" class+="form-control" %}
id="{{ form.end_date.id_for_label }}"
class="form-control end_date" required
value="{{ form.end_date.value|date:"Y-m-d" }}"/>
</div> </div>
<div class="col-sm-12 col-md-5"> <div class="col-sm-12 col-md-5">
{% render_field form.end_time type="time" class+="form-control end_time" %} {% render_field form.end_time type="time" class+="form-control" %}
</div> </div>
</div> </div>
@@ -290,10 +287,7 @@
class="col-sm-4 control-label">{{ form.access_at.label }}</label> class="col-sm-4 control-label">{{ form.access_at.label }}</label>
<div class="col-sm-8"> <div class="col-sm-8">
<input type="datetime-local" name="{{ form.access_at.name }}" {% render_field form.access_at type="datetime-local" class+="form-control" %}
id="{{ form.access_at.id_for_label }}"
class="form-control"
value="{{ form.access_at.value|date:"c" }}"/>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -301,10 +295,7 @@
class="col-sm-4 control-label">{{ form.meet_at.label }}</label> class="col-sm-4 control-label">{{ form.meet_at.label }}</label>
<div class="col-sm-8"> <div class="col-sm-8">
<input type="datetime-local" name="{{ form.meet_at.name }}" {% render_field form.meet_at type="datetime-local" class+="form-control" %}
id="{{ form.meet_at.id_for_label }}"
class="form-control"
value="{{ form.meet_at.value|date:"c" }}"/>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
@@ -341,7 +332,7 @@
class="form-control autocomplete-json" class="form-control autocomplete-json"
data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials" data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials"
data-target="{{ form.mic.id_for_label }}" data-target="{{ form.mic.id_for_label }}"
value="{{ object.mic.name|default_if_none:"" }}"/> value="{{ mic.name|default_if_none:"" }}"/>
</div> </div>
</div> </div>
@@ -357,9 +348,9 @@
<input type="text" id="{{ form.checked_in_by.id_for_label }}-input" <input type="text" id="{{ form.checked_in_by.id_for_label }}-input"
class="form-control autocomplete-json" class="form-control autocomplete-json"
data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,username,initials" data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials"
data-target="{{ form.checked_in_by.id_for_label }}" data-target="{{ form.checked_in_by.id_for_label }}"
value="{{ object.checked_in_by.name|default_if_none:"" }}"/> value="{{ checked_in_by.name|default_if_none:"" }}"/>
</div> </div>
</div> </div>
{% endif %} {% endif %}