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-08-31 02:32:45 +00:00
|
|
|
from datetime import datetime
|
2016-08-20 05:57:35 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
from .models import MembershipType
|
2016-09-09 04:24:52 +00:00
|
|
|
from .models import Booking
|
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'),
|
|
|
|
}
|
2016-08-31 02:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BookingBillingForm(BillingAddressForm):
|
|
|
|
token = forms.CharField(widget=forms.HiddenInput())
|
|
|
|
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'),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
'value': ''}))
|
|
|
|
end_date = forms.DateField(required=False,
|
|
|
|
widget=forms.TextInput(attrs={'id': 'booking-date-2'}))
|
|
|
|
|
|
|
|
# def clean_date_range(self):
|
|
|
|
# date_range = self.cleaned_data.get('date_range')
|
|
|
|
# dates = date_range.replace(' ', '').split('-')
|
|
|
|
# try:
|
|
|
|
# start_date, end_date = [datetime.strptime(date_string, "%m/%d/%Y").date()
|
|
|
|
# for date_string in dates]
|
|
|
|
# except ValueError:
|
|
|
|
# raise forms.ValidationError("Submit valid dates.")
|
|
|
|
|
|
|
|
# if start_date > end_date:
|
|
|
|
# raise forms.ValidationError("Your end date must be greather than your start date.")
|
|
|
|
|
|
|
|
# q1 = Q(start_date__lte=start_date, end_date__gte=start_date)
|
|
|
|
# q2 = Q(start_date__gt=start_date, start_date__lte=end_date)
|
|
|
|
# if Booking.objects.filter(q1 | q2).exists():
|
|
|
|
# raise forms.ValidationError("You already have a booking in these dates.")
|
|
|
|
|
|
|
|
# return start_date, end_date
|
|
|
|
|
|
|
|
def clean_start_date(self):
|
|
|
|
start_date = self.cleaned_data.get('start_date')
|
|
|
|
if not start_date:
|
|
|
|
raise forms.ValidationError("This field is required.")
|
|
|
|
# try:
|
|
|
|
# start_date = datetime.strptime(start_date, "%m/%d/%Y").date()
|
|
|
|
# except ValueError:
|
|
|
|
# raise forms.ValidationError("Submit valid dates.")
|
|
|
|
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
|
|
|
|
|
|
|
q1 = Q(start_date__lte=start_date, end_date__gte=start_date)
|
|
|
|
q2 = Q(start_date__gt=start_date, start_date__lte=end_date)
|
|
|
|
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
|