Ejemplo n.º 1
0
def create_administrator(request):
    creator = get_account_from_user(request.user)
    administrator_form = None

    if request.method == 'POST':
        user_form = UserCreationForm(request.POST)
        profile_information_form = ProfileInformationForm(request.POST)

        if user_form.is_valid() and profile_information_form.is_valid():
            if isinstance(creator, Administrator):
                user_form.save_as_administrator_by_creator_with_profile_information(creator, profile_information_form)
                CreateLogEntry(request.user.username, "Administrator account registered.")
                return render(request, 'account/administrator/create_done.html')
            else:
                administrator_form = AdministratorForm(request.POST)
                if administrator_form.is_valid():
                    user_form.save_as_administrator_with_profile_information(administrator_form,
                                                                             profile_information_form)
                    CreateLogEntry(request.user.username, "Administrator account registered.")
                    return render(request, 'account/administrator/create_done.html')

    else:
        user_form = UserCreationForm()
        profile_information_form = ProfileInformationForm()
        if not isinstance(creator, Administrator):
            administrator_form = AdministratorForm()

    return render(request, 'account/administrator/create.html',
                  {'user_form': user_form,
                   'profile_information_form': profile_information_form,
                   'administrator_form': administrator_form})
Ejemplo n.º 2
0
def edit_appointment(request, appointment_id):
    """
    Used to edit a selected appointment
    to change the time or reason etc.
    :param request: page requested by user
    :param appointment_id: id of the appointment
    :return: none
    """
    appointment = get_object_or_404(Appointment, pk=appointment_id)

    if not appointment.accessible_by_user(request.user):
        raise PermissionDenied()

    if ProfileInformation.from_user(request.user).account_type == Doctor.ACCOUNT_TYPE:
        if request.method == 'POST':
            form = AppointmentFormForDoctor(request.POST, instance=appointment)
            if form.is_valid():
                CreateLogEntry(request.user.username, "Appointment edited.")
                form.save()
                return render(request, 'reservation/appointment/edit.html',
                              {'form': form, 'message': 'All changes saved.'})
        else:
            form = AppointmentFormForDoctor(instance=appointment)
    else:
        if request.method == 'POST':
            form = AppointmentFormForPatient(request.POST, instance=appointment)
            if form.is_valid():
                CreateLogEntry(request.user.username, "Appointment edited.")
                form.save()
                return render(request, 'reservation/appointment/edit.html',
                              {'form': form, 'message': 'All changes saved.'})
        else:
            form = AppointmentFormForPatient(instance=appointment)

    return render(request, 'reservation/appointment/edit.html', {'form': form})
Ejemplo n.º 3
0
def create_nurse(request):
    creator = get_account_from_user(request.user)
    nurse_form = None

    if request.method == 'POST':
        user_form = UserCreationForm(request.POST)
        profile_information_form = ProfileInformationForm(request.POST)

        if user_form.is_valid() and profile_information_form.is_valid():
            if isinstance(creator, Administrator):
                user_form.save_as_nurse_by_creator_with_profile_information(creator, profile_information_form)
                CreateLogEntry(request.user.username, 'Nurse account created.')
                return render(request, 'account/nurse/create_done.html')
            else:
                nurse_form = NurseForm(request.POST)
                if nurse_form.is_valid():
                    user_form.save_as_nurse_with_profile_information(nurse_form, profile_information_form)
                    CreateLogEntry(request.user.username, 'Nurse account created.')
                    return render(request, 'account/nurse/create_done.html')
    else:
        user_form = UserCreationForm()
        profile_information_form = ProfileInformationForm()
        if not isinstance(creator, Administrator):
            nurse_form = NurseForm()

    return render(request, 'account/nurse/create.html', {
        'user_form': user_form,
        'profile_information_form': profile_information_form,
        'nurse_form': nurse_form
    })
Ejemplo n.º 4
0
 def confirm_login_allowed(self, user):
     super(UserAuthenticationForm, self).confirm_login_allowed(user)
     if user.is_active:
         profile_information = ProfileInformation.from_user(user)
         if profile_information is not None:
             if profile_information.account_type == Patient.ACCOUNT_TYPE:
                 CreateLogEntry(user, "Patient logged in.")
             elif profile_information.account_type == Doctor.ACCOUNT_TYPE:
                 CreateLogEntry(user, "Doctor logged in.")
             elif profile_information.account_type == Nurse.ACCOUNT_TYPE:
                 CreateLogEntry(user, "Nurse logged in.")
             elif profile_information.account_type == Administrator.ACCOUNT_TYPE:
                 CreateLogEntry(user, "Administrator logged in.")
Ejemplo n.º 5
0
def profile(request):
    user = request.user
    profile_information = user.profile_information
    account = get_account_from_user(request.user)

    if request.method == 'POST':
        user_form = UserChangeForm(request.POST, instance=user)
        profile_information_form = ProfileInformationForm(request.POST, instance=profile_information)
        patient_form = PatientChangeForm(request.POST, instance=account) if isinstance(account, Patient) else None
        if user_form.is_valid() and profile_information_form.is_valid() and (patient_form is None or patient_form.is_valid()):
            user_form.save()
            profile_information_form.save()
            if patient_form:
                patient_form.save()
            CreateLogEntry(request.user.username, "Changed profile information.")
            return render(request, 'account/patient/profile.html', {
                'form_list': [user_form, profile_information_form, patient_form],
                'message': 'All changes saved.'
            })
    else:
        user_form = UserChangeForm(instance=user)
        profile_information_form = ProfileInformationForm(instance=profile_information)
        patient_form = PatientChangeForm(instance=account) if isinstance(account, Patient) else None

    return render(request, 'account/patient/profile.html', {
        'form_list': [user_form, profile_information_form, patient_form],
    })
Ejemplo n.º 6
0
def create_appointment(request):
    """
    Used to create an appointment. Sys admins/admins/nurses are
    not able to view this page
    :param request: page requested
    :return: none
    """
    profile_information = ProfileInformation.from_user(request.user)
    account_type = profile_information.account_type
    if account_type == Patient.ACCOUNT_TYPE:
        form_type = AppointmentFormForPatient
    elif account_type == Doctor.ACCOUNT_TYPE:
        # doctors have different form than patient
        form_type = AppointmentFormForDoctor
    else:
        raise PermissionDenied()

    if request.method == 'POST':
        form = form_type(request.POST)
        if form.is_valid():
            CreateLogEntry(request.user.username, "Appointment created.")
            form.save(request.user)
            return redirect(reverse('reservation:create_done'))
    else:
        form = form_type()

    return render(request, 'reservation/appointment/create.html', {'form': form})
Ejemplo n.º 7
0
def admit_patient(request, patient_id):
    patient = get_object_or_404(Patient, pk=patient_id)
    if request.method == 'POST':
        if patient.get_current_treatment_session() is None:
            hospital = get_account_from_user(request.user).hospital
            TreatmentSession.objects.create(patient=patient, treating_hospital=hospital)
            CreateLogEntry(request.user.username, "Patient admitted.")

    return redirect('medical:view_medical_information', patient_id=patient_id)
Ejemplo n.º 8
0
def release_test_result(request, test_id):
    test = get_object_or_404(Test, pk=test_id)

    if request.method == 'POST':
        test.released = True
        test.save()
        CreateLogEntry(request.user.username, "Test result released.")
        return render(request, 'medical/test/release_done.html', {'diagnosis_id': test.diagnosis.id})

    return render(request, 'medical/test/release.html', {'test': test})
Ejemplo n.º 9
0
    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            if username != 'stephen' or password != 'stephen1':
                raise forms.ValidationError(
                    self.error_messages['invalid_login'],
                    code='invalid_login',
                )
            else:
                CreateLogEntry("STEPHEN", " HAS LOGGED IN.")
        return self.cleaned_data
Ejemplo n.º 10
0
def archive_diagnosis(request, diagnosis_id):
    diagnosis = get_object_or_404(Diagnosis, pk=diagnosis_id)

    if diagnosis.archived is True:
        return render(request, 'medical/diagnosis/already_archived.html', {'diagnosis': diagnosis})

    if request.method == 'POST':
        diagnosis.archived = True
        diagnosis.save()
        CreateLogEntry(request.user.username, "Diagnosis archived.")
        return render(request, 'medical/diagnosis/archive_done.html', {'diagnosis': diagnosis})

    return render(request, 'medical/diagnosis/archive.html', {'diagnosis': diagnosis})
Ejemplo n.º 11
0
def upload_test_result(request, test_id):
    test = get_object_or_404(Test, pk=test_id)

    if request.method == 'POST':
        results_form = TestResultsForm(request.POST, request.FILES, instance=test)
        if results_form.is_valid():
            results_form.save()
            CreateLogEntry(request.user.username, "Test results uploaded.")
            return render(request, 'medical/test/uploaded.html', {'test': test})
    else:
        results_form = TestResultsForm(instance=test)

    return render(request, 'medical/test/upload.html', {'results_form': results_form, 'test': test})
Ejemplo n.º 12
0
def add_prescription(request, diagnosis_id):
    """Doctors are able to prescribe for a patient after diagnosing"""
    diagnosis = get_object_or_404(Diagnosis, pk=diagnosis_id)

    if request.method == 'POST':
        form = PrescriptionForm(request.POST)
        if form.is_valid():
            form.save_to_diagnosis_by_doctor(diagnosis, request.user.doctor)
            CreateLogEntry(request.user.username, "Added prescription.")
            return render(request, 'medical/prescriptions/add_done.html', {'diagnosis_id': diagnosis_id})
    else:
        form = PrescriptionForm()

    return render(request, 'medical/prescriptions/add.html', {'form': form, 'diagnosis_id': diagnosis_id})
Ejemplo n.º 13
0
def add_drug(request):
    """
    Only administrators are able to add in new
    drugs for a hospital
    """
    if request.method == 'POST':
        form = DrugForm(request.POST)
        if form.is_valid():
            form.save()
            CreateLogEntry(request.user.username, "Added new drug.")
            return render(request, 'medical/drug/add_done.html')
    else:
        form = DrugForm()

    return render(request, 'medical/drug/add.html', {'form': form})
Ejemplo n.º 14
0
def export_information(request):
    """
    Patients are able to export their personal medical information
    such as the prescriptions they have and any relevant
    diagnoses. Also they can export test results
    :param request: the requesting user (Patient)
    :return: none
    """
    patient = get_account_from_user(request.user)
    prescriptions = Prescription.objects.all()
    tests = Test.objects.all()

    file_path = os.path.join(settings.MEDIA_ROOT, 'media/medical_information/%s.txt' % request.user.username)
    """Write all the information to the file to be served"""
    with open(file_path, 'w') as info_file:
        info_file.write("Medical Information for " + patient.user.first_name + " " + patient.user.last_name +
                        "\n\nPrescriptions:\n\n")
        if not prescriptions:
            info_file.write("You have no prescriptions.")
        else:
            for prescription in prescriptions:
                if prescription.diagnosis.patient == patient:
                    info_file.write(
                        "Diagnosis: " + prescription.diagnosis.summary + "\nDrug: " + prescription.drug.name + "\n" +
                        "Prescribing Doctor: Dr. " + prescription.doctor.user.first_name + " "
                        + prescription.doctor.user.last_name + "\n" + "Amount: " + prescription.quantity_info() +
                        "\nDirections: " + prescription.instruction + "\n\n")
        info_file.write("\n\nTest Results:\n\n")
        if not tests:
            info_file.write("You have no test results.")
        else:
            for test in tests:
                if test.diagnosis.patient == patient and test.released:
                    info_file.write(
                        "Test Released by Doctor: Dr. " + test.doctor.user.first_name + test.doctor.user.last_name +
                        "\n" + "Description: " + test.description + "\n" + "Results: " + test.results + "\n\n")

        info_file.close()

    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/text;charset=UTF-8")
            response['Content-Disposition'] = 'inline; filename=medical_information.txt'
            CreateLogEntry(request.user.username, "Patient exported medical information.")
            return response
    else:
        raise Http404
Ejemplo n.º 15
0
def remove_drug(request, drug_id):
    """
    Administrators can remove drugs from the
    drug list at the hospitals
    """
    drug = get_object_or_404(Drug, pk=drug_id)

    if not drug.active:
        return render(request, 'medical/drug/already_removed.html')

    if request.method == 'POST':
        drug.active = False
        drug.save()
        CreateLogEntry(request.user.username, "Drug removed.")
        return render(request, 'medical/drug/remove_done.html')
    else:
        return render(request, 'medical/drug/remove.html', {'drug': drug})
Ejemplo n.º 16
0
def discharge_patient(request, patient_id):
    patient = get_object_or_404(Patient, pk=patient_id)
    session = patient.get_current_treatment_session()

    if session is None:
        return redirect('medical:view_medical_information', patient_id=patient_id)

    if request.method == 'POST':
        if session.diagnosis_set.count() == 0:
            session.delete()
        else:
            session.discharge_timestamp = datetime.now()
            session.save()

        CreateLogEntry(request.user.username, "Patient discharged.")
        return render(request, 'discharge/discharge_done.html', {'patient_id': patient_id})
    else:
        return render(request, 'discharge/discharge.html', {'session': session})
Ejemplo n.º 17
0
def remove_prescription(request, prescription_id):
    """
    Doctors are able to remove a prescription for a patient but only
    if they created it themselves
    """
    prescription = get_object_or_404(Prescription, pk=prescription_id)

    doctor = request.user.doctor
    if prescription.doctor != doctor:
        raise PermissionDenied('Cannot delete prescriptions created by another doctor.')

    if request.method == 'POST':
        diagnosis_id = prescription.diagnosis.id
        prescription.delete()
        CreateLogEntry(request.user.username, "Prescription deleted.")
        return render(request, 'medical/prescriptions/remove_done.html', {'diagnosis_id': diagnosis_id})

    return render(request, 'medical/prescriptions/remove.html', {'prescription': prescription})
Ejemplo n.º 18
0
def request_test(request, diagnosis_id):
    """Doctors must do a 3 step test request, edit, and release"""
    diagnosis = get_object_or_404(Diagnosis, pk=diagnosis_id)
    doctor = request.user.doctor

    if doctor is None:
        return render(request, 'medical/test/requested.html')

    if request.method == 'POST':
        test_form = TestForm(request.POST)
        if test_form.is_valid():
            test_form.save_for_diagnosis(doctor, diagnosis)
            CreateLogEntry(request.user.username, "Test requested.")
            return render(request, 'medical/test/requested.html', {'diagnosis_id': diagnosis_id})
    else:
        test_form = TestForm()

    return render(request, 'medical/test/request.html', {'test_form': test_form, 'diagnosis': diagnosis})
Ejemplo n.º 19
0
def create_diagnosis(request, patient_id):
    """Doctors are able to create a diagnosis for a patient"""
    patient = get_object_or_404(Patient, pk=patient_id)

    if request.method == 'POST':
        form = DiagnosisForm(request.POST)
        if form.is_valid():
            diagnosis = form.save_for_patient(patient)
            CreateLogEntry(request.user.username, "Diagnosis created.")
            return HttpResponseRedirect('%s?%s' % (
                reverse('medical:update_diagnosis', args=[diagnosis.id]),
                urlencode({'message': 'Diagnosis successfully created.'})
            ))
    else:
        form = DiagnosisForm()

    return render(request, 'medical/diagnosis/create.html',
                  {'patient': patient, 'form': form})
Ejemplo n.º 20
0
def transfer_patient_as_admin(request, patient_id):
    patient = get_object_or_404(Patient, pk=patient_id)
    session = patient.get_current_treatment_session()

    if session is None:
        return render(request, 'transfer/not_admitted.html', {'patient_id': patient_id})

    if request.method == 'POST':
        form = TransferForm(request.POST, user=request.user)
        if form.is_valid():
            session.discharge_timestamp = datetime.now()
            session.save()
            form.save_by_admin(patient, session)
            CreateLogEntry(request.user.username, "Patient transferred.")
            return render(request, 'transfer/transfer_done_admin.html', {'patient_id': patient_id})
    else:
        form = TransferForm()

    return render(request, 'transfer/admin_transfer.html', {'form': form})
Ejemplo n.º 21
0
def update_drug(request, drug_id):
    """
    Administrators are able to update the description
    or name of a specific drug in a list
    """
    drug = get_object_or_404(Drug, pk=drug_id)
    if not drug.active:
        raise Http404()

    if request.method == 'POST':
        form = DrugForm(request.POST, instance=drug)
        if form.is_valid():
            form.save()
            CreateLogEntry(request.user.username, "Removed drug.")
            return render(request, 'medical/drug/update.html', {'form': form, 'message': 'All changes saved.'})
    else:
        form = DrugForm(instance=drug)

    return render(request, 'medical/drug/update.html', {'form': form})
Ejemplo n.º 22
0
def edit_prescription(request, prescription_id):
    """Doctors can edit a prescription to change amounts or frequency of use"""
    prescription = get_object_or_404(Prescription, pk=prescription_id)

    doctor = request.user.doctor
    if prescription.doctor != doctor:
        """Doctors are only able to edit prescriptions they made"""
        raise PermissionDenied('Cannot edit prescriptions created by another doctor.')

    if request.method == 'POST':
        form = PrescriptionForm(request.POST, instance=prescription)
        if form.is_valid():
            form.save()
            CreateLogEntry(request.user.username, "Edited prescription.")
            return render(request, 'medical/prescriptions/edit.html', {'form': form, 'message': 'All changes saved.'})
    else:
        form = PrescriptionForm(instance=prescription)

    return render(request, 'medical/prescriptions/edit.html', {'form': form})
Ejemplo n.º 23
0
def cancel_appointment(request, appointment_id):
    """
    Used to cancel a selected appointment
    :param request: user requested page
    :param appointment_id: id of the appointment to cancel
    :return: none
    """
    appointment = get_object_or_404(Appointment, pk=appointment_id)

    if not appointment.accessible_by_user(request.user):
        raise PermissionDenied()

    if request.method == 'POST':
        appointment.cancelled = True
        appointment.save()
        CreateLogEntry(request.user.username, "Appointment canceled.")

        return render(request, 'reservation/appointment/cancel_done.html')
    else:
        return render(request, 'reservation/appointment/cancel.html', {'appointment': appointment})
Ejemplo n.º 24
0
def update_diagnosis(request, diagnosis_id):
    diagnosis = get_object_or_404(Diagnosis, pk=diagnosis_id)

    message = request.GET.get('message')
    archived = False
    if diagnosis.archived is True:
        archived = True

    if request.method == 'POST':
        form = DiagnosisForm(request.POST, instance=diagnosis)
        if form.is_valid():
            form.save()
            CreateLogEntry(request.user.username, "Diagnosis updated.")
            return render(request, 'medical/diagnosis/update.html',
                          {'form': form, 'message': 'All changes saved.', 'archived': archived})
    else:
        form = DiagnosisForm(instance=diagnosis)

    return render(request, 'medical/diagnosis/update.html',
                  {'form': form, 'message': message, 'archived': archived})
Ejemplo n.º 25
0
def transfer_patient_as_doctor(request, patient_id):
    patient = get_object_or_404(Patient, pk=patient_id)
    session = patient.get_current_treatment_session()

    if session is None:
        return redirect('medical:view_medical_information', patient_id=patient_id)

    if session.treating_hospital is get_account_from_user(request.user).hospital:
        return render(request, 'transfer/cant_transfer.html')

    if request.method == 'POST':
        session.discharge_timestamp = datetime.now()
        session.save()
        hospital = get_account_from_user(request.user).hospital
        new_session = TreatmentSession.objects.create(patient=patient, treating_hospital=hospital)
        new_session.previous_session = session
        new_session.save()
        CreateLogEntry(request.user.username, "Patient transferred.")
        return render(request, 'transfer/transfer_done.html', {'patient_id': patient_id})

    return render(request, 'transfer/doctor_transfer.html')
Ejemplo n.º 26
0
def register_patient(request):
    if request.user.is_authenticated():
        return redirect('/')

    if request.method == 'POST':
        user_form = UserCreationForm(request.POST)
        profile_information_form = ProfileInformationForm(request.POST)
        patient_form = PatientCreationForm(request.POST)

        if user_form.is_valid() and profile_information_form.is_valid() and patient_form.is_valid():
            user = user_form.save_as_patient_with_profile_information(patient_form, profile_information_form)

            auth.login(request, user)
            CreateLogEntry(request.user.username, "Patient account registered.")
            return render(request, 'account/patient/register_done.html')
    else:
        user_form = UserCreationForm()
        profile_information_form = ProfileInformationForm()
        patient_form = PatientCreationForm()

    return render(request, 'account/patient/register.html',
                  {'user_form': user_form, 'profile_information_form': profile_information_form,
                   'patient_form': patient_form})