diff --git a/oscar/apps/dashboard/vouchers/forms.py b/oscar/apps/dashboard/vouchers/forms.py index 79bee52bfe5..3ac2eefc8f1 100644 --- a/oscar/apps/dashboard/vouchers/forms.py +++ b/oscar/apps/dashboard/vouchers/forms.py @@ -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 diff --git a/tests/unit/dashboard/voucher_form_tests.py b/tests/unit/dashboard/voucher_form_tests.py new file mode 100644 index 00000000000..beaf43b73e2 --- /dev/null +++ b/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())) diff --git a/tests/unit/voucher/__init__.py b/tests/unit/voucher/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/unit/voucher_tests.py b/tests/unit/voucher/model_tests.py similarity index 93% rename from tests/unit/voucher_tests.py rename to tests/unit/voucher/model_tests.py index 742a0224bb9..908752bc272 100644 --- a/tests/unit/voucher_tests.py +++ b/tests/unit/voucher/model_tests.py @@ -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()