mercredi 27 mars 2019

Checking if all info exists in one record

I am currently making an appointment scheduler. I am looking to execute an if statement if the doctor is available at the selected time and date. The code I currently have is as follows:

def book():
    form = BookAppointment(request.form)
    if request.method == 'POST' and form.validate():
        appointment_date = form.appointment_date.data
        appointment_time = form.appointment_time.data
        appointment_patient_id = form.appointment_patient_id.data
        appointment_doctor_id = form.appointment_doctor_id.data
        appointment_centre_id = form.appointment_centre_id.data
        username = session.get('username', None)

        # Create cursor
        cur = mysql.connection.cursor()

        find_doctor = cur.execute("SELECT * FROM doctors WHERE doctor_id =%s", [appointment_doctor_id])
        find_date = cur.execute("SELECT * FROM appointments WHERE appointment_date =%s", [appointment_date])
        find_time = cur.execute("SELECT * FROM appointments WHERE appointment_time =%s", [appointment_time])
        msg = cur.fetchone()

        # Execute query
        cur.execute("INSERT INTO appointments(appointment_date, appointment_time, patient_id, doctor_id, centre_id, patient_username) VALUES(%s, %s, %s, %s, %s, %s)", (appointment_date, appointment_time, appointment_patient_id, appointment_doctor_id, appointment_centre_id, username))

        if find_doctor == 0 and find_date == 0 and find_time == 0:
            if appointment_date.weekday() == 0 or appointment_date.weekday() == 1 or appointment_date.weekday() == 2 or appointment_date.weekday() == 3 or appointment_date.weekday() == 4:
                # Commit to DB
                mysql.connection.commit()

                # Close connection
                cur.close()

                flash('Appointment made successfully!', 'success')
                return redirect(url_for('book'))

            else:
                error = 'Sorry, appointments cannot be made for a Saturday/Sunday'
                return render_template('/patient/bookappointment.html', error=error, form=form)
        else:
            flash('Appointment unavailable, please select another date or time', 'danger')
            return redirect(url_for('book'))
    return render_template('/patient/bookappointment.html', form=form)

So I want an appointment to be made if there is currently no appointment scheduled with the same doctor at the same date and time. Thanks

Aucun commentaire:

Enregistrer un commentaire