-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
27 lines (19 loc) · 1.03 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.utils.translation import gettext_lazy as _
from django import forms
from django.forms import ModelForm
from models import Coupon, Promotion
class CouponCodeForm(forms.Form):
code = forms.CharField()
def clean_code(self):
code = self.cleaned_data.get('code')
valid_code = Coupon.objects.filter(used__isnull=True, code=code).exists()
if not valid_code:
raise forms.ValidationError("This promotion code has either been used or is invalid. If you'd like to watch the video, go to textbooksop.com/growing-knowledge")
return code
class CouponForm(forms.ModelForm):
class Meta:
model = Coupon
fields = ('name', 'code', 'used', 'promotion', 'company', 'address', 'city', 'state', 'zip', 'email', 'phone', 'prize', 'supplier', 'promotion_answer', 'fertilizer_used', 'agree' )
def __init__(self, *args, **kwargs):
super(CouponForm, self).__init__(*args, **kwargs)
self.fields['promotion_answer'].label = self.instance.promotion.promotion_question