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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import datetime
  2. import six
  3. from django import forms
  4. from django.utils.translation import ugettext_lazy as _
  5. from oscar.core.loading import get_model
  6. from oscar.forms import widgets
  7. ConditionalOffer = get_model('offer', 'ConditionalOffer')
  8. Condition = get_model('offer', 'Condition')
  9. Benefit = get_model('offer', 'Benefit')
  10. class MetaDataForm(forms.ModelForm):
  11. class Meta:
  12. model = ConditionalOffer
  13. fields = ('name', 'description',)
  14. class RestrictionsForm(forms.ModelForm):
  15. start_datetime = forms.DateTimeField(
  16. widget=widgets.DateTimePickerInput(),
  17. label=_("Start date"), required=False)
  18. end_datetime = forms.DateTimeField(
  19. widget=widgets.DateTimePickerInput(),
  20. label=_("End date"), required=False)
  21. def __init__(self, *args, **kwargs):
  22. super(RestrictionsForm, self).__init__(*args, **kwargs)
  23. today = datetime.date.today()
  24. self.fields['start_datetime'].initial = today.strftime(
  25. self.fields['start_datetime'].widget.format)
  26. class Meta:
  27. model = ConditionalOffer
  28. fields = ('start_datetime', 'end_datetime',
  29. 'max_basket_applications', 'max_user_applications',
  30. 'max_global_applications', 'max_discount')
  31. def clean(self):
  32. cleaned_data = super(RestrictionsForm, self).clean()
  33. start = cleaned_data['start_datetime']
  34. end = cleaned_data['end_datetime']
  35. if start and end and end < start:
  36. raise forms.ValidationError(_(
  37. "The end date must be after the start date"))
  38. return cleaned_data
  39. class ConditionForm(forms.ModelForm):
  40. custom_condition = forms.ChoiceField(
  41. required=False,
  42. label=_("Custom condition"), choices=())
  43. def __init__(self, *args, **kwargs):
  44. super(ConditionForm, self).__init__(*args, **kwargs)
  45. custom_conditions = Condition.objects.all().exclude(
  46. proxy_class=None)
  47. if len(custom_conditions) > 0:
  48. # Initialise custom_condition field
  49. choices = [(c.id, six.text_type(c)) for c in custom_conditions]
  50. choices.insert(0, ('', ' --------- '))
  51. self.fields['custom_condition'].choices = choices
  52. condition = kwargs.get('instance')
  53. if condition:
  54. self.fields['custom_condition'].initial = condition.id
  55. else:
  56. # No custom conditions and so the type/range/value fields
  57. # are no longer optional
  58. for field in ('type', 'range', 'value'):
  59. self.fields[field].required = True
  60. class Meta:
  61. model = Condition
  62. exclude = ('proxy_class',)
  63. def clean(self):
  64. data = super(ConditionForm, self).clean()
  65. # Check that either a condition has been entered or a custom condition
  66. # has been chosen
  67. if not any(data.values()):
  68. raise forms.ValidationError(
  69. _("Please either choose a range, type and value OR "
  70. "select a custom condition"))
  71. if not data['custom_condition']:
  72. if not data.get('range', None):
  73. raise forms.ValidationError(
  74. _("A range is required"))
  75. return data
  76. def save(self, *args, **kwargs):
  77. # We don't save a new model if a custom condition has been chosen,
  78. # we simply return the instance that has been chosen
  79. if self.cleaned_data['custom_condition']:
  80. return Condition.objects.get(
  81. id=self.cleaned_data['custom_condition'])
  82. return super(ConditionForm, self).save(*args, **kwargs)
  83. class BenefitForm(forms.ModelForm):
  84. custom_benefit = forms.ChoiceField(
  85. required=False,
  86. label=_("Custom incentive"), choices=())
  87. def __init__(self, *args, **kwargs):
  88. super(BenefitForm, self).__init__(*args, **kwargs)
  89. custom_benefits = Benefit.objects.all().exclude(
  90. proxy_class=None)
  91. if len(custom_benefits) > 0:
  92. # Initialise custom_benefit field
  93. choices = [(c.id, six.text_type(c)) for c in custom_benefits]
  94. choices.insert(0, ('', ' --------- '))
  95. self.fields['custom_benefit'].choices = choices
  96. benefit = kwargs.get('instance')
  97. if benefit:
  98. self.fields['custom_benefit'].initial = benefit.id
  99. else:
  100. # No custom benefit and so the type fields
  101. # are no longer optional
  102. self.fields['type'].required = True
  103. class Meta:
  104. model = Benefit
  105. exclude = ('proxy_class',)
  106. def clean(self):
  107. data = super(BenefitForm, self).clean()
  108. # Check that either a benefit has been entered or a custom benfit
  109. # has been chosen
  110. if not any(data.values()):
  111. raise forms.ValidationError(
  112. _("Please either choose a range, type and value OR "
  113. "select a custom incentive"))
  114. if data['custom_benefit']:
  115. if data.get('range') or data.get('type') or data.get('value'):
  116. raise forms.ValidationError(
  117. _("No other options can be set if you are using a "
  118. "custom incentive"))
  119. return data
  120. def save(self, *args, **kwargs):
  121. # We don't save a new model if a custom benefit has been chosen,
  122. # we simply return the instance that has been chosen
  123. if self.cleaned_data['custom_benefit']:
  124. return Benefit.objects.get(
  125. id=self.cleaned_data['custom_benefit'])
  126. return super(BenefitForm, self).save(*args, **kwargs)
  127. class OfferSearchForm(forms.Form):
  128. name = forms.CharField(required=False, label=_("Offer name"))
  129. is_active = forms.BooleanField(required=False, label=_("Is active?"))