from django.db import models
from django.core.exceptions import ValidationError
from django.db import transaction
from .models import PartialBooking, BookingSlot

from datetime import timedelta


class BookingManager(models.Manager):
    @transaction.atomic
    def make_a_booking(self, bookable, start_date, end_date, start_slot, end_slot, user):
        has_next = True
        slot = start_slot
        current_date = start_date
        partial_bookings = []

        slots = BookingSlot.objects.filter(bookable=bookable).order_by("start_time")
        booking = self.model(user=user, bookable=bookable)  # Temporary booking, not saved yet!
        booking.full_clean()
        booking.save()
        if len(slots) == 0:
            # Throw error
            return None

        while has_next:
            try:
                print(booking)
                print(slot)
                print(current_date)
                p = PartialBooking(booking=booking,
                                   slot=slot,
                                   date=current_date)
                print("0")
                p.clean_fields()
                print("1!")
                p.clean()
                print("2!")
                p.validate_unique()
                print("3!")
                #p.full_clean(validate_unique=True)
                partial_bookings.append(p)
            except ValidationError:
                # Do not catch.
                return None

            # Try to book next slot.

            if (current_date >= end_date) and (slot == end_slot):
                has_next = False
            else:
                # if last slot of the day, add day and jump to first slot.
                if slots == slots.reverse()[0]:
                    slot = slots[0]
                    current_date = current_date + timedelta(days=1)
                else:
                    # Go to the next slot.
                    cnt = 0
                    for s in slots:
                        if s == slot:
                            break
                        cnt = + 1
                    if cnt + 1 >= len(slots):
                        slot = slots[0]
                    else:
                        slot = slots[cnt+1]
        booking.save()
        return booking

@@ -760,32 +760,50 @@
    return None%0A%0A
+        count = 0%0A
         while ha
@@ -810,16 +810,16 @@
 s_next:%0A
-
         
@@ -1510,16 +1510,152 @@
  slot.%0A%0A
+            count += 1%0A            if count == 10:%0A                print(%22N%C3%A5got gick nog galet =/%22)%0A                has_next = False%0A%0A%0A%0A
         
@@ -1675,17 +1675,17 @@
 nt_date 
-%3E
+=
 = end_da
@@ -1861,17 +1861,16 @@
  if slot
-s
  == slot
@@ -2228,58 +2228,8 @@
 + 1%0A
-                    if cnt + 1 %3E= len(slots):%0A    
     
@@ -2249,33 +2249,37 @@
    slot = slots%5B
-0
+cnt+1
 %5D%0A              
@@ -2276,78 +2276,70 @@
     
-            else:%0A                        slot = slots%5Bcnt+1%5D%0A
+booking.save()%0A        for p in partial_bookings:%0A    
         
 book
@@ -2326,39 +2326,33 @@
 gs:%0A            
-booking
+p
 .save()%0A        

Make a booking fungerar typ nu. Finns lite svagheter kvar att fixa.