def viewHistoryFromAppointment():
    if request.method == 'POST':
        date = request.form['date']
        time = request.form['time']
        user = userManager.getID(request.form['user'])

        currentDate = request.form['currentDate']
        currentTime = request.form['currentTime']
        currentUser = userManager.getID(request.form['currentPatient'])

        activeAppointment = int(
            request.form['activeAppointment']
        )  #is the appointment currently happening boolean
        appointment = appointmentManager.getAppointmentUsingDate(
            user, date, time)  #the appointment history instance
        currentAppointment = appointmentManager.getAppointmentUsingDate(
            currentUser, currentDate,
            currentTime)  #the appointment that the edit is happening from

        return render_template('appointmentDetails.html',
                               loggedInUser=current_user.get_id(),
                               appointment=appointment,
                               activeAppointment=activeAppointment,
                               currentAppointment=currentAppointment)
    return render_template('appointmentDetails.html',
                           loggedInUser=current_user.get_id())
def successfulUpdate():
    if request.method == 'POST':
        date = request.form['date']
        time = request.form['time']
        user = userManager.getID(request.form['patient'])

        appointment = appointmentManager.getAppointmentUsingDate(
            user, date, time)

        currentDate = request.form['currentDate']
        currentTime = request.form['currentTime']
        currentPatient = userManager.getID(request.form['currentPatient'])
        currentAppointment = appointmentManager.getAppointmentUsingDate(
            currentPatient, currentDate,
            currentTime)  #the appointment that the edit is happening from

        addedNotes = str(request.form['addedNotes'])
        addedMedicine = str(request.form['addedMedicine'])

        appointmentManager.updateAppointment(appointment, addedNotes,
                                             addedMedicine)

        currentAppointment.accessed = True
        currentAppointment.notes = appointment.notes
        currentAppointment.prescribedMedicine = appointment.prescribedMedicine

        saveData(centreManager, userManager, appointmentManager)
        return render_template('appointmentDetails.html',
                               loggedInUser=current_user.get_id(),
                               appointment=appointment)
    return render_template('index.html', loggedInUser=current_user.get_id())
def updateRatings(name, typeUser):
    if typeUser == 'centre':
        centre = centreManager.searchHealthCentresByName(name)[0]
        centre.updateRating(current_user.get_id(),
                            int(request.form['updateRatingBox']))
        saveData(centreManager, userManager, appointmentManager)
        return redirect(url_for('profile',
                                loggedInUser=current_user.get_id(),
                                typeUser=typeUser,
                                name=name),
                        code=307)
    elif typeUser == 'provider':
        if appointmentManager.appointmentCompleted(current_user, name) == True:
            provider = userManager.getID(name)
            provider.updateRating(current_user.get_id(),
                                  int(request.form['updateRatingBox']))
            saveData(centreManager, userManager, appointmentManager)
            return redirect(url_for('profile',
                                    loggedInUser=current_user.get_id(),
                                    typeUser=typeUser,
                                    name=name),
                            code=307)
        else:
            provider = userManager.searchID(name)
            return render_template(
                "profile.html",
                loggedInUser=current_user.get_id(),
                option="provider",
                provider=provider[0],
                error=
                "No History with this provider! No Rating can be provided.")
def showBookings():
    if request.method == 'POST':
        appointmentIndex = int(request.form['appointmentIndex'])
        appointmentNotes = str(request.form['notes'])
        appointmentPrescribedMedicine = str(request.form['prescribedMedicine'])
        currentAppointment = current_user.getSpecificAppointment(
            appointmentIndex)
        currentAppointment.notes = appointmentNotes
        currentAppointment.prescribedMedicine = appointmentPrescribedMedicine
        currentAppointment.accessed = True
        patient = userManager.getID(currentAppointment.patient)

        #if a referral was made, we want to store the patient in the list of referrals for the specialist chosen
        if current_user.getProfession() == "GP":
            newReferral = str(request.form['radioRefer'])
            if newReferral != "NoRefer":
                patient.addReferral(newReferral)
        saveData(centreManager, userManager, appointmentManager)
        return render_template('index.html',
                               loggedInUser=current_user.get_id(),
                               updated=True)

    # sorted function converts the date attribute in the appointment to a "datetime" object,
    # which then can be used to compare two dates
    # the sorted function returns the new sorted list, and the lambda function is used to get the date attribute
    return render_template(
        'appointments.html',
        loggedInUser=current_user.get_id(),
        appointments=appointmentManager.getAppointments(current_user))
def historyDetails():
    if request.method == 'POST':
        date = request.form['date']
        time = request.form['time']
        user = userManager.getID(request.form['user'])

        appointment = appointmentManager.getAppointmentUsingDate(
            user, date, time)  #the appointment history instance

        return render_template('appointmentDetails.html',
                               loggedInUser=current_user.get_id(),
                               appointment=appointment)
def updateHistory():
    if request.method == 'POST':
        date = request.form['date']
        time = request.form['time']
        user = userManager.getID(request.form['patient'])

        appointment = appointmentManager.getAppointmentUsingDate(
            user, date, time)  #the appointment history instance

        currentDate = request.form['currentDate']
        currentTime = request.form['currentTime']
        currentPatient = userManager.getID(request.form['currentPatient'])

        currentAppointment = appointmentManager.getAppointmentUsingDate(
            currentPatient, currentDate,
            currentTime)  #the appointment that the edit is happening from
        return render_template('update_history.html',
                               loggedInUser=current_user.get_id(),
                               appointment=appointment,
                               currentAppointment=currentAppointment)
    return render_template('update_history.html',
                           loggedInUser=current_user.get_id())
def getTimes(email):
    provider = userManager.getID(email)
    start_time = provider.get_startWorkingHours()
    end_time = provider.get_endWorkingHours()
    fmt = '%H:%M'
    time = datetime.datetime.strptime(start_time, fmt)
    end = datetime.datetime.strptime(end_time, fmt)
    min30 = datetime.timedelta(minutes=30)
    times = []
    while time < end:
        times.append('%s-%s' % (time.strftime(fmt),
                                (time + min30).strftime(fmt)))
        time += min30

    return times
def login():
    if current_user.is_authenticated == False:
        if request.method == 'POST':
            if userManager.correctCredentials(
                    request.form["loginEmail"],
                    request.form["loginPassword"]) == True:
                user = userManager.getID(request.form["loginEmail"])
                login_user(user)
                return redirect(url_for('index'))
            return render_template(
                "login.html", success=False
            )  # if login failed, return an alert saying "Wrong email/password"
        return render_template(
            "login.html")  # runs when the user first opens the page
    else:
        return redirect(url_for('index'))
def accessedAppointment():
    if request.method == 'POST':

        appointmentIndex = (int(request.form['appointment']) - 1)
        # Get patient appointments
        patient_id = current_user.getSpecificAppointment(
            appointmentIndex).patient
        patient = userManager.getID(patient_id)
        appointments = appointmentManager.getAppointments(patient)

        specialists = userManager.searchExpertise("")
        saveData(centreManager, userManager, appointmentManager)
        return render_template(
            'accessedAppointment.html',
            loggedInUser=current_user.get_id(),
            appointment=current_user.getSpecificAppointment(appointmentIndex),
            appointmentIndex=appointmentIndex,
            appointments=appointments,
            specialists=specialists)
    return render_template('accessedAppointment.html',
                           loggedInUser=current_user.get_id())
def get_user(user_id):
    return userManager.getID(user_id)