Files
goodtimes/manage_subscriptions/forms.py
2024-08-12 20:47:36 +05:30

85 lines
2.4 KiB
Python

from django import forms
from accounts.models import IAmPrincipalType
from manage_subscriptions.models import (
PrincipalSubscription,
StripeProduct,
Subscription,
Plan,
)
class PlanForm(forms.ModelForm):
class Meta:
model = Plan
fields = ["title", "days"] # Include all fields you want from the model
# You can add custom validation for Plan fields here if needed
# Example:
# def clean_title(self):
# title = self.cleaned_data.get('title')
# # Add your validation logic here
# return title
class SubscriptionForm(forms.ModelForm):
class Meta:
model = Subscription
fields = [
"title",
"stripe_product",
"plan",
"high_amount",
"amount",
"short_description",
"principal_types",
"referral_percentage",
"active",
"deleted",
"is_free",
]
def __init__(self, *args, **kwargs):
super(SubscriptionForm, self).__init__(*args, **kwargs)
event_user = IAmPrincipalType.objects.get(name="event_user")
event_manager = IAmPrincipalType.objects.get(name="event_manager")
self.fields["principal_types"].queryset = IAmPrincipalType.objects.filter(
id__in=[event_user.id, event_manager.id]
)
def clean(self):
cleaned_data = super().clean()
stripe_product = cleaned_data.get("stripe_product")
if not stripe_product:
self.add_error(
"stripe_product",
"Please select a Stripe product to create a subscription.",
)
return cleaned_data
class PrincipalSubscriptionForm(forms.ModelForm):
class Meta:
model = PrincipalSubscription
fields = "__all__" # Includes all fields from the model
widgets = {
"start_date": forms.DateInput(attrs={"type": "date"}),
"end_date": forms.DateInput(attrs={"type": "date"}),
"grace_period_end_date": forms.DateInput(attrs={"type": "date"}),
"cancelled_date_time": forms.DateTimeInput(attrs={"type": "datetime"}),
}
class StripeProductForm(forms.ModelForm):
class Meta:
model = StripeProduct
fields = [
"title",
"description",
]
widgets = {
"description": forms.Textarea(attrs={"rows": 3}),
}