Beispiel #1
0
def create(req):
    """ Books a new slot for the user.

        Shows user alters if:
            Slot is booked succesfully.
            User exceeds the limit of number of slots that can be booked in advance for a day.
            User attmpts to book two consecutive slots in a day in advance.
            Requested slot is already booked by another user.

        Input: req:request object.
        Output: HttpResponseRedirect object.
    """
    slot = Slot.objects.get(id=req.POST.get("slot"))
    date_string = req.POST.get("date")
    date = datetime.date.today(
    ) if date_string == "CURRENT" else datetime.datetime.strptime(
        date_string, "%Y-%m-%d")
    all_slots = Slot.get_free_slots(
        req.user.board.mid
    ) if date_string == "CURRENT" else Slot.get_free_slots_on(
        date, req.user.board.mid)

    if slot in all_slots:
        if date_string == "CURRENT":
            Booking.objects.create(slot=slot,
                                   account=req.user,
                                   booking_date=date)
            messages.add_message(req, messages.SUCCESS,
                                 "Slot " + str(slot) + " booked successfully.")
        else:
            bookings = req.user.booking_set.select_related("slot").filter(
                booking_date__year=date.year,
                booking_date__month=date.month,
                booking_date__day=date.day)
            if len(bookings) >= LIMIT:
                messages.add_message(
                    req, messages.ERROR, "Can't book more than " + str(LIMIT) +
                    " slots in a day in advance.")
            elif len(bookings) < LIMIT:
                consecutive_check = True
                for b in bookings:
                    if abs(b.slot.start_hour - slot.start_hour) <= 1:
                        consecutive_check = False
                        break
                if not consecutive_check:
                    messages.add_message(
                        req, messages.ERROR,
                        "Can't book 2 consecutive slots in a day in advance.")
                else:
                    Booking.objects.create(slot=slot,
                                           account=req.user,
                                           booking_date=date)
                    messages.add_message(
                        req, messages.SUCCESS,
                        "Slot " + str(slot) + " booked successfully.")
    else:
        messages.add_message(req, messages.ERROR,
                             "Slot " + str(slot) + " already booked.")

    return redirect(index)
Beispiel #2
0
def show(req, date_string):
    """ Shows all available slots.
        Input: req:request object.
        Output: HttpResponse object.
    """
    date = datetime.datetime.strptime(date_string, "%Y-%m-%d")
    all_slots = Slot.get_free_slots_on(date, req.user.board.mid)
    return render(req, "slot/show.html", {"all_slots": all_slots})
Beispiel #3
0
def create(req):
    slot = Slot.objects.get(id=req.POST.get("slot"))
    date_string = req.POST.get("date")
    date = datetime.date.today(
    ) if date_string == "CURRENT" else datetime.datetime.strptime(
        date_string, "%Y-%m-%d")
    all_slots = Slot.get_free_slots(
        req.user.board.mid
    ) if date_string == "CURRENT" else Slot.get_free_slots_on(
        date, req.user.board.mid)

    if slot in all_slots:
        if date_string == "CURRENT":
            Booking.objects.create(slot=slot,
                                   account=req.user,
                                   booking_date=date)
            messages.add_message(req, messages.SUCCESS,
                                 "Slot " + str(slot) + " booked successfully.")
        else:
            bookings = req.user.booking_set.select_related("slot").filter(
                booking_date__year=date.year,
                booking_date__month=date.month,
                booking_date__day=date.day)
            if len(bookings) >= LIMIT:
                messages.add_message(
                    req, messages.ERROR, "Can't book more than " + str(LIMIT) +
                    " slots in a day in advance.")
            elif len(bookings) < LIMIT:
                consecutive_check = True
                for b in bookings:
                    if abs(b.slot.start_hour - slot.start_hour) <= 1:
                        consecutive_check = False
                        break
                if not consecutive_check:
                    messages.add_message(
                        req, messages.ERROR,
                        "Can't book 2 consecutive slots in a day in advance.")
                else:
                    Booking.objects.create(slot=slot,
                                           account=req.user,
                                           booking_date=date)
                    messages.add_message(
                        req, messages.SUCCESS,
                        "Slot " + str(slot) + " booked successfully.")
    else:
        messages.add_message(req, messages.ERROR,
                             "Slot " + str(slot) + " already booked.")

    return redirect(index)
Beispiel #4
0
def show(req, date_string):
    date = datetime.datetime.strptime(date_string, "%Y-%m-%d")
    all_slots = Slot.get_free_slots_on(date, req.user.board.mid)
    return render(req, "slot/show.html", {"all_slots": all_slots})