Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix validation error in dashboard voucher form
The clean method was raising a KeyError when the date fields were empty.
  • Loading branch information
codeinthehole committed Apr 17, 2013
1 parent fd256b6 commit 02b3644
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions oscar/apps/dashboard/vouchers/forms.py
Expand Up @@ -63,9 +63,9 @@ def clean_code(self):

def clean(self):
cleaned_data = super(VoucherForm, self).clean()
start_date = cleaned_data['start_date']
end_date = cleaned_data['end_date']
if end_date < start_date:
start_date = cleaned_data.get('start_date', None)
end_date = cleaned_data.get('end_date', None)
if start_date and end_date and end_date < start_date:
raise forms.ValidationError(_("The start date must be before the end date"))
return cleaned_data

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/dashboard/voucher_form_tests.py
@@ -0,0 +1,22 @@
from django import test

from oscar.apps.dashboard.vouchers import forms


class TestVoucherForm(test.TestCase):

def test_handles_empty_date_fields(self):
data = {'code': '',
'name': '',
'start_date': '',
'end_date': '',
'benefit_range': '',
'benefit_type': 'Percentage',
'usage': 'Single use'}
form = forms.VoucherForm(data=data)
try:
form.is_valid()
except Exception, e:
import traceback
self.fail("Validating form failed: %s\n\n%s" % (
e.message, traceback.format_exc()))
Empty file added tests/unit/voucher/__init__.py
Empty file.
Expand Up @@ -23,9 +23,8 @@ def test_saves_code_as_uppercase(self):

def test_verifies_dates_are_sensible(self):
with self.assertRaises(exceptions.ValidationError):
voucher = Voucher.objects.create(code='lower',
start_date=END_DATE,
end_date=START_DATE)
voucher = Voucher.objects.create(
code='lower', start_date=END_DATE, end_date=START_DATE)
voucher.clean()


Expand Down

0 comments on commit 02b3644

Please sign in to comment.