Skip to content

Commit

Permalink
Fixed #803: order to create must not be empty (#807)
Browse files Browse the repository at this point in the history
* Fixed #803: order to create must not be empty

* Fixed: enable error messages on order batch create form. Related to #803
  • Loading branch information
lingxiaoyang authored and erozqba committed Sep 29, 2017
1 parent 7253b48 commit e2f6b28
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
6 changes: 0 additions & 6 deletions src/frontend/js/multidatespicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,14 @@ $(function() {
$.each(client_meals_default, function (key, value) {
var selector = "#id_"+key+"_"+date+"_quantity";
$(selector).val(value || 0);
dismissFieldError($(selector));
});
if (client_meals_default.hasOwnProperty('size')) {
$(size_selector).dropdown('set selected', client_meals_default.size);
dismissFieldError($(size_selector));
}
}
});
})();

function dismissFieldError(elem) {
$(elem).closest('.error').removeClass('error');
}

$('#form_create_batch #id_client .ui.dropdown').dropdown('setting', 'onChange', function (value, text, $selectedItem) {
// on client change, update the form to update the client default.
$('#form_create_batch #id_is_submit').val("0");
Expand Down
41 changes: 41 additions & 0 deletions src/order/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,47 @@ def clean_is_submit(self):
)
return is_submit

def clean(self):
"""
For each delivery date, the quantities and options should
not be all empty. Refs #803.
"""
cleaned_data = super(CreateOrdersBatchForm, self).clean()
delivery_dates_str = cleaned_data.get('delivery_dates')
if delivery_dates_str:
delivery_dates = delivery_dates_str.split('|')
else:
delivery_dates = []
fields_not_null_check = [
('delivery_{}', lambda x: x is True),
('pickup_{}', lambda x: x is True),
('visit_{}', lambda x: x is True),
]
for meal, meal_translation in COMPONENT_GROUP_CHOICES:
if meal is COMPONENT_GROUP_CHOICES_SIDES:
continue # "sides" not in the form
else:
fields_not_null_check.append(
('%s_{}_quantity' % meal, lambda x: x and x > 0))
for delivery_date in delivery_dates:
if all([
bool(check_fn(
cleaned_data.get(field_template.format(delivery_date))
)) is False
for field_template, check_fn in fields_not_null_check
]):
# Error-highlight all fields on that date.
for field_template, check_fn in fields_not_null_check:
self.add_error(
field_template.format(delivery_date),
forms.ValidationError(
_("Empty order is not allowed."),
code='empty_delivery_date'
)
)

return cleaned_data


class OrderStatusChangeForm(forms.ModelForm):

Expand Down
6 changes: 5 additions & 1 deletion src/order/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-04 14:50-0400\n"
"POT-Creation-Date: 2017-08-25 16:12-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: PascalPriori <pascal.priori@savoirfairelinux.com>, 2016\n"
"Language-Team: French (https://www.transifex.com/savoirfairelinux/teams/63058/fr/)\n"
Expand Down Expand Up @@ -41,6 +41,10 @@ msgstr ""
msgid "This field must be 1 to submit the form."
msgstr ""

#: order/forms.py
msgid "Empty order is not allowed."
msgstr ""

#: order/forms.py
msgid "A reason is required for No Charge order."
msgstr ""
Expand Down
5 changes: 5 additions & 0 deletions src/order/templates/order/create_batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h2 class="ui header">{% trans 'Place orders' %}</h2>
{% with form|get_item:field_name as field %}
<div class="field {% if field.errors %}error{% endif %}">
{{ field }}
{{ field.errors }}
</div>
{% endwith %}
{% endwith %}
Expand All @@ -77,6 +78,7 @@ <h2 class="ui header">{% trans 'Place orders' %}</h2>
{% with form|get_item:field_name as field %}
<div class="field {% if field.errors %}error{% endif %}">
{{ field }}
{{ field.errors }}
</div>
{% endwith %}
{% endwith %}
Expand All @@ -94,6 +96,7 @@ <h2 class="ui header">{% trans 'Place orders' %}</h2>
<div class="ui toggle checkbox">
{{ field }}
<label>{% trans "Delivery" %}</label>
{{ field.errors }}
</div>
</div>
{% endwith %}
Expand All @@ -107,6 +110,7 @@ <h2 class="ui header">{% trans 'Place orders' %}</h2>
<div class="ui toggle checkbox">
{{ field }}
<label>{% trans "Pickup" %}</label>
{{ field.errors }}
</div>
</div>
{% endwith %}
Expand All @@ -120,6 +124,7 @@ <h2 class="ui header">{% trans 'Place orders' %}</h2>
<div class="ui toggle checkbox">
{{ field }}
<label>{% trans "Visit" %}</label>
{{ field.errors }}
</div>
</div>
{% endwith %}
Expand Down
25 changes: 15 additions & 10 deletions src/order/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,21 @@ def form_invalid(self, form, **kwargs):
# open accordions if there's an invalid field in it.
context = self.get_context_data(**kwargs)
context['form'] = form
for field in form.errors.keys():
try:
# next() - find first occurence
invalid_date = next(
date for date in context['accordions_inactive']
if date in field
)
context['accordions_inactive'].remove(invalid_date)
except StopIteration:
pass
if form.cleaned_data.get('is_submit') != 1:
form._errors = {}
else:
for field in form.errors.keys():
try:
# next() - find first occurence
invalid_date = next(
date for date in context['accordions_inactive']
if date in field
)
print(invalid_date)
context['accordions_inactive'].remove(invalid_date)
except StopIteration:
pass

return self.render_to_response(context)

def form_valid(self, form):
Expand Down

0 comments on commit e2f6b28

Please sign in to comment.