diff --git a/digitalglarus/forms.py b/digitalglarus/forms.py index 6982037f..f5bb53aa 100644 --- a/digitalglarus/forms.py +++ b/digitalglarus/forms.py @@ -81,6 +81,10 @@ class BookingDateForm(forms.Form): end_date = forms.DateField(required=False, widget=forms.TextInput(attrs={'id': 'booking-date-2'})) + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user') + super(BookingDateForm, self).__init__(*args, **kwargs) + def clean_start_date(self): start_date = self.cleaned_data.get('start_date') if not start_date: @@ -94,7 +98,6 @@ class BookingDateForm(forms.Form): return end_date def clean(self): - start_date = self.cleaned_data.get('start_date') end_date = self.cleaned_data.get('end_date') @@ -104,8 +107,10 @@ class BookingDateForm(forms.Form): 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) + q1 = Q(bookingorder__customer__user=self.user, + start_date__lte=start_date, end_date__gte=start_date) + q2 = Q(bookingorder__customer__user=self.user, + 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.") diff --git a/digitalglarus/views.py b/digitalglarus/views.py index 0fa81637..2b11e65a 100644 --- a/digitalglarus/views.py +++ b/digitalglarus/views.py @@ -96,6 +96,11 @@ class BookingSelectDatesView(LoginRequiredMixin, MembershipRequiredMixin, FormVi login_url = reverse_lazy('digitalglarus:login') success_url = reverse_lazy('digitalglarus:booking_payment') + def get_form_kwargs(self): + kwargs = super(BookingSelectDatesView, self).get_form_kwargs() + kwargs.update({'user': self.request.user}) + return kwargs + def form_valid(self, form): user = self.request.user start_date = form.cleaned_data.get('start_date')