Exemplo n.º 1
0
def availability(request, tutorPK, pk):
    tutor = Tutor.objects.get(pk=tutorPK)
    avail = Avail.objects.get(pk=int(pk))
    startDateTime = datetime.datetime(2018, 9, 9, avail.startHour, avail.startMinute, 0)
    endDateTime = datetime.datetime(2018, 9, 9, avail.endHour, avail.endMinute, 0)
    if avail.endHour == 0 and avail.endMinute == 0:
        endDateTime += datetime.timedelta(days=1)

    weekdays = [
        ("Sunday", 0),
        ("Monday", 1),
        ("Tuesday", 2),
        ("Wednesday", 3),
        ("Thursday", 4),
        ("Friday", 5),
        ("Saturday", 6),
    ]
    weekdayDefault = avail.iso_weekday

    startHours = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 0)]
    startHourDefault = startDateTime.hour % 12

    startMinutes = [("00", 0), ("15", 15), ("30", 30), ("45", 45)]
    startMinuteDefault = startDateTime.minute

    durationHours = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
    durationHourDefault = (endDateTime - startDateTime).seconds // 3600

    durationMinutes = [("00", 0), ("15", 15), ("30", 30), ("45", 45)]
    durationMinuteDefault = (endDateTime.minute - startDateTime.minute + 60) % 60

    ampms = [("AM", 0), ("PM", 1)]
    if startDateTime - datetime.datetime(2018, 9, 9, 0, 0, 0) >= datetime.timedelta(hours=12):
        ampmDefault = 1
    else:
        ampmDefault = 0

    message = ""
    messageType = "default"

    weekdayMin = 0
    weekdayMax = 7
    startHourMin = 0
    startHourMax = 12
    startMinuteMin = 0
    startMinuteMax = 4
    ampmMin = 0
    ampmMax = 2
    durationHourMin = 0
    durationHourMax = 25
    durationMinuteMin = 0
    durationMinuteMax = 4

    if request.method == "POST":
        form = AvailForm(request.POST)
        if form.is_valid():
            # Get hidden information from form
            # availPK = int(form.cleaned_data['availPK'])
            # print("test: " + str(availID))

            delete = int(form.cleaned_data["delete"])
            print("test delete: " + str(delete))
            if delete == 1:
                print("would delete")
                # avail.delete()
                return HttpResponseRedirect("/availability/")

            weekdayMin = int(form.cleaned_data["weekdayMin"])
            print("test: " + str(weekdayMin))
            weekdayMax = int(form.cleaned_data["weekdayMax"])
            print("test: " + str(weekdayMax))
            startHourMin = int(form.cleaned_data["startHourMin"])
            print("test: " + str(startHourMin))
            startHourMax = int(form.cleaned_data["startHourMax"])
            print("test: " + str(startHourMax))
            startMinuteMin = int(form.cleaned_data["startMinuteMin"])
            print("test: " + str(startMinuteMin))
            startMinuteMax = int(form.cleaned_data["startMinuteMax"])
            print("test: " + str(startMinuteMax))
            ampmMin = int(form.cleaned_data["ampmMin"])
            print("test: " + str(ampmMin))
            ampmMax = int(form.cleaned_data["ampmMax"])
            print("test: " + str(ampmMax))
            durationHourMin = int(form.cleaned_data["durationHourMin"])
            print("test: " + str(durationHourMin))
            durationHourMax = int(form.cleaned_data["durationHourMax"])
            print("test: " + str(durationHourMax))
            durationMinuteMin = int(form.cleaned_data["durationMinuteMin"])
            print("test: " + str(durationMinuteMin))
            durationMinuteMax = int(form.cleaned_data["durationMinuteMax"])
            print("test: " + str(durationMinuteMax))
            # Get information from form
            weekday = int(form.cleaned_data["weekday"])
            weekdayDefault = weekday
            startHour = int(form.cleaned_data["startHour"])
            startHourDefault = startHour
            startMinute = int(form.cleaned_data["startMinute"])
            startMinuteDefault = startMinute
            durationHour = int(form.cleaned_data["durationHour"])
            durationHourDefault = durationHour
            durationMinute = int(form.cleaned_data["durationMinute"])
            durationMinuteDefault = durationMinute
            ampm = int(form.cleaned_data["ampm"])
            ampmDefault = ampm

            # Collect the start and end of the Avail
            newStartDateTime = datetime.datetime(2018, 9, 9, startHour, startMinute)
            if ampm == 1:
                newStartDateTime += datetime.timedelta(hours=12)

            newEndDateTime = (
                newStartDateTime + datetime.timedelta(hours=durationHour) + datetime.timedelta(minutes=durationMinute)
            )
            # Avail validation before checking for conflicts with already existing Avails

            message = availValidation(durationMinute, durationHour, newStartDateTime, newEndDateTime)

            if len(message) == 0:
                avail.iso_weekday = weekday
                avail.startHour = newStartDateTime.hour
                avail.startMinute = newStartDateTime.minute
                avail.endHour = newEndDateTime.hour
                avail.endMinute = newEndDateTime.minute
                # newAvail = Avail(iso_weekday=weekday, startHour=newStartDateTime.hour, startMinute=newStartDateTime.minute, endHour=newEndDateTime.hour, endMinute=newEndDateTime.minute)

                # Filter Avails by day
                avails = Avail.objects.filter(tutor=tutor, iso_weekday=weekday)

                # Compare Avail being created to the other Avail
                message = checkAvailConflict(avail, avails, True)

            if len(message) == 0:
                # TODO: message for if the avail really did update or not
                avail.save()
                messageType = "info"
                message = "Availability updated successfully! "
                message += (
                    str(weekdays[weekday][0])
                    + " "
                    + newStartDateTime.time().strftime("%I:%M %p")
                    + " - "
                    + newEndDateTime.time().strftime("%I:%M %p")
                )
                if newEndDateTime.time().hour == 0 and newEndDateTime.time().minute == 0:
                    message += " (" + str(weekdays[(weekday + 1) % 7][0]) + ")"
            else:
                messageType = "warning"
                print(message)

        else:
            # jquery should prevent this from happening, but for some reason if it happens
            messageType = "danger"
            message = "Form wasn't complete due to an unforseen issue. Please report this bug to the Admin. Thank you."
            print("Form wasn't complete")

        # return render(request, 'main/avails2.html', {'message': message, 'messageType': messageType})
    else:
        pass
        # form = AvailForm()

    # Limit the select options for the availability form
    weekdays = weekdays[weekdayMin:weekdayMax]
    startHours = startHours[startHourMin:startHourMax]
    startMinutes = startMinutes[startMinuteMin:startHourMax]
    durationHours = durationHours[durationHourMin:durationHourMax]
    durationMinutes = durationMinutes[durationMinuteMin:durationMinuteMax]
    ampms = ampms[ampmMin:ampmMax]

    startDateTime = datetime.datetime(2018, 9, 9, avail.startHour, avail.startMinute)
    endDateTime = datetime.datetime(2018, 9, 9, avail.endHour, avail.endMinute)
    if avail.endHour == 0 and avail.endMinute == 0:
        endDateTime += datetime.timedelta(days=1)
    startTime = startDateTime.time().strftime("%I:%M %p")
    endTime = endDateTime.time().strftime("%I:%M %p")
    seconds = (endDateTime - startDateTime).seconds
    hours = seconds // 3600
    seconds -= hours * 3600
    minutes = seconds // 60
    duration = ""
    if hours == 1:
        duration += "1 hour"
    elif hours > 1:
        duration += str(hours) + " hours"
    if hours > 0 and minutes > 0:
        duration += " and "
    if minutes == 1:
        duration += "1 minute"
    elif minutes > 1:
        duration += str(minutes) + " minutes"
    startDateTime += datetime.timedelta(days=avail.iso_weekday)
    weekdayString = startDateTime.strftime("%A")

    return render(
        request,
        "main/avail.html",
        {
            "tutor": tutor,
            "pk": pk,
            "message": message,
            "messageType": messageType,
            "weekdays": weekdays,
            "weekdayDefault": weekdayDefault,
            "startHours": startHours,
            "startHourDefault": startHourDefault,
            "startMinutes": startMinutes,
            "startMinuteDefault": startMinuteDefault,
            "ampms": ampms,
            "ampmDefault": ampmDefault,
            "durationHours": durationHours,
            "durationHourDefault": durationHourDefault,
            "durationMinutes": durationMinutes,
            "durationMinuteDefault": durationMinuteDefault,
            "weekdayMin": weekdayMin,
            "weekdayMax": weekdayMax,
            "startHourMin": startHourMin,
            "startHourMax": startHourMax,
            "startMinuteMin": startMinuteMin,
            "startMinuteMax": startMinuteMax,
            "ampmMin": ampmMin,
            "ampmMax": ampmMax,
            "durationHourMin": durationHourMin,
            "durationHourMax": durationHourMax,
            "durationMinuteMin": durationMinuteMin,
            "durationMinuteMax": durationMinuteMax,
            "avail": avail,
            "startTime": startTime,
            "endTime": endTime,
            "weekdayString": weekdayString,
            "duration": duration,
        },
    )
Exemplo n.º 2
0
def availabilities(request, tutorPK):
    tutor = Tutor.objects.get(pk=tutorPK)
    if request.user.email == tutor.email or request.user.is_staff:
        pass
    else:
        return HttpResponseForbidden()

    weekdays = [
        ("Sunday", 0),
        ("Monday", 1),
        ("Tuesday", 2),
        ("Wednesday", 3),
        ("Thursday", 4),
        ("Friday", 5),
        ("Saturday", 6),
    ]
    weekdayDefault = None

    startHours = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 0)]
    startHourDefault = None

    startMinutes = [("00", 0), ("15", 15), ("30", 30), ("45", 45)]
    startMinuteDefault = None

    durationHours = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
    durationHourDefault = None

    durationMinutes = [("00", 0), ("15", 15), ("30", 30), ("45", 45)]
    durationMinuteDefault = None

    ampms = [("AM", 0), ("PM", 1)]
    ampmDefault = None

    message = ""
    messageType = "default"

    weekdayMin = 0
    weekdayMax = 7
    startHourMin = 0
    startHourMax = 12
    startMinuteMin = 0
    startMinuteMax = 4
    ampmMin = 0
    ampmMax = 2
    durationHourMin = 0
    durationHourMax = 25
    durationMinuteMin = 0
    durationMinuteMax = 4

    if request.method == "POST":
        form = AvailForm(request.POST)
        if form.is_valid():
            delete = int(form.cleaned_data["delete"])
            print("test delete: " + str(delete))
            if delete == 1:
                print("would delete")
                # avail.delete()
                return HttpResponseRedirect("/availability/")
            # Get hidden information from form
            weekdayMin = int(form.cleaned_data["weekdayMin"])
            print("test: " + str(weekdayMin))
            weekdayMax = int(form.cleaned_data["weekdayMax"])
            print("test: " + str(weekdayMax))
            startHourMin = int(form.cleaned_data["startHourMin"])
            print("test: " + str(startHourMin))
            startHourMax = int(form.cleaned_data["startHourMax"])
            print("test: " + str(startHourMax))
            startMinuteMin = int(form.cleaned_data["startMinuteMin"])
            print("test: " + str(startMinuteMin))
            startMinuteMax = int(form.cleaned_data["startMinuteMax"])
            print("test: " + str(startMinuteMax))
            ampmMin = int(form.cleaned_data["ampmMin"])
            print("test: " + str(ampmMin))
            ampmMax = int(form.cleaned_data["ampmMax"])
            print("test: " + str(ampmMax))
            durationHourMin = int(form.cleaned_data["durationHourMin"])
            print("test: " + str(durationHourMin))
            durationHourMax = int(form.cleaned_data["durationHourMax"])
            print("test: " + str(durationHourMax))
            durationMinuteMin = int(form.cleaned_data["durationMinuteMin"])
            print("test: " + str(durationMinuteMin))
            durationMinuteMax = int(form.cleaned_data["durationMinuteMax"])
            print("test: " + str(durationMinuteMax))
            # Get information from form
            weekday = int(form.cleaned_data["weekday"])
            weekdayDefault = weekday
            startHour = int(form.cleaned_data["startHour"])
            startHourDefault = startHour
            startMinute = int(form.cleaned_data["startMinute"])
            startMinuteDefault = startMinute
            durationHour = int(form.cleaned_data["durationHour"])
            durationHourDefault = durationHour
            durationMinute = int(form.cleaned_data["durationMinute"])
            durationMinuteDefault = durationMinute
            ampm = int(form.cleaned_data["ampm"])
            ampmDefault = ampm

            # Collect the start and end of the Avail
            newStartDateTime = datetime.datetime(2018, 9, 9, startHour, startMinute)
            if ampm == 1:
                newStartDateTime += datetime.timedelta(hours=12)

            newEndDateTime = (
                newStartDateTime + datetime.timedelta(hours=durationHour) + datetime.timedelta(minutes=durationMinute)
            )
            # Avail validation before checking for conflicts with already existing Avails

            message = availValidation(durationMinute, durationHour, newStartDateTime, newEndDateTime)

            if len(message) == 0:
                # Filter Avails by day
                avails = Avail.objects.filter(tutor=tutor, iso_weekday=weekday)

                # Create new Avail
                if availPK == 0:
                    print("derp" + str(availPK))
                    newAvail = Avail(
                        iso_weekday=weekday,
                        startHour=newStartDateTime.hour,
                        startMinute=newStartDateTime.minute,
                        endHour=newEndDateTime.hour,
                        endMinute=newEndDateTime.minute,
                    )

                    # Compare Avail being created to the other Avail
                    message = checkAvailConflict(newAvail, avails, False)
                    if len(message) == 0:
                        # TODO: check if newAvail actually saved successfully and have a message if it doesn't, etc
                        newAvail.save()
                        messageType = "info"
                        message = "Availability saved successfully! "
                        message += (
                            str(weekdays[weekday][0])
                            + " "
                            + newStartDateTime.time().strftime("%I:%M %p")
                            + " - "
                            + newEndDateTime.time().strftime("%I:%M %p")
                        )
                        if newEndDateTime.time().hour == 0 and newEndDateTime.time().minute == 0:
                            message += " (" + str(weekdays[(weekday + 1) % 7][0]) + ")"
                    else:
                        messageType = "warning"
                        print(message)
                # Update avail
                else:
                    print("moo")
                    avail = Avail.objects.get(pk=int(availPK))
                    avail.iso_weekday = weekday
                    avail.startHour = newStartDateTime.hour
                    avail.startMinute = newStartDateTime.minute
                    avail.endHour = newEndDateTime.hour
                    avail.endMinute = newEndDateTime.minute
                    # newAvail = Avail(iso_weekday=weekday, startHour=newStartDateTime.hour, startMinute=newStartDateTime.minute, endHour=newEndDateTime.hour, endMinute=newEndDateTime.minute)

                    # Compare Avail being created to the other Avail
                    message = checkAvailConflict(avail, avails, True)

                if len(message) == 0:
                    # TODO: message for if the avail really did update or not
                    avail.save()
                    messageType = "info"
                    message = "Availability updated successfully! "
                    message += (
                        str(weekdays[weekday][0])
                        + " "
                        + newStartDateTime.time().strftime("%I:%M %p")
                        + " - "
                        + newEndDateTime.time().strftime("%I:%M %p")
                    )
                    if newEndDateTime.time().hour == 0 and newEndDateTime.time().minute == 0:
                        message += " (" + str(weekdays[(weekday + 1) % 7][0]) + ")"

        else:
            # jquery should prevent this from happening, but for some reason if it happens
            messageType = "danger"
            message = "Form wasn't complete due to an unforseen issue. Please report this bug to the Admin. Thank you."
            print("Form wasn't complete")
    else:
        pass
        # form = AvailForm()
    # Limit the select options for the availability form
    # weekdays = weekdays[weekdayMin:weekdayMax]
    # startHours = startHours[startHourMin:startHourMax]
    # startMinutes = startMinutes[startMinuteMin:startHourMax]
    # durationHours = durationHours[durationHourMin:durationHourMax]
    # durationMinutes = durationMinutes[durationMinuteMin:durationMinuteMax]
    # ampms = ampms[ampmMin:ampmMax]

    return render(
        request,
        "main/avails2.html",
        {
            "tutor": tutor,
            "message": message,
            "messageType": messageType,
            "weekdays": weekdays,
            "weekdayDefault": weekdayDefault,
            "startHours": startHours,
            "startHourDefault": startHourDefault,
            "startMinutes": startMinutes,
            "startMinuteDefault": startMinuteDefault,
            "ampms": ampms,
            "ampmDefault": ampmDefault,
            "durationHours": durationHours,
            "durationHourDefault": durationHourDefault,
            "durationMinutes": durationMinutes,
            "durationMinuteDefault": durationMinuteDefault,
            "weekdayMin": weekdayMin,
            "weekdayMax": weekdayMax,
            "startHourMin": startHourMin,
            "startHourMax": startHourMax,
            "startMinuteMin": startMinuteMin,
            "startMinuteMax": startMinuteMax,
            "ampmMin": ampmMin,
            "ampmMax": ampmMax,
            "durationHourMin": durationHourMin,
            "durationHourMax": durationHourMax,
            "durationMinuteMin": durationMinuteMin,
            "durationMinuteMax": durationMinuteMax,
        },
    )