website/hub/services/forms.py

51 lines
1.7 KiB
Python
Raw Normal View History

2025-01-27 16:51:23 +01:00
from django import forms
2025-01-28 10:41:39 +01:00
from .models import Lead, Plan, PlanPrice
2025-01-27 16:51:23 +01:00
class LeadForm(forms.ModelForm):
class Meta:
model = Lead
2025-01-31 17:13:55 +01:00
fields = ["name", "company", "email", "phone", "message"]
2025-01-27 16:51:23 +01:00
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"}),
2025-01-31 17:13:55 +01:00
"message": forms.Textarea(attrs={"class": "form-control", "rows": 4}),
2025-01-27 16:51:23 +01:00
}
2025-01-28 10:41:39 +01:00
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