def view_patient_disease_groups(patient_id, disease_group_id=None): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) disease_group = None if disease_group_id is None: form = AddPatientDiseaseGroupForm() # TODO permissions # TODO sort order form.disease_group_id.choices = [(x.id, x.name) for x in DiseaseGroup.query.all() if not patient.in_disease_group(x)] # TODO check if already in disease group if request.method == 'POST': if not patient.can_edit(current_user): abort(403) if form.validate(): disease_group_id = form.disease_group_id.data disease_group = DiseaseGroup.query.get_or_404(disease_group_id) try: add_patient_to_disease_group(patient, disease_group) db.session.commit() except IntegrityError: db.session.rollback() flash('Saved.', 'success') return redirect(url_for('patients.view_patient_disease_groups', patient_id=patient.id)) else: disease_group = DiseaseGroupPatient.query\ .filter(DiseaseGroupPatient.patient == patient)\ .filter(DiseaseGroupPatient.id == disease_group_id)\ .first_or_404() if not disease_group.can_edit(disease_group): abort(403) form = EditPatientDiseaseGroupForm(obj=disease_group) if form.validate_on_submit(): disease_group.is_active = form.is_active.data db.session.commit() flash('Saved.', 'success') return redirect(url_for('patients.view_patient_disease_groups', patient_id=patient.id)) context = dict( patient=patient, patient_data=get_patient_data(patient), form=form, disease_group=disease_group, ) return render_template('patient/disease_groups.html', **context)
def view_pathology_list(patient_id): patient = Patient.query.get_or_404(patient_id) context = dict( patient=patient, patient_data=get_patient_data(patient), ) return render_template('patient/pathology.html', **context)
def view_diagnosis(patient_id, disease_group_id): patient = Patient.query.get_or_404(patient_id) disease_group = DiseaseGroup.query.get_or_404(disease_group_id) if not patient.can_view(current_user): abort(403) context = dict(patient=patient, patient_data=get_patient_data(patient), disease_group=disease_group) return render_template("patient/diagnosis.html", **context)
def view_result_graph(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) form = ResultGraphForm() form.result_code.choices = get_result_code_choices() context = dict(patient=patient, patient_data=get_patient_data(patient), form=form) return render_template("patient/results_graph.html", **context)
def view_result_definitions(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) result_group_definitions = ResultGroupDefinition.query.order_by(ResultGroupDefinition.name).all() context = dict( patient=patient, patient_data=get_patient_data(patient), result_group_definitions=result_group_definitions ) return render_template("patient/result_definitions.html", **context)
def view_demographics_list(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) facility_patients = get_facility_patients(patient) context = dict( patient=patient, patient_data=get_patient_data(patient), facility_patients=facility_patients ) return render_template('patient/demographics.html', **context)
def view_result_list(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) context = dict(patient=patient, patient_data=get_patient_data(patient)) form = SelectResultGroupForm(formdata=request.args, csrf_enabled=False) result_group_choices = get_result_group_choices() result_group_choices.insert(0, ("", "All")) form.result_group_definition_id.choices = result_group_choices if form.validate(): result_group_definition_id = form.result_group_definition_id.data else: result_group_definition_id = None query = ( Result.query.join(Result.result_group) .join(Result.result_definition) .join(ResultGroup.result_group_definition) .join(ResultGroup.facility) .filter(ResultGroup.patient == patient) ) if result_group_definition_id is not None: result_group_definition = ResultGroupDefinition.query.get_or_404(result_group_definition_id) query = query.filter(ResultGroup.result_group_definition == result_group_definition) context["result_group_definition"] = result_group_definition query, ordering = order_query(query, LIST_ORDER_BY, "date", DESCENDING) query = query.order_by(desc(ResultGroup.date), ResultGroupDefinition.name, ResultDefinition.name) pagination = paginate_query(query, default_per_page=50) results = pagination.items context.update(dict(pagination=pagination, ordering=ordering, results=results, form=form)) return render_template("patient/results_list.html", **context)
def delete_patient(patient_id): patient = Patient.query.get_or_404(patient_id) # TODO probably shouldn't be able to delete a patient who belongs to non-editable units if not patient.can_edit(current_user): abort(403) form = DeleteForm() if form.validate_on_submit(): db.session.delete(patient) db.session.commit() flash('Patient deleted.', 'success') return redirect(url_for('patients.view_patient_list')) else: context = dict( patient=patient, patient_data=get_patient_data(patient) ) return render_template('patient/delete.html', **context)
def view_patient_units(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) form = PatientUnitForm() # TODO permissions # TODO sort order form.unit_id.choices = [(x.id, x.name) for x in Unit.query.all() if not patient.in_unit(x)] # TODO check if already in unit if request.method == 'POST': if not patient.can_edit(current_user): abort(403) if form.validate(): unit_id = form.unit_id.data unit = Unit.query.get_or_404(unit_id) try: add_patient_to_unit(patient, unit) db.session.commit() except IntegrityError: db.session.rollback() flash('Saved.', 'success') return redirect(url_for('patients.view_patient_units', patient_id=patient.id)) context = dict( patient=patient, patient_data=get_patient_data(patient), form=form, ) return render_template('patient/units.html', **context)
def view_patient_list(): if not current_user.has_view_patient_permission: abort(403) form = get_patient_search_form(current_user) query = build_patient_search_query(current_user, form) ordering = Ordering(form.order_by.data, form.order_direction.data) pagination = paginate_query(query, default_per_page=PER_PAGE_DEFAULT) patients = pagination.items patients = [(x, get_patient_data(x)) for x in patients] context = dict( patients=patients, form=form, pagination=pagination, ordering=ordering, per_page_choices=PER_PAGE_CHOICES, ) return render_template('patients.html', **context)
def view_result_table(patient_id): patient = Patient.query.get_or_404(patient_id) if not patient.can_view(current_user): abort(403) form = ResultTableForm(formdata=request.args, csrf_enabled=False) form.result_codes.choices = get_result_code_choices() result_codes = form.result_codes.data # Default result codes if not result_codes: result_codes = ["CREATININE"] result_definitions = ResultDefinition.query.filter(ResultDefinition.code.in_(result_codes)).all() result_definition_dict = {x.code: x for x in result_definitions} # Remove invalid result codes result_codes = [x for x in result_codes if x in result_definition_dict] form.result_codes.data = result_codes if result_codes: # Sorting is done later to keep item grouping consistent results = ( Result.query.join(Result.result_definition) .join(Result.result_group) .filter(ResultDefinition.code.in_(result_codes)) .filter(ResultGroup.patient == patient) .order_by(ResultGroup.id) .order_by(Result.id) .all() ) else: results = [] table = ResultTable(result_codes) table.add_all(results) # Build list of sortable columns sort_columns = ["date", "source"] sort_columns.extend([RESULT_CODE_SORT_PREFIX + x for x in result_codes]) ordering = ordering_from_request(sort_columns, "date", DESCENDING) sort_column = ordering.column reverse = ordering.direction == DESCENDING if sort_column.startswith(RESULT_CODE_SORT_PREFIX): sort_result_code = sort_column.split(RESULT_CODE_SORT_PREFIX)[1] table.sort_by_result_code(sort_result_code, reverse) elif sort_column == "source": table.sort_by_facility(reverse) else: table.sort_by_date(reverse) result_columns = [(result_definition_dict[x], RESULT_CODE_SORT_PREFIX + x) for x in result_codes] context = dict( patient=patient, patient_data=get_patient_data(patient), table=table, ordering=ordering, form=form, result_columns=result_columns, ) return render_template("patient/results_table.html", **context)