66 lines
3.0 KiB
Python
66 lines
3.0 KiB
Python
from django import forms
|
|
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
|
|
from .models import User, Profile, Address, PaymentMethod
|
|
|
|
class CustomUserCreationForm(UserCreationForm):
|
|
class Meta:
|
|
model = User
|
|
fields = ('username', 'email')
|
|
|
|
class CustomUserChangeForm(UserChangeForm):
|
|
class Meta:
|
|
model = User
|
|
fields = ('username', 'email')
|
|
|
|
class ProfileForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Profile
|
|
fields = ('theme_preference',)
|
|
widgets = {
|
|
'theme_preference': forms.Select(attrs={'class': 'form-control'})
|
|
}
|
|
|
|
class AddressForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Address
|
|
fields = ('name', 'street', 'city', 'state', 'zip_code', 'address_type', 'is_default')
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Full Name'}),
|
|
'street': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Street Address'}),
|
|
'city': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'City'}),
|
|
'state': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'State'}),
|
|
'zip_code': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'ZIP Code'}),
|
|
'address_type': forms.Select(attrs={'class': 'form-select'}),
|
|
'is_default': forms.CheckboxInput(attrs={'class': 'form-checkbox'}),
|
|
}
|
|
|
|
class PaymentMethodForm(forms.ModelForm):
|
|
card_number = forms.CharField(max_length=19, widget=forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Card Number'}))
|
|
cvv = forms.CharField(max_length=4, widget=forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'CVV'}))
|
|
billing_address = forms.ModelChoiceField(queryset=Address.objects.none(), required=False, empty_label="Select Billing Address")
|
|
|
|
class Meta:
|
|
model = PaymentMethod
|
|
fields = ('brand', 'exp_month', 'exp_year', 'billing_address', 'is_default')
|
|
widgets = {
|
|
'brand': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Card Brand (e.g. Visa)'}),
|
|
'exp_month': forms.NumberInput(attrs={'class': 'form-input', 'placeholder': 'Exp Month', 'min': '1', 'max': '12'}),
|
|
'exp_year': forms.NumberInput(attrs={'class': 'form-input', 'placeholder': 'Exp Year', 'min': '2024'}),
|
|
'billing_address': forms.Select(attrs={'class': 'form-select'}),
|
|
'is_default': forms.CheckboxInput(attrs={'class': 'form-checkbox'}),
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.pop('user', None)
|
|
super().__init__(*args, **kwargs)
|
|
if user:
|
|
self.fields['billing_address'].queryset = Address.objects.filter(user=user)
|
|
|
|
def save(self, commit=True):
|
|
pm = super().save(commit=False)
|
|
pm.card_number = self.cleaned_data['card_number']
|
|
pm.cvv = self.cleaned_data['cvv']
|
|
if commit:
|
|
pm.save()
|
|
return pm
|