You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forms.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django import forms
  2. from django.db.models.loading import get_model
  3. from django.utils.translation import ugettext_lazy as _
  4. ConditionalOffer = get_model('offer', 'ConditionalOffer')
  5. Condition = get_model('offer', 'Condition')
  6. Benefit = get_model('offer', 'Benefit')
  7. class MetaDataForm(forms.ModelForm):
  8. start_date = forms.DateField(widget=forms.DateInput(format='%Y-%m-%d'),
  9. label=_("Start date"), required=False)
  10. end_date = forms.DateField(widget=forms.DateInput(format='%Y-%m-%d'),
  11. label=_("End date"), required=False)
  12. class Meta:
  13. model = ConditionalOffer
  14. fields = ('name', 'description', 'start_date', 'end_date',
  15. 'max_basket_applications', 'max_user_applications',
  16. 'max_global_applications', 'max_discount')
  17. class ConditionForm(forms.ModelForm):
  18. custom_condition = forms.ChoiceField(
  19. required=False,
  20. label=_("Custom condition"), choices=())
  21. def __init__(self, *args, **kwargs):
  22. super(ConditionForm, self).__init__(*args, **kwargs)
  23. custom_conditions = Condition.objects.all().exclude(
  24. proxy_class=None)
  25. if len(custom_conditions) > 0:
  26. # Initialise custom_condition field
  27. choices = [(c.id, c.__unicode__()) for c in custom_conditions]
  28. choices.insert(0, ('', ' --------- '))
  29. self.fields['custom_condition'].choices = choices
  30. condition = kwargs.get('instance')
  31. if condition:
  32. self.fields['custom_condition'].initial = condition.id
  33. else:
  34. # No custom conditions and so the type/range/value fields
  35. # are no longer optional
  36. for field in ('type', 'range', 'value'):
  37. self.fields[field].required = True
  38. class Meta:
  39. model = Condition
  40. exclude = ('proxy_class',)
  41. def clean(self):
  42. data = super(ConditionForm, self).clean()
  43. # Check that either a condition has been entered or a custom condition
  44. # has been chosen
  45. if not any(data.values()):
  46. raise forms.ValidationError(
  47. _("Please either choose a range, type and value OR "
  48. "select a custom condition"))
  49. if not data['custom_condition']:
  50. if not data['range']:
  51. raise
  52. return data
  53. def save(self, *args, **kwargs):
  54. # We don't save a new model if a custom condition has been chosen,
  55. # we simply return the instance that has been chosen
  56. if self.cleaned_data['custom_condition']:
  57. return Condition.objects.get(
  58. id=self.cleaned_data['custom_condition'])
  59. return super(ConditionForm, self).save(*args, **kwargs)
  60. class BenefitForm(forms.ModelForm):
  61. class Meta:
  62. model = Benefit
  63. class PreviewForm(forms.Form):
  64. pass
  65. class OfferSearchForm(forms.Form):
  66. name = forms.CharField(required=False, label=_("Offer name"))
  67. is_active = forms.BooleanField(required=False, label=_("Is active?"))