class RestrictionsForm(forms.ModelForm): start_datetime = forms.DateTimeField( widget=widgets.DateTimePickerInput(), label=_("Start date"), required=False) end_datetime = forms.DateTimeField( widget=widgets.DateTimePickerInput(), label=_("End date"), required=False) def __init__(self, *args, **kwargs): super(RestrictionsForm, self).__init__(*args, **kwargs) today = datetime.date.today() self.fields['start_datetime'].initial = today class Meta: model = ConditionalOffer fields = ('start_datetime', 'end_datetime', 'max_basket_applications', 'max_user_applications', 'max_global_applications', 'max_discount', 'priority', 'exclusive') def clean(self): cleaned_data = super(RestrictionsForm, self).clean() start = cleaned_data['start_datetime'] end = cleaned_data['end_datetime'] if start and end and end < start: raise forms.ValidationError(_( "The end date must be after the start date")) return cleaned_data
class Meta: model = VoucherSet fields = [ 'name', 'code_length', 'description', 'start_datetime', 'end_datetime', 'count', ] widgets = { 'start_datetime': widgets.DateTimePickerInput(), 'end_datetime': widgets.DateTimePickerInput(), }
def test_div_attrs_context(self): i = widgets.DateTimePickerInput(format='%d/%m/%Y %H:%M') ctx = i.get_context('test_input', None, {}) self.assertEqual(ctx['div_attrs'], { 'data-wshopWidget': 'datetime', 'data-datetimeFormat': 'dd/mm/yyyy hh:ii', })
def test_datetimepickerinput_format_unicode(self): # Check that the widget can handle unicode formats i = widgets.DateTimePickerInput(format=u'δ-%d/%m/%Y %H:%M') date = datetime.datetime(2017, 5, 1, 10, 57) html = i.render('datetime', date) self.assertIn(u'value="δ-01/05/2017 10:57"', html)
def test_icon_classes_context(self): i = widgets.DateTimePickerInput(format='%d/%m/%Y %H:%M') ctx = i.get_context('test_input', None, {}) self.assertEqual(ctx['icon_classes'], 'icon-calendar glyphicon-calendar')
class VoucherForm(forms.Form): """ A specialised form for creating a voucher and offer model. """ name = forms.CharField(label=_("Name")) code = forms.CharField(label=_("Code")) start_datetime = forms.DateTimeField(widget=widgets.DateTimePickerInput(), label=_("Start datetime")) end_datetime = forms.DateTimeField(widget=widgets.DateTimePickerInput(), label=_("End datetime")) usage = forms.ChoiceField(choices=Voucher.USAGE_CHOICES, label=_("Usage")) benefit_range = forms.ModelChoiceField( label=_('Which products get a discount?'), queryset=Range.objects.all(), ) type_choices = ( (Benefit.PERCENTAGE, _('Percentage off of products in range')), (Benefit.FIXED, _('Fixed amount off of products in range')), (Benefit.SHIPPING_PERCENTAGE, _("Discount is a percentage off of the shipping cost")), (Benefit.SHIPPING_ABSOLUTE, _("Discount is a fixed amount of the shipping cost")), (Benefit.SHIPPING_FIXED_PRICE, _("Get shipping for a fixed price")), ) benefit_type = forms.ChoiceField( choices=type_choices, label=_('Discount type'), ) benefit_value = forms.DecimalField(label=_('Discount value')) exclusive = forms.BooleanField( required=False, label=_("Exclusive offers cannot be combined on the same items")) def __init__(self, voucher=None, *args, **kwargs): self.voucher = voucher super(VoucherForm, self).__init__(*args, **kwargs) def clean_name(self): name = self.cleaned_data['name'] try: voucher = Voucher.objects.get(name=name) except Voucher.DoesNotExist: pass else: if (not self.voucher) or (voucher.id != self.voucher.id): raise forms.ValidationError( _("The name '%s' is already in" " use") % name) return name def clean_code(self): code = self.cleaned_data['code'].strip().upper() if not code: raise forms.ValidationError(_("Please enter a voucher code")) try: voucher = Voucher.objects.get(code=code) except Voucher.DoesNotExist: pass else: if (not self.voucher) or (voucher.id != self.voucher.id): raise forms.ValidationError( _("The code '%s' is already in" " use") % code) return code def clean(self): cleaned_data = super(VoucherForm, self).clean() start_datetime = cleaned_data.get('start_datetime') end_datetime = cleaned_data.get('end_datetime') if start_datetime and end_datetime and end_datetime < start_datetime: raise forms.ValidationError( _("The start date must be before the" " end date")) return cleaned_data