Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

forms.py 5.7KB

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