Created
April 9, 2019 13:54
-
-
Save lksudha/0ded05c814292104bbaecf0ae59dd2d9 to your computer and use it in GitHub Desktop.
custom manager for a doctor model in a EHR for use by doctor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # stdlib imports | |
| import logging | |
| import traceback | |
| import json | |
| from datetime import datetime | |
| from dateutil import relativedelta | |
| from sys import exc_info | |
| # django imports | |
| from django.db import models | |
| from django.apps import apps | |
| from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, ValidationError, FieldDoesNotExist | |
| from django.db import DatabaseError, transaction, IntegrityError | |
| from django.db.models import Q | |
| from django.forms import model_to_dict | |
| from indivo.lib.indivo_exception import IndivoException | |
| from indivo.lib.utils import to_json | |
| from indivo.models.accounts import Account | |
| from indivo.models.records_and_documents import Record | |
| from indivo.models.doctor import DoctorAppointment | |
| from prescription.models import PrescriptionEntry, Prescription | |
| from rxprofile.models import DoctorProfile, ParentHistory | |
| # from doctor.models import AppointmentVital | |
| from complaint.models import SimpleComplaint | |
| from .utils import (get_complaints, get_diagnosis, get_doctor_notes, get_examination_findings, get_investigation, | |
| get_provisional_diagnosis, get_visit_history_item, get_lab_tests) | |
| from complaint.utils import get_simple_complaint_id, get_simple_complaint_appointment | |
| # from common.utils import get_appointment_vital | |
| # from doctor.models import OpReview | |
| logger = logging.getLogger(__name__) | |
| class OpReviewManager(models.Manager): | |
| def create_op_review(self, account, input_data): | |
| result = {} | |
| try: | |
| with transaction.atomic(): | |
| patient = Account.objects.get(account__email=input_data.get('patient_email')) | |
| if input_data.get('follow_up_date'): | |
| follow_up_date = datetime.strptime(input_data.get('follow_up_date'), '%Y-%m-%d') | |
| else: | |
| follow_up_date = None | |
| op_review = self.create(doctor_account=account, patient_account=patient, | |
| appointment_id=input_data.get('appointment_id'), | |
| complaint_id=input_data.get('complaint_id'), | |
| notes=input_data.get('notes'), findings=input_data.get('findings'), | |
| provisional_diagnosis=input_data.get('provisional_diagnosis'), | |
| investigation=input_data.get('investigation'), | |
| diagnosis=input_data.get('diagnosis'), | |
| prescription_id=input_data.get('prescription_id'), | |
| advice=input_data.get('advice'), | |
| lab_tests=input_data.get('lab_tests'), | |
| image_attachments=input_data.get('image_attachments'), | |
| others=input_data.get('others'), follow_up_date=follow_up_date, | |
| follow_up_text=input_data.get('follow_up_text')) | |
| except (KeyError, ValueError, DatabaseError, Account.DoesNotExist) as e: | |
| print(e) | |
| logger.exception(e) | |
| result = {'error': 'unable to save review data'} | |
| except ValidationError as e: | |
| print(e) | |
| logger.exception(e) | |
| result = {'error': 'duplicate data or data not valid'} | |
| else: | |
| result = op_review | |
| finally: | |
| return result | |
| def _get_complaint_vitals(self, op_review, appointment_id): | |
| if op_review.complaint: | |
| complaint_data = get_simple_complaint_id(op_review.complaint.id) | |
| else: | |
| complaint_data = get_simple_complaint_appointment(appointment_id) | |
| if op_review: | |
| vitals_id = self._get_vitals_data(op_review) | |
| else: | |
| appointment = DoctorAppointment.objects.get(id=appointment_id) | |
| vitals_id = appointment.appointment_vital.get().vitals | |
| return (complaint_data, vitals_id) | |
| def get_op_review(self, appointment_id): | |
| try: | |
| op_review = self.prefetch_related('complaint').prefetch_related('prescription')\ | |
| .get(appointment_id=appointment_id) | |
| if op_review.prescription: | |
| # obj = Prescription.objects.get(id=op_review.prescription) | |
| prescription = PrescriptionEntry.objects.get_prescription_entries(op_review.prescription) | |
| else: | |
| prescription = None | |
| complaint_data, vitals_id = self._get_complaint_vitals(op_review, appointment_id) | |
| op_review_data = { | |
| 'appointment_id': op_review.appointment_id, 'complaint': complaint_data, 'notes': op_review.notes, | |
| 'findings': op_review.findings, 'provisional_diagnosis': op_review.provisional_diagnosis, | |
| 'investigation': op_review.investigation, 'diagnosis': op_review.diagnosis, | |
| 'prescription': prescription, 'advice': op_review.advice, | |
| 'others': op_review.others, 'follow_up_date': op_review.follow_up_date, | |
| 'follow_up_text': op_review.follow_up_text, | |
| 'lab_tests': op_review.lab_tests, 'image_attachments': op_review.image_attachments, | |
| 'vitals': vitals_id | |
| } | |
| # except self.model.related_field.RelatedObjectDoesNotExist: | |
| # pass | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.exception(e) | |
| result = {'error': 'unable to retrieve object'} | |
| raise IndivoException | |
| except (PrescriptionEntry.DoesNotExist, Prescription.DoesNotExist) as e: | |
| complaint_data, vitals_id = self._get_complaint_vitals(op_review, appointment_id) | |
| result = { | |
| 'appointment_id': op_review.appointment_id, 'complaint': complaint_data, 'notes': op_review.notes, | |
| 'findings': op_review.findings, 'provisional_diagnosis': op_review.provisional_diagnosis, | |
| 'investigation': op_review.investigation, 'diagnosis': op_review.diagnosis, | |
| 'prescription': None, 'advice': op_review.advice, | |
| 'others': op_review.others, 'follow_up_date': op_review.follow_up_date, | |
| 'follow_up_text': op_review.follow_up_text, | |
| 'lab_tests': op_review.lab_tests, 'image_attachments': op_review.image_attachments, | |
| 'vitals': vitals_id | |
| } | |
| except self.model.DoesNotExist as e: | |
| # print('no object: {}'.format(e)) | |
| try: | |
| complaint_data = get_simple_complaint_appointment(appointment_id) | |
| except self.model.DoesNotExist: | |
| result = {} | |
| else: | |
| result = {'complaint': complaint_data} | |
| finally: | |
| return result | |
| else: | |
| result = op_review_data | |
| finally: | |
| return result | |
| def update_op_review(self, input_data): | |
| result = {} | |
| try: | |
| op_review = self.prefetch_related('complaint').prefetch_related('prescription')\ | |
| .get(appointment_id=input_data['appointment_id']) | |
| prescription = PrescriptionEntry.objects.get_prescription_entries(op_review.prescription) | |
| if input_data.get('notes'): | |
| op_review.notes = input_data.get('notes') | |
| if input_data.get('findings'): | |
| op_review.findings = input_data.get('findings') | |
| if input_data.get('provisional_diagnosis'): | |
| op_review.provisional_diagnosis = input_data.get('provisional_diagnosis') | |
| if input_data.get('investigation'): | |
| op_review.investigation = input_data.get('investigation') | |
| if input_data.get('diagnosis'): | |
| op_review.diagnosis = input_data.get('diagnosis') | |
| if input_data.get('advice'): | |
| op_review.food_instructions = input_data.get('advice') | |
| if input_data.get('others'): | |
| op_review.others = input_data.get('others') | |
| if input_data.get('follow_up_date'): | |
| follow_up_date = datetime.strptime(input_data.get('follow_up_date'), '%Y-%m-%d') | |
| op_review.follow_up_date = follow_up_date | |
| if input_data.get('follow_up_text'): | |
| op_review.follow_up_text = input_data.get('follow_up_text') | |
| if input_data.get('prescription_id'): | |
| op_review.prescription_id = input_data.get('prescription_id') | |
| if input_data.get('complaint_id'): | |
| op_review.complaint_id = input_data.get('complaint_id') | |
| if input_data.get('lab_tests'): | |
| op_review.complaint_id = input_data.get('lab_tests') | |
| op_review.save() | |
| op_review_data = { | |
| 'appointment_id': op_review.appointment_id, 'complaint': {'issue': op_review.complaint.issue, | |
| 'medicine_given': op_review.complaint.medicine_given}, | |
| 'notes': op_review.notes, | |
| 'findings': op_review.findings, 'provisional_diagnosis': op_review.provisional_diagnosis, | |
| 'investigation': op_review.investigation, 'diagnosis': op_review.diagnosis, | |
| 'prescription': prescription, 'advice': op_review.advice, | |
| 'others': op_review.others, 'follow_up_date': op_review.follow_up_date, 'lab_tests': op_review.lab_tests | |
| } | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| # print(e) | |
| logger.exception(e) | |
| result = {'error': 'unable to save'} | |
| except self.model.DoesNotExist as e: | |
| # print(e) | |
| logger.exception(e) | |
| result = {'error': 'unable to retrieve existing overview entry'} | |
| else: | |
| result = op_review_data | |
| finally: | |
| return result | |
| def get_historical_items(self, doctor_account, patient_email): | |
| complaints = list() | |
| notes = list() | |
| findings = list() | |
| provisional_diagnosis = list() | |
| investigations = list() | |
| diagnosis = list() | |
| try: | |
| patient_account = Account.objects.get(account__email=patient_email) | |
| op_reviews = self.filter(doctor_account=doctor_account, patient_account=patient_account)\ | |
| .prefetch_related('complaint') | |
| for item in op_reviews: | |
| get_complaints(item, complaints) | |
| get_doctor_notes(item, notes) | |
| get_examination_findings(item, findings) | |
| get_provisional_diagnosis(item, provisional_diagnosis) | |
| get_investigation(item, investigations) | |
| get_diagnosis(item, diagnosis) | |
| result = {'overview_history': { | |
| 'complaints': complaints, 'doctor_notes': notes, 'examination_findings': findings, | |
| 'provisional_diagnosis': provisional_diagnosis, 'investigations': investigations, | |
| 'diagnosis': diagnosis | |
| }} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| print(e) | |
| logger.error(e) | |
| result = {'error': 'unable to get overview history'} | |
| except Account.DoesNotExist as e: | |
| logger.error(e) | |
| result = {'error': 'unable to retrieve patient'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def get_simple_visit_history(self, doctor_account, patient_email): | |
| history_items = list() | |
| try: | |
| patient_account = Account.objects.get(account__email=patient_email) | |
| op_reviews = self.filter(doctor_account=doctor_account, patient_account=patient_account)\ | |
| .prefetch_related('complaint') | |
| # print('op reviews model:{}'.format(op_reviews)) | |
| for item in op_reviews: | |
| get_visit_history_item(item, history_items) | |
| result = {'visit_history': history_items} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get visit history'} | |
| except Account.DoesNotExist as e: | |
| logger.error(e) | |
| result = {'error': 'unable to retrieve patient'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def get_simple_visit_history_mobile(self, patient_account, doctor_email): | |
| history_items = list() | |
| try: | |
| doctor_account = Account.objects.get(account__email=doctor_email) | |
| op_reviews = self.filter(doctor_account=doctor_account, patient_account=patient_account)\ | |
| .prefetch_related('complaint') | |
| # print('op reviews model:{}'.format(op_reviews)) | |
| for item in op_reviews: | |
| get_visit_history_item(item, history_items) | |
| result = {'visit_history': history_items} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get visit history'} | |
| except Account.DoesNotExist as e: | |
| logger.error(e) | |
| result = {'error': 'unable to retrieve doctor'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def get_lab_tests(self, doctor_account, patient_email): | |
| lab_tests_list = list() | |
| try: | |
| patient_account = Account.objects.get(account__email=patient_email) | |
| op_reviews = self.filter(doctor_account=doctor_account, patient_account=patient_account) | |
| for item in op_reviews: | |
| get_lab_tests(item, lab_tests_list) | |
| result = {'lab_tests': lab_tests_list} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| except Account.DoesNotExist as e: | |
| logger.error(e) | |
| else: | |
| return result | |
| @staticmethod | |
| def _get_simple_report(op_review): | |
| """ | |
| common information for simple prescription and visit history | |
| :param op_review: | |
| :return: | |
| """ | |
| try: | |
| prescription = PrescriptionEntry.objects.get_prescription_entries(op_review.prescription) | |
| advice_data = {'advice': op_review.advice, | |
| 'others': op_review.others, } | |
| result = {'diagnosis': op_review.diagnosis, 'advice': advice_data, | |
| 'follow_up_date': op_review.follow_up_date, 'follow_up_text': op_review.follow_up_text, | |
| 'prescription': prescription, 'prescription_expiry_date': op_review.prescription.expiry_date} | |
| except (PrescriptionEntry.DoesNotExist, Prescription.DoesNotExist): | |
| advice_data = {'advice': op_review.advice, | |
| 'others': op_review.others, } | |
| result = {'diagnosis': op_review.diagnosis, 'advice': advice_data, | |
| 'follow_up_date': op_review.follow_up_date, 'follow_up_text': op_review.follow_up_text, | |
| 'prescription': None, 'prescription_expiry_date': None} | |
| except (DatabaseError, KeyError, ValueError, Exception) as e: | |
| logger.error(e) | |
| # print(e) | |
| result = {'error': 'unable to get report data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_doctor_details(op_review): | |
| """ | |
| doctor details for prescription | |
| :param op_review: | |
| :return: | |
| """ | |
| try: | |
| # profile = DoctorProfile.objects.get_doctor_profile(op_review.doctor_account) | |
| profile = op_review.doctor_account.account_doctor_profile | |
| profile_data = {'full_name': op_review.doctor_account.full_name, | |
| 'registration_number': profile.registration, 'degree': profile.degree} | |
| result = {'doctor_profile': profile_data} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get report data'} | |
| except DoctorProfile.DoesNotExist as e: | |
| logger.error(e) | |
| result = {'error': 'doctor profile not present'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_doctor_notes(op_review): | |
| """ | |
| get doctor notes | |
| :param op_review: | |
| :return: | |
| """ | |
| try: | |
| notes_data = op_review.notes | |
| result = {'notes': notes_data} | |
| except (DatabaseError, KeyError, ValueError, FieldDoesNotExist) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get notes data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_lab_test_data(op_review): | |
| """ | |
| get lab test data | |
| :param op_review: | |
| :return: | |
| """ | |
| try: | |
| lab_test_data = op_review.lab_tests | |
| result = {'lab_tests': lab_test_data} | |
| except (DatabaseError, KeyError, ValueError, FieldDoesNotExist) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get lab test data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_patient_details(op_review): | |
| try: | |
| record = Record.objects.prefetch_related('demographics').get(owner_id=op_review.patient_account.id) | |
| today = datetime.today() | |
| age = relativedelta.relativedelta(today, record.demographics.bday) | |
| if age.years > 1: | |
| years = 'years' | |
| else: | |
| years = 'year' | |
| if age.months > 1: | |
| months = 'months' | |
| else: | |
| months = 'month' | |
| demographics_data = {'patient_name': '{} {}'.format(record.demographics.name_given, | |
| record.demographics.name_family), | |
| 'gender': record.demographics.gender, 'dob': record.demographics.bday, | |
| 'mr_number': record.demographics.mr_number, | |
| 'blood_group': record.demographics.blood_group, | |
| 'age': '{} {} {} {}'.format(age.years, years, age.months, months)} | |
| family_history_data = ParentHistory.objects.get_hereditary_disease(op_review.patient_account.email) | |
| result = {'patient_demographics': demographics_data, 'family_history': family_history_data} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get report data'} | |
| except Record.DoesNotExist as e: | |
| logger.error(e) | |
| result = {'error': 'patient record not present'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_additional_prescription_details(op_review): | |
| try: | |
| complaint_data = get_simple_complaint_id(op_review.complaint.id) | |
| # print(complaint_data) | |
| doctor_data = { | |
| 'complaint': complaint_data, 'diagnosis': op_review.diagnosis, 'findings': op_review.findings, | |
| 'investigation': op_review.investigation | |
| } | |
| result = doctor_data | |
| except (IndivoException, KeyError, ValueError) as e: | |
| # print(e) | |
| logger.error(e) | |
| result = {'error': 'unable to get report data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| @staticmethod | |
| def _get_vitals_data(op_review): | |
| try: | |
| vitals_id = op_review.appointment.appointment_vital.get().vitals | |
| except ObjectDoesNotExist: | |
| vitals_id = '' | |
| return vitals_id | |
| def get_visit_history_detailed(self, overview_id): | |
| try: | |
| op_review = self.prefetch_related('prescription').get(id=overview_id) | |
| final_data = self._get_simple_report(op_review) | |
| notes_data = self._get_doctor_notes(op_review) | |
| final_data.update(notes_data) | |
| lab_test_data = self._get_lab_test_data(op_review) | |
| final_data.update(lab_test_data) | |
| # try: | |
| # vitals_id = op_review.appointment.appointment_vital.get().vitals | |
| # except ObjectDoesNotExist: | |
| # vitals_id = '' | |
| vitals_id = self._get_vitals_data(op_review) | |
| vitals_data = dict(vitals=vitals_id) | |
| final_data.update(vitals_data) | |
| # print('before result') | |
| result = {'visit_history_detailed': final_data} | |
| # print(result) | |
| except (DatabaseError, KeyError, ValueError, Exception) as e: | |
| # print('in ex mgr') | |
| logger.error(e) | |
| # print(e) | |
| # print(traceback.print_exc()) | |
| result = {'error': 'unable to get patient overview data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def get_simple_prescription_report(self, overview_id): | |
| try: | |
| op_review = self.prefetch_related('prescription').prefetch_related('doctor_account').get(id=overview_id) | |
| final_data = self._get_simple_report(op_review) | |
| doctor_profile_data = self._get_doctor_details(op_review) | |
| final_data.update(doctor_profile_data) | |
| patient_data = self._get_patient_details(op_review) | |
| final_data.update(patient_data) | |
| result = {'simple_prescription': final_data} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get patient overview data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def get_detailed_prescription_report(self, overview_id): | |
| try: | |
| op_review = self.prefetch_related('prescription').prefetch_related('complaint').get(id=overview_id) | |
| final_data = self._get_simple_report(op_review) | |
| doctor_profile_data = self._get_doctor_details(op_review) | |
| final_data.update(doctor_profile_data) | |
| patient_data = self._get_patient_details(op_review) | |
| final_data.update(patient_data) | |
| notes_data = self._get_doctor_notes(op_review) | |
| final_data.update(notes_data) | |
| lab_test_data = self._get_lab_test_data(op_review) | |
| final_data.update(lab_test_data) | |
| doctor_data = self._get_additional_prescription_details(op_review) | |
| final_data.update(doctor_data) | |
| # try: | |
| # vitals_id = op_review.appointment.appointment_vital.get().vitals | |
| # except ObjectDoesNotExist: | |
| # vitals_id = '' | |
| vitals_id = self._get_vitals_data(op_review) | |
| vitals_data = dict(vitals=vitals_id) | |
| final_data.update(vitals_data) | |
| result = {'detailed_prescription': final_data} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| result = {'error': 'unable to get patient overview data'} | |
| else: | |
| pass | |
| finally: | |
| return result | |
| def attach_lab_test_image(self, overview_id, image_list): | |
| try: | |
| with transaction.atomic(): | |
| op_review = self.get(id=overview_id) | |
| op_review.image_attachments = image_list | |
| op_review.save() | |
| except (DatabaseError, KeyError, ValueError, FieldDoesNotExist) as e: | |
| # print(e) | |
| raise IndivoException(e) | |
| else: | |
| result = {'response': 'image attached successfully'} | |
| return result | |
| def get_previous_investigation_reports(self, overview_id): | |
| try: | |
| op_review = self.get(id=overview_id) | |
| previous = op_review.get_previous_by_created_at() | |
| attachments = previous.image_attachments | |
| except (self.model.DoesNotExist, KeyError, ValueError, FieldDoesNotExist, DatabaseError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return attachments | |
| class ImageManager(models.Manager): | |
| def add_profile_image(self, account, data, image_id): | |
| try: | |
| # print('in add profile') | |
| if data.get('user_type') == 'doctor' and data.get('profile_type') == 'patient': | |
| patient_account = Account.objects.get(account__email=data.get('patient_email')) | |
| created_for = patient_account | |
| else: | |
| created_for = account | |
| with transaction.atomic(): | |
| kwargs = {'account': created_for, 'category': 'profile'} | |
| defaults = {'image_id': image_id, 'creator': account} | |
| image = self.update_or_create(defaults=defaults, **kwargs) | |
| except (DatabaseError, KeyError, ValidationError, IntegrityError, FieldDoesNotExist) as e: | |
| # print(e) | |
| raise IndivoException(e) | |
| else: | |
| return {'response': 'image link saved successfully'} | |
| def get_image_link(self, account, category): | |
| try: | |
| if category == 'profile': | |
| image = self.get(account=account, category=category) | |
| image_link = image.image_id | |
| except (MultipleObjectsReturned, KeyError, ValueError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return {'image_id': image_link} | |
| def get_images(self, account, category, user_type=None, patient_email=None): | |
| try: | |
| if user_type == 'doctor': | |
| patient = Account.objects.get(account__email=patient_email) | |
| image_account = patient | |
| else: | |
| image_account = account | |
| images = self.filter(account=image_account, category=category).values_list('image_id', flat=True) | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| raise | |
| else: | |
| return images | |
| class LabTestManager(models.Manager): | |
| def add_lab_test(self, account, test_data): | |
| try: | |
| with transaction.atomic(): | |
| test = self.create(account=account, creator=account, json=test_data, status='active') | |
| result = dict(id=test.id, created_at=test.created_at, account=test.account_id, status=test.status, | |
| json=test.json) | |
| except (KeyError, DatabaseError, ValueError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return result | |
| def get_lab_test(self, account, filter_option, filter_value=None): | |
| try: | |
| if filter_option == 'all': | |
| tests = self.filter(account=account, status='active').order_by('created_at') | |
| # result = [x for x in tests] | |
| result = list() | |
| for item in tests: | |
| data = dict(id=item.id, created_at=item.created_at, account=item.account_id, status=item.status, | |
| json=item.json) | |
| result.append(data) | |
| # print(result) | |
| # print('in all') | |
| elif filter_option == 'id': | |
| obj = self.get(id=filter_value) | |
| # result = model_to_dict(obj) | |
| result = dict(id=obj.id, created_at=obj.created_at, account=obj.account_id, status=obj.status, | |
| json=obj.json) | |
| elif filter_option == 'title_like': | |
| # search = | |
| result = list() | |
| tests = self.filter(account=account, status='active', json__iregex=filter_value)\ | |
| .order_by('created_at') | |
| for obj in tests: | |
| data = dict(id=obj.id, created_at=obj.created_at, account=obj.account_id, status=obj.status, | |
| json=obj.json) | |
| result.append(data) | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return result | |
| def update_lab_test(self, account, doc_id, test_data): | |
| try: | |
| with transaction.atomic(): | |
| existing_test = self.get(id=doc_id) | |
| existing_test.status = 'void' | |
| existing_test.save() | |
| new_test = self.create(account=account, creator=account, json=test_data, status='active') | |
| result = dict(id=new_test.id, created_at=new_test.created_at, account=new_test.account_id, | |
| status=new_test.status, json=new_test.json) | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return result | |
| class AdviceManager(models.Manager): | |
| def create_advice(self, account, disease, description): | |
| try: | |
| with transaction.atomic(): | |
| advice = self.create(doctor_account=account, creator=account, disease=disease, description=description) | |
| except (DatabaseError, FieldDoesNotExist, KeyError) as e: | |
| logger.error(e) | |
| raise IndivoException(e) | |
| else: | |
| return advice | |
| def create_advice_bulk(self, account, input_data): | |
| try: | |
| for item in input_data: | |
| with transaction.atomic(): | |
| advice = self.create(doctor_account=account, creator=account, disease=item.get('disease'), | |
| description=item.get('description')) | |
| except (DatabaseError, FieldDoesNotExist, KeyError) as e: | |
| logger.error(e) | |
| raise IndivoException(e) | |
| else: | |
| return self._get_advice_list(account) | |
| def _get_advice_list(self, account): | |
| try: | |
| result = self.filter(doctor_account=account).values('id', 'disease', 'description') | |
| result_list = [x for x in result] | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| raise IndivoException(e) | |
| else: | |
| return result_list | |
| def list_advice(self, account): | |
| return self._get_advice_list(account) | |
| def update_advice(self, input_data): | |
| try: | |
| advice = self.get(id=input_data.get('id')) | |
| with transaction.atomic(): | |
| if input_data.get('disease'): | |
| advice.disease = input_data.get('disease') | |
| if input_data.get('description'): | |
| advice.description = input_data.get('description') | |
| advice.save() | |
| result = {'id': advice.id, 'disease': advice.disease, 'description': advice.description} | |
| except (DatabaseError, KeyError, ValueError) as e: | |
| logger.error(e) | |
| else: | |
| return result | |
| class AppointmentVitalManager(models.Manager): | |
| def link_appointment(self, appointment_id, vital_id): | |
| try: | |
| obj = self.create(appointment_id=appointment_id, vitals=vital_id) | |
| except (DatabaseError, KeyError, ValueError, IntegrityError) as e: | |
| raise IndivoException(e) | |
| else: | |
| return {'result': 'appointment and vital linked'} | |
| def get_vitals(self, appointment_id): | |
| try: | |
| obj = self.get(appointment_id=appointment_id) | |
| except (self.model.DoesNotExist, DatabaseError, KeyError, ValueError, IndexError) as e: | |
| raise IndivoException(e) | |
| else: | |
| result = obj.vitals | |
| return result | |
| class MedicalHistoryManager(models.Manager): | |
| def create_medical_history(self, doctor_account, patient_email, input_data): | |
| try: | |
| # print(patient_email) | |
| patient_account = Account.objects.get(account__email=patient_email) | |
| obj = self.create(patient_account=patient_account, | |
| date=input_data.get('date'), | |
| problem_type=input_data.get('problem_type'), | |
| hospital_name=input_data.get('hospital_name'), | |
| doctor_first_name=input_data.get('doctor_first_name'), | |
| doctor_last_name=input_data.get('doctor_last_name'), | |
| diagnosis=input_data.get('diagnosis'), | |
| creator=doctor_account | |
| ) | |
| except Account.DoesNotExist as e: | |
| raise | |
| except (DatabaseError, KeyError, ValueError, IndexError, FieldDoesNotExist) as e: | |
| raise | |
| else: | |
| return obj | |
| def get_medical_history(self, history_id): | |
| try: | |
| obj = self.get(id=history_id) | |
| except (self.model.DoesNotExist, KeyError, IndexError, FieldDoesNotExist) as e: | |
| raise | |
| else: | |
| return obj | |
| def list_medical_history(self, patient_email): | |
| try: | |
| patient_account = Account.objects.get(account__email=patient_email) | |
| items = self.filter(patient_account=patient_account).values('date', 'problem_type', 'hospital_name', | |
| 'doctor_first_name', 'doctor_last_name', | |
| 'diagnosis', 'id') | |
| result = [x for x in items] | |
| except (DatabaseError, KeyError, ValueError, IndexError) as e: | |
| raise | |
| else: | |
| return result | |
| def update_medical_history(self, history_id, input_data): | |
| try: | |
| obj = self.get(id=history_id) | |
| if input_data.get('date'): | |
| obj.date = input_data.get('date') | |
| if input_data.get('problem_type'): | |
| obj.problem_type = input_data.get('problem_type') | |
| if input_data.get('hospital_name'): | |
| obj.hospital_name = input_data.get('hospital_name') | |
| if input_data.get('doctor_first_name'): | |
| obj.doctor_first_name = input_data.get('doctor_first_name') | |
| if input_data.get('doctor_last_name'): | |
| obj.doctor_last_name = input_data.get('doctor_last_name') | |
| if input_data.get('diagnosis'): | |
| obj.diagnosis = input_data.get('diagnosis') | |
| obj.save() | |
| except (self.model.DoesNotExist, KeyError, IndexError, FieldDoesNotExist) as e: | |
| raise | |
| else: | |
| return obj | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment