49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from django import forms
|
|
from .models import Lead, Plan, PlanPrice
|
|
|
|
|
|
class LeadForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Lead
|
|
fields = ["name", "company", "email", "phone"]
|
|
widgets = {
|
|
"name": forms.TextInput(attrs={"class": "form-control"}),
|
|
"company": forms.TextInput(attrs={"class": "form-control"}),
|
|
"email": forms.EmailInput(attrs={"class": "form-control"}),
|
|
"phone": forms.TextInput(attrs={"class": "form-control"}),
|
|
}
|
|
|
|
|
|
class PlanForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Plan
|
|
fields = ("name", "description", "is_default", "features", "order")
|
|
widgets = {
|
|
"description": forms.Textarea(attrs={"rows": 3}),
|
|
"features": forms.Textarea(attrs={"rows": 4}),
|
|
}
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
# If this is set as default, ensure no other plan for this service is default
|
|
if cleaned_data.get("is_default"):
|
|
service = self.instance.service
|
|
if service:
|
|
Plan.objects.filter(service=service).exclude(
|
|
pk=self.instance.pk
|
|
).update(is_default=False)
|
|
return cleaned_data
|
|
|
|
|
|
class PlanPriceForm(forms.ModelForm):
|
|
class Meta:
|
|
model = PlanPrice
|
|
fields = ("currency", "price")
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
currency = cleaned_data.get("currency")
|
|
price = cleaned_data.get("price")
|
|
if price and price < 0:
|
|
raise forms.ValidationError("Price cannot be negative")
|
|
return cleaned_data
|