2016-08-20 05:57:35 +00:00
|
|
|
from django import forms
|
2016-09-09 04:24:52 +00:00
|
|
|
from django.db.models import Q
|
2016-08-20 05:57:35 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2016-09-09 04:24:52 +00:00
|
|
|
|
2016-08-20 05:57:35 +00:00
|
|
|
from utils.models import BillingAddress
|
|
|
|
from utils.forms import LoginFormMixin, SignupFormMixin, BillingAddressForm
|
|
|
|
|
2016-10-01 22:17:46 +00:00
|
|
|
from .models import MembershipType, MembershipOrder
|
2016-11-28 15:32:37 +00:00
|
|
|
from .models import Booking, BookingOrder
|
2016-08-20 05:57:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoginForm(LoginFormMixin):
|
|
|
|
email = forms.CharField(widget=forms.EmailInput())
|
|
|
|
password = forms.CharField(widget=forms.PasswordInput())
|
|
|
|
|
|
|
|
|
|
|
|
class SignupForm(SignupFormMixin):
|
|
|
|
confirm_password = forms.CharField(widget=forms.PasswordInput())
|
|
|
|
password = forms.CharField(widget=forms.PasswordInput())
|
|
|
|
name = forms.CharField(label='name',
|
|
|
|
widget=forms.TextInput(attrs={'placeholder': 'Full name'}))
|
|
|
|
|
|
|
|
|
|
|
|
class MembershipBillingForm(BillingAddressForm):
|
|
|
|
token = forms.CharField(widget=forms.HiddenInput())
|
|
|
|
membership_type = forms.ModelChoiceField(queryset=MembershipType.objects.all(),
|
|
|
|
widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
fields = ['membership_type', 'street_address', 'city', 'postal_code', 'country']
|
|
|
|
labels = {
|
|
|
|
'street_address': _('Street Address'),
|
|
|
|
'city': _('City'),
|
|
|
|
'postal_code': _('Postal Code'),
|
|
|
|
'country': _('Country'),
|
2019-12-25 05:02:37 +00:00
|
|
|
'vat_number': _('VAT Number'),
|
2016-08-20 05:57:35 +00:00
|
|
|
}
|
2016-08-31 02:32:45 +00:00
|
|
|
|
|
|
|
|
2016-10-01 22:17:46 +00:00
|
|
|
class MembershipOrderForm(forms.ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = MembershipOrder
|
|
|
|
fields = ['membership', 'customer', 'billing_address',
|
|
|
|
'last4', 'cc_brand', 'stripe_charge_id', 'amount']
|
|
|
|
|
|
|
|
# def save(self, commit=True):
|
|
|
|
# instance = super(MembershipOrderForm, self).save(commit=False)
|
|
|
|
|
|
|
|
# # if commit:
|
|
|
|
# # DonatorStatus.create(self.cleaned_data['donator'].user)
|
|
|
|
# # instance.save()
|
|
|
|
|
|
|
|
# return instance
|
|
|
|
|
|
|
|
|
2016-08-31 02:32:45 +00:00
|
|
|
class BookingBillingForm(BillingAddressForm):
|
2016-10-14 04:33:48 +00:00
|
|
|
token = forms.CharField(widget=forms.HiddenInput(), required=False)
|
2016-08-31 02:32:45 +00:00
|
|
|
start_date = forms.DateField(widget=forms.HiddenInput())
|
|
|
|
end_date = forms.DateField(widget=forms.HiddenInput())
|
|
|
|
price = forms.FloatField(widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
fields = ['start_date', 'end_date', 'price', 'street_address',
|
|
|
|
'city', 'postal_code', 'country']
|
|
|
|
labels = {
|
|
|
|
'street_address': _('Street Address'),
|
|
|
|
'city': _('City'),
|
|
|
|
'postal_code': _('Postal Code'),
|
|
|
|
'country': _('Country'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-28 15:32:37 +00:00
|
|
|
class CancelBookingForm(forms.ModelForm):
|
|
|
|
status = forms.CharField(widget=forms.HiddenInput(), required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BookingOrder
|
|
|
|
fields = ['status']
|
|
|
|
|
2016-12-05 00:44:26 +00:00
|
|
|
# def clean(self):
|
|
|
|
# booking = self.instance.booking
|
|
|
|
# days_to_start = (booking.start_date - datetime.today().date()).days
|
|
|
|
# if days_to_start < 7:
|
|
|
|
# raise forms.ValidationError("You can't cancel your booking")
|
|
|
|
# return self.cleaned_data
|
2016-11-28 15:32:37 +00:00
|
|
|
|
|
|
|
|
2016-08-31 02:32:45 +00:00
|
|
|
class BookingDateForm(forms.Form):
|
2016-09-16 04:16:21 +00:00
|
|
|
start_date = forms.DateField(required=False,
|
|
|
|
widget=forms.TextInput(attrs={'id': 'booking-date-1',
|
2017-06-29 14:34:40 +00:00
|
|
|
'value': 'Select your date'}))
|
2016-09-16 04:16:21 +00:00
|
|
|
end_date = forms.DateField(required=False,
|
|
|
|
widget=forms.TextInput(attrs={'id': 'booking-date-2'}))
|
|
|
|
|
2016-10-14 06:04:40 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user')
|
|
|
|
super(BookingDateForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2016-09-16 04:16:21 +00:00
|
|
|
def clean_start_date(self):
|
|
|
|
start_date = self.cleaned_data.get('start_date')
|
|
|
|
if not start_date:
|
|
|
|
raise forms.ValidationError("This field is required.")
|
|
|
|
return start_date
|
|
|
|
|
|
|
|
def clean_end_date(self):
|
|
|
|
end_date = self.cleaned_data.get('end_date')
|
|
|
|
if not end_date:
|
|
|
|
raise forms.ValidationError("This field is required.")
|
|
|
|
return end_date
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
start_date = self.cleaned_data.get('start_date')
|
|
|
|
end_date = self.cleaned_data.get('end_date')
|
|
|
|
|
|
|
|
if not start_date or not end_date:
|
|
|
|
return self.cleaned_data
|
2016-08-31 02:32:45 +00:00
|
|
|
|
2016-09-06 00:45:45 +00:00
|
|
|
if start_date > end_date:
|
2016-08-31 02:32:45 +00:00
|
|
|
raise forms.ValidationError("Your end date must be greather than your start date.")
|
2016-09-09 04:24:52 +00:00
|
|
|
|
2016-10-14 06:04:40 +00:00
|
|
|
q1 = Q(bookingorder__customer__user=self.user,
|
2017-01-09 14:34:25 +00:00
|
|
|
start_date__lte=start_date, end_date__gte=start_date,
|
|
|
|
bookingorder__status=BookingOrder.APPROVED)
|
2016-10-14 06:04:40 +00:00
|
|
|
q2 = Q(bookingorder__customer__user=self.user,
|
2017-01-09 14:34:25 +00:00
|
|
|
start_date__gt=start_date, start_date__lte=end_date,
|
|
|
|
bookingorder__status=BookingOrder.APPROVED)
|
2016-09-09 04:24:52 +00:00
|
|
|
if Booking.objects.filter(q1 | q2).exists():
|
|
|
|
raise forms.ValidationError("You already have a booking in these dates.")
|
|
|
|
|
2016-08-31 02:32:45 +00:00
|
|
|
return self.cleaned_data
|