<commit_before>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<commit_msg>Make a booking fungerar typ nu. Finns lite svagheter kvar att fixa.<commit_after>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

        count = 0
        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.

            count += 1
            if count == 10:
                print("Något gick nog galet =/")
                has_next = False



            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 slot == 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
                    slot = slots[cnt+1]
        booking.save()
        for p in partial_bookings:
            p.save()
        return booking
{'fraction_non_alphanumeric': 0.07627652868249632, 'fraction_numerical': 0.005883588989283463, 'mean_word_length': 1.2452830188679245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3302258', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1170, 'n_words': 319}