def register_new_hospital(request): # # this view provides an admin with the ability to register other users if request.user.is_superuser: title = "Register Hospital" form = HospitalForm(request.POST or None) if form.is_valid(): hospital = form.save(commit=False) hospital.save() messages.success(request, 'Form submission successful') log = Log(username="******", action=" created " + hospital.name + " hospital ") log.save() msg = "%s was registered as a new hospital" % hospital return landing(request, msg) context = {"form": form, "title": title} return render(request, 'administration/create_user.html', context) else: return redirect('/')
def register_new_nurse(request): # this view provides an admin with the ability to register other users if request.user.is_superuser: title = "Register Nurse" form = UserRegisterForm(request.POST or None) if form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') user.set_password(password) user.save() profile = Profile() profile.user = user profile.save() nurse = Nurse() nurse.profile = profile nurse.save() log = Log(username="******", action=" registered " + nurse.profile.user.username + " as a nurse ") log.save() msg = "Nurse %s was successfully created" % nurse return landing(request, msg) context = {"form": form, "title": title} return render(request, 'administration/create_user.html', context) else: return redirect('/')
def register_view(request): # if a user goes to the login screen while authenticated # they will be redirected to their profile if request.user.is_authenticated(): return redirect('/profile') title = "Register" form = UserRegisterForm(request.POST or None) if form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') user.set_password(password) user.save() profile = Profile() profile.user = user profile.save() patient = Patient() patient.profile = profile patient.save() log = Log(username=user.username, action=" registered as a patient") log.save() new_user = authenticate(username=user.username, password=password) login(request, new_user) return redirect('/profile') context = {"form": form, "title": title} return render(request, "login/register.html", context)
def edit_patient(request, patient_id): """ This view is used for doctors wishing to edit a patients profile """ if request.user.is_authenticated(): title = "Edit Patient" user_id = request.user.id if (Doctor.objects.filter(profile__user_id=user_id).count() == 1): doc = Doctor.objects.filter(profile__user_id=request.user.id)[0] usertype = "Doctor" else: doc = None usertype = "Nurse" pat_instance = Patient.objects.get(id=patient_id) initdata = { 'patient': pat_instance, "height": pat_instance.height, "weight": pat_instance.weight, "doctor": pat_instance.doctor, "allergies": pat_instance.allergies } form = PatientForm(request.POST, instance=pat_instance, initial=initdata) if request.POST: if form.is_valid(): if (Doctor.objects.filter( profile__user_id=user_id).count() == 1): log = Log(username=doc.profile.user.username, action=" edited a patient ") log.save() form.save() fullname = Patient.objects.filter(id=patient_id)[0].profile.user.first_name + " " + \ Patient.objects.filter(id=patient_id)[0].profile.user.last_name return redirect('/patientedit') else: form = PatientForm(instance=pat_instance, initial=initdata) fullname = Patient.objects.filter(id=patient_id)[0].profile.user.first_name + " " + \ Patient.objects.filter(id=patient_id)[0].profile.user.last_name return render( request, 'account/patientmodify.html', { 'form': form, 'usertype': usertype, 'patname': fullname, 'patient': patient_id, 'doc': doc, 'patinst': pat_instance })
def view_thread(request, thread_id): if request.user.is_authenticated: user_id = request.user.id userType = "" """ sets user type for the rendered page so only content for the specific user type is displayed """ isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) if isPatient: userType = "Patient" elif isDoctor: userType = "Doctor" elif isNurse: userType = "Nurse" user = Profile.objects.filter(user_id=user_id)[0] thread = Thread.objects.filter(id=thread_id)[0] messages = Message.objects.filter(thread_id=thread_id) # new message form stuff # create new form form = MessageForm(request.POST or None) # if the form is completed, log the action and save the new message if form.is_valid(): message = form.save(commit=False) message.author = Profile.objects.get(user_id=user_id) message.thread = thread message.save() log = Log(username=message.author.user.username, action=" sent a message") log.save() return HttpResponseRedirect("/viewthread/%s" % thread_id) print(userType) return render( request, "messenger/viewthread.html", { "usertype": userType, "thread": thread, "messages": messages, "user": user, "form": form })
def new_thread(request): """ this view manages the request of a new thread """ if request.user.is_authenticated(): title = "New Thread" user_id = request.user.id userType = "" """ sets user type for the rendered page so only content for the specific user type is displayed """ isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) if isPatient: userType = "Patient" elif isDoctor: userType = "Doctor" elif isNurse: userType = "Nurse" # create new form form = ThreadForm(request.POST or None) # if the form is completed, log the action and save the new thread if form.is_valid(): thread = form.save(commit=False) thread.user1 = Profile.objects.get(user_id=request.user.id) thread.save() log = Log(username=thread.user1.user.username, action=" created a new thread") log.save() return redirect('/messenger') context = {"form": form, "title": title, "usertype": userType} return render(request, "messenger/createthread.html", context) else: return redirect('/login')
def register_new_doctor(request): # # this view provides an admin with the ability to register other users if request.user.is_superuser: title = "Register Doctor" form = UserRegisterForm(request.POST or None) profile_form = ProfileForm(request.POST or None) if form.is_valid() and profile_form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') user.set_password(password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() doctor = Doctor() doctor.profile = profile doctor.save() messages.success(request, 'Form submission successful') log = Log(username="******", action=" registered " + doctor.profile.user.username + " as a doctor ") log.save() msg = "Doctor %s was successfully created" % doctor return landing(request, msg) context = { "form": form, "title": title, "prof": profile_form, } return render(request, 'administration/create_user.html', context) else: return redirect('/')
def toggle_admittance(request, patient_id): patient = Patient.objects.filter(id=patient_id)[0] if (patient.isAdmitted == "YES"): patient.isAdmitted = "NO" log = Log(username=patient.profile.user.username, action="has been discharged") else: patient.isAdmitted = "YES" log = Log(username=patient.profile.user.username, action="has been admitted") patient.save() log.save() return redirect('/patientedit')
def new_appointment_w_date(request, date_id): """ This view manages the creation of a new appointment """ if request.user.is_authenticated(): title = "Create Appointment" user_id = request.user.id # used to check which type of user is creating a new appointment isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) year = date_id[0:4] month = date_id[4:6] day = date_id[6:8] date_string = year + "-" + month + "-" + day userType = "" if isPatient: userType = "Patient" #initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) # if the form is completed, log the action and save the new appointment if form.is_valid(): appointment = form.save(commit=False) appointment.patient = Patient.objects.get( profile__user_id=request.user.id) #appointment.date = date_string appointment.save() log = Log(username=appointment.patient.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') elif isDoctor: userType = "Doctor" # initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) if form.is_valid(): appointment = form.save(commit=False) appointment.doctor = Doctor.objects.get( profile__user_id=request.user.id) #appointment.date = date_string appointment.save() log = Log(username=appointment.doctor.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') elif isNurse: userType = "Nurse" nurse = (Nurse.objects.filter(profile__user_id=user_id))[0] # initialize the from the the passed in date initdata = {"date": date_string} # create a new form form = AppointmentForm(request.POST or None, initial=initdata) if form.is_valid(): appointment = form.save(commit=False) #appointment.date = date_string appointment.save() log = Log(username=nurse.profile.user.username, action=" created a new appointment ") log.save() return redirect('/calendar') context = {"form": form, "title": title, "usertype": userType} return render(request, "appointments/create_appointment.html", context) else: return redirect('/login') #-------------------------------------------------------------------------------------------------------
def new_appointment(request): """ This view manages the creation of a new appointment """ if request.user.is_authenticated(): title = "Create Appointment" user_id = request.user.id # used to check which type of user is creating a new appointment isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) userType = "" if isPatient: userType = "Patient" # create a new form form = AppointmentForm(request.POST or None) # if the form is completed, log the action and save the new appointment if form.is_valid(): appointment = form.save(commit=False) appointment.patient = Patient.objects.get( profile__user_id=request.user.id) appointment.save() log = Log(username=appointment.patient.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment with %s successfully created on %s" % ( appointment.doctor, appointment.date) return account.views.index(request, msg) elif isDoctor: userType = "Doctor" form = AppointmentFormDoctor(request.POST or None) if form.is_valid(): appointment = form.save(commit=False) appointment.doctor = Doctor.objects.get( profile__user_id=request.user.id) appointment.save() log = Log(username=appointment.doctor.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment with patient %s successfully created on %s" % ( appointment.patient, appointment.date) return account.views.index(request, msg) elif isNurse: userType = "Nurse" nurse = (Nurse.objects.filter(profile__user_id=user_id))[0] form = AppointmentFormNurse(request.POST or None) if form.is_valid(): appointment = form.save(commit=False) appointment.save() log = Log(username=nurse.profile.user.username, action=" created a new appointment ") log.save() msg = "Appointment successfully created with patient %s seeing %s on %s" % ( appointment.patient, appointment.doctor, appointment.date) return account.views.index(request, msg) context = {"form": form, "title": title, "usertype": userType} return render(request, "appointments/create_appointment.html", context) else: return redirect('/login')
def edit_appointment(request, appointment_id): # this view is used for editing appointments with the appointmentform if request.user.is_authenticated(): title = "Edit Appointment" user_id = request.user.id isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) app_instance = Appointment.objects.get(id=appointment_id) if isPatient: # get the patient instance pat = Patient.objects.filter(profile__user_id=request.user.id)[0] # tests if the patient is viewing only their appointments isValid = (Appointment.objects.filter(id=appointment_id, patient=pat)) # redirect if trying to access others appointments if not isValid: return redirect('/profile') # used to fill form with current values initdata = { 'doctor': app_instance.doctor, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } # create the form form = AppointmentForm(request.POST, instance=app_instance, initial=initdata) if request.POST: """ modifications to appointments is completed in a bit of a messy way. The issue is that appointments should not be able to be edited to be during the same time of another if the doctor is the same. This test is done by getting a list of all appointments and checking if any match the current form submission. The problem with this is when an appointment is under edit, it already exists, and the checker flags the operation as illegal. The way around this was to save all the instance's current fields, and delete it. If the form fails, a new instance is created with the old forms and the user can try again. If the form is successful a new instance is created with the new values and the user is redirected. """ # these are the appointment fields being saved pk = Appointment.objects.filter(id=appointment_id)[0].id date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time doc = Appointment.objects.filter(id=appointment_id)[0].doctor reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason # deletes the current appointment instance Appointment.objects.filter(id=appointment_id)[0].delete() # if the form is valid, log it and save the new data if form.is_valid(): log = Log(username=pat.profile.user.username, action=" edited an appointment ") log.save() form.save() msg = "Appointment with %s on %s successfully modified" % ( doc, date) return account.views.index(request, msg) # if the form fails, create a new appointment instance and save it and send a new form else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentForm(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'patient' }) elif isDoctor: doc = Doctor.objects.filter(profile__user_id=request.user.id)[0] # tests if the doctor is viewing only their appointments isValid = (Appointment.objects.filter(id=appointment_id, doctor=doc)) if not isValid: return redirect('/profile') initdata = { 'patient': app_instance.patient, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } form = AppointmentFormDoctor(request.POST, instance=app_instance, initial=initdata) if request.POST: date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time pat = Appointment.objects.filter(id=appointment_id)[0].patient reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason pk = Appointment.objects.filter(id=appointment_id)[0].id Appointment.objects.filter(id=appointment_id)[0].delete() if form.is_valid(): log = Log(username=doc.profile.user.username, action=" edited an appointment ") log.save() form.save() return redirect('/') else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentFormDoctor(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'Doctor' }) elif isNurse: nurse = Nurse.objects.filter(profile__user_id=request.user.id) initdata = { 'patient': app_instance.patient, "doctor": app_instance.doctor, "date": app_instance.date, 'time': app_instance.time, 'reason': app_instance.reason, 'short_reason': app_instance.short_reason } form = AppointmentFormNurse(request.POST, instance=app_instance, initial=initdata) if request.POST: pk = Appointment.objects.filter(id=appointment_id)[0].id date = Appointment.objects.filter(id=appointment_id)[0].date time = Appointment.objects.filter(id=appointment_id)[0].time pat = Appointment.objects.filter(id=appointment_id)[0].patient doc = Appointment.objects.filter(id=appointment_id)[0].doctor reason = Appointment.objects.filter( id=appointment_id)[0].reason s_reason = Appointment.objects.filter( id=appointment_id)[0].short_reason Appointment.objects.filter(id=appointment_id)[0].delete() if form.is_valid(): form.save() log = Log(username=nurse[0].profile.user.username, action=" edited an appointment ") log.save() return redirect('/') else: app = Appointment(id=pk, patient=pat, date=date, time=time, doctor=doc, reason=reason, short_reason=s_reason) app.save() else: form = AppointmentFormNurse(instance=app_instance, initial=initdata) return render(request, 'appointments/edit_appointment.html', { 'form': form, 'usertype': 'Nurse' }) else: return redirect('/login')
def edit_info(request): if request.user.is_authenticated: # gets the user id based on the request user_id = request.user.id # gets the user instance based on the id user_instance = User.objects.get(id=user_id) isPatient = (Patient.objects.filter( profile__user_id=user_id).count() == 1) isDoctor = (Doctor.objects.filter( profile__user_id=user_id).count() == 1) isNurse = (Nurse.objects.filter(profile__user_id=user_id).count() == 1) if isPatient: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber, 'hospital_assignment': profile_instance.hospital_assignment } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the patient instance based on the id patient_instance = Patient.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled patient_init_data = { 'height': patient_instance.height, 'weight': patient_instance.weight, 'age': patient_instance.age, 'hospital_preferred': patient_instance.hospital_preferred } # create a new patient form that is tied to the current instance and is prefilled with current data # if the user is submitting a form, save it if it is valid if request.POST: patient_form = PatientForm(request.POST, instance=patient_instance) profile_form = ProfileForm(request.POST, instance=profile_instance) user_form = UserForm(request.POST, instance=user_instance) if user_form.is_valid() and patient_form.is_valid( ) and profile_form.is_valid(): user_form.save() patient_form.save() profile_form.save() log = Log(username=patient_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") else: patient_form = PatientForm(instance=patient_instance, initial=patient_init_data) profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) user_form = UserForm(instance=user_instance, initial=user_init_data) appointments = Appointment.objects.filter(patient=patient_instance) return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "patient": patient_instance, "userform": user_form, "patientform": patient_form, "profileform": profile_form, "usertype": "Patient", "appointments": appointments }) elif isDoctor: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber, 'hospital_assignment': profile_instance.hospital_assignment } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the doctor instance based on the id doctor_instance = Doctor.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled doctor_init_data = {'type': doctor_instance.type} # create a new patient form that is tied to the current instance and is prefilled with current data doctor_form = DoctorForm(instance=doctor_instance, initial=doctor_init_data) # if the user is submitting a form, save it if it is valid if request.POST: doctor_form = DoctorForm(request.POST, instance=doctor_instance, initial=doctor_init_data) profile_form = ProfileForm(request.POST, instance=profile_instance, initial=profile_init_data) user_form = UserForm(request.POST, instance=user_instance, initial=user_init_data) if user_form.is_valid() and profile_form.is_valid(): user_form.save() doctor_form.save() profile_form.save() log = Log(username=doctor_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") appointments = Appointment.objects.filter( doctor=doctor_instance).order_by('-date').reverse() return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "doctor": doctor_instance, "userform": user_form, "doctorform": doctor_form, "profileform": profile_form, "usertype": "Doctor", "appointments": appointments }) elif isNurse: # if there is data that exists on the user, the forms will be prefilled user_init_data = { 'first_name': user_instance.first_name, 'last_name': user_instance.last_name, 'email': user_instance.email, 'username': user_instance.username } # create a new user form that is tied to the current instance and is prefilled with current data user_form = UserForm(instance=user_instance, initial=user_init_data) # get the profile instance based on the id profile_instance = Profile.objects.get(user_id=user_id) # if there is data that exists on the profile, the forms will be prefilled profile_init_data = { 'streetAddress': profile_instance.streetAddress, 'town': profile_instance.town, 'zipCode': profile_instance.zipCode, 'state': profile_instance.state, 'phoneNumber': profile_instance.phoneNumber } profile_form = ProfileForm(instance=profile_instance, initial=profile_init_data) # get the nurse instance based on the id nurse_instance = Nurse.objects.get(profile__user_id=user_id) # if there is data that exists on the patient, the forms will be prefilled nurse_init_data = {'type': nurse_instance.type} # create a new nurse form that is tied to the current instance and is prefilled with current data nurse_form = NurseForm(instance=nurse_instance, initial=nurse_init_data) # if the user is submitting a form, save it if it is valid if request.POST: nurse_form = NurseForm(request.POST, instance=nurse_instance, initial=nurse_init_data) profile_form = ProfileForm(request.POST, instance=profile_instance, initial=profile_init_data) user_form = UserForm(request.POST, instance=user_instance, initial=user_init_data) if user_form.is_valid(): user_form.save() nurse_form.save() profile_form.save() log = Log(username=nurse_instance.profile.user.username, action=" made profile changes ") log.save() return redirect('/profile') else: print("error") appointments = Appointment.objects.filter( hospital=profile_instance.hospital_assignment).order_by( '-date').reverse() return render( request, "account/useredit.html", { "user": request.user, "profile": profile_instance, "nurse": nurse_instance, "userform": user_form, "nurseform": nurse_form, "profileform": profile_form, "usertype": "Nurse", "appointments": appointments }) else: return redirect('/login')