def upload_problem_image(request, problem_id): resp = {'success': False} actor_profile = UserProfile.objects.get(user=request.user) problem = Problem.objects.get(id=problem_id) problem.authenticated = actor_profile.role in ['physician', 'admin'] problem.save() patient = problem.patient images = request.FILES image_holder = [] for dict in images: image = request.FILES[dict] patient_image = PatientImage(patient=patient, problem=problem, image=image) patient_image.save() activity = summary = ''' Physician added <u>image</u> to <u>problem</u> <b>%s</b> <br/><a href="/media/%s"> <img src="/media/%s" class="thumbnail thumbnail-custom" /></a> ''' % (problem.problem_name, patient_image.image, patient_image.image) op_add_event(request.user, patient, summary, problem) add_problem_activity(problem, request.user, activity, 'input') image_holder.append(patient_image) resp['images'] = PatientImageSerializer(image_holder, many=True).data resp['success'] = True return ajax_response(resp)
def patient_refused(request, a1c_id): a1c = AOneC.objects.get(id=a1c_id) observation = a1c.observation observation.effective_datetime = datetime.strptime( request.POST.get('date'), '%Y-%m-%d').date() observation.save() if request.POST.get('patient_refused_A1C', None): a1c.patient_refused_A1C = True a1c.save() # set problem authentication actor_profile = UserProfile.objects.get(user=request.user) set_problem_authentication_false(actor_profile, a1c.problem) summary = """ Patient refused a1c , <u>problem</u> <b>%s</b> """ % (a1c.problem.problem_name) add_problem_activity(a1c.problem, request.user, summary) resp = {} resp['a1c'] = AOneCSerializer(a1c).data resp['success'] = True return ajax_response(resp)
def add_problem_goal(request, problem_id): resp = {'success': False} actor = request.user actor_profile = UserProfile.objects.get(user=actor) problem = Problem.objects.get(id=problem_id) patient = problem.patient goal = request.POST.get('name') new_goal = Goal(patient=patient, problem=problem, goal=goal) new_goal.save() physician = request.user summary = '''Added <u> goal </u> : <b>%s</b> to <u>problem</u> : <b>%s</b>''' % ( goal, problem.problem_name) op_add_event(physician, patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity, 'output') resp['success'] = True resp['goal'] = GoalSerializer(new_goal).data return ajax_response(resp)
def add_value(request, component_id): resp = {} actor_profile = UserProfile.objects.get(user=request.user) component = ObservationComponent.objects.get(id=component_id) effective_date = datetime.strptime(request.POST.get('date'), '%Y-%m-%d').date() value = ObservationValue.objects.create(component=component, value_quantity=request.POST.get( "value", None), effective_datetime=effective_date, author=request.user) a1c = component.observation.observation_aonecs a1c.patient_refused_A1C = False a1c.todo_past_six_months = False a1c.save() resp['value'] = ObservationValueSerializer(value).data resp['success'] = True # set problem authentication set_problem_authentication_false(actor_profile, a1c.problem) summary = """ Added new a1c value <u>A1C</u> : <b>%s</b> , <u>problem</u> <b>%s</b> """ % (value.value_quantity, a1c.problem.problem_name) add_problem_activity(a1c.problem, request.user, summary) summary = "An A1C value of <b>%s</b> was entered" % (value.value_quantity) op_add_event(request.user, a1c.problem.patient, summary, a1c.problem) return ajax_response(resp)
def add_history_note(request, problem_id): resp = {'success': False} try: problem = Problem.objects.get(id=problem_id) except Problem.DoesNotExist: return ajax_response(resp) # Get params actor = request.user actor_profile = UserProfile.objects.get(user=actor) note = request.POST.get('note') physician = request.user patient = problem.patient # Save note new_note = ProblemNote.objects.create_history_note(actor, problem, note) # Save problem log activity = "Added History Note <b>{}</b>".format(note) add_problem_activity(problem, request.user, activity, 'input') # Save system log op_add_event(physician, patient, activity, problem) # https://trello.com/c/hkdbHZjw auto_generate_note_todo(actor_profile, patient, problem, request, resp) # Build response resp['success'] = True resp['note'] = ProblemNoteSerializer(new_note).data return ajax_response(resp)
def add_patient_common_problem(request, patient_id): resp = {'success': False} actor = request.user actor_profile = UserProfile.objects.get(user=actor) cproblem = request.POST.get('cproblem') problem_type = request.POST.get('type') problem = CommonProblem.objects.get(id=cproblem) if Problem.objects.filter(problem_name=problem.problem_name, concept_id=problem.concept_id, patient__id=patient_id).exists(): return ajax_response({"msg": "Problem already added"}) new_problem = Problem.objects.create_new_problem(patient_id, problem.problem_name, problem.concept_id, actor_profile) physician = request.user summary = 'Added <u>problem</u> <b>%s</b>' % problem.problem_name op_add_event(physician, new_problem.patient, summary, new_problem) activity = "Added <u>problem</u>: <b>%s</b>" % problem.problem_name add_problem_activity(new_problem, request.user, activity) resp['success'] = True resp['problem'] = ProblemSerializer(new_problem).data return ajax_response(resp)
def update_problem_status(request, problem_id): resp = {'success': False} # Different permissions for different cases # Modify this view actor_profile = UserProfile.objects.get(user=request.user) is_controlled = request.POST.get('is_controlled') == 'true' is_active = request.POST.get('is_active') == 'true' authenticated = request.POST.get('authenticated') == 'true' problem = Problem.objects.select_related("patient").get(id=problem_id) problem.is_controlled = is_controlled problem.is_active = is_active problem.authenticated = authenticated problem.save() status_labels = { 'problem_name': problem.problem_name, 'is_controlled': "controlled" if is_controlled else "not controlled", "is_active": "active" if is_active else "not_active", "authenticated": "authenticated" if authenticated else "not_authenticated", } physician = request.user summary = """Changed <u>problem</u>: <b>%(problem_name)s</b> status to : <b>%(is_controlled)s</b> ,<b>%(is_active)s</b> ,<b>%(authenticated)s</b>""" % status_labels op_add_event(physician, problem.patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity) resp['success'] = True return ajax_response(resp)
def update_goal_status(request, patient_id, goal_id): resp = {} patient = User.objects.get(id=patient_id) goal = Goal.objects.get(id=goal_id, patient=patient) is_controlled = request.POST.get('is_controlled') == 'true' accomplished = request.POST.get('accomplished') == 'true' goal.is_controlled = is_controlled goal.accomplished = accomplished goal.save() status_labels = { "goal": goal.goal, "is_controlled": "controlled" if goal.is_controlled else "not controlled", "accomplished": "accomplished" if goal.accomplished else "not accomplished", "problem": goal.problem.problem_name if goal.problem else "", } physician = request.user summary = "Change <u>goal</u>: <b>%(goal)s</b> <u>status</u>" summary += " to <b>%(is_controlled)s</b> <b>%(accomplished)s</b>" summary += " for <u>problem</u> <b>%(problem)s</b> " summary = summary % status_labels op_add_event(physician, patient, summary, goal.problem) if goal.problem: actor_profile = UserProfile.objects.get(user=request.user) add_problem_activity(goal.problem, request.user, summary, 'output') resp['success'] = True return ajax_response(resp)
def add_goal_note(request, patient_id, goal_id): resp = {} actor_profile = UserProfile.objects.get(user=request.user) goal = Goal.objects.get(id=goal_id, patient_id=patient_id) note = request.POST.get('new_note') new_note = TextNote.objects.create(note=note, by=actor_profile.role) goal.notes.add(new_note) problem_name = goal.problem.problem_name if goal.problem else "", summary = """ Added <u>note</u> <b>%s</b> for <u>goal</u>: <b>%s</b> , <u> problem </u>: <b>%s</b> """ % (note, goal.goal, problem_name) physician = request.user patient = goal.patient op_add_event(physician, patient, summary, goal.problem) if goal.problem: add_problem_activity(goal.problem, request.user, summary, 'output') resp['success'] = True resp['note'] = TextNoteSerializer(new_note).data return ajax_response(resp)
def relate_problem(request): resp = {'success': False} actor_profile = UserProfile.objects.get(user=request.user) relationship = request.POST.get('relationship') == 'true' source_id = request.POST.get('source_id', None) target_id = request.POST.get('target_id', None) source = Problem.objects.get(id=source_id) target = Problem.objects.get(id=target_id) activity = None if relationship: try: problem_relationship = ProblemRelationship.objects.get( source=source, target=target) except ProblemRelationship.DoesNotExist: problem_relationship = ProblemRelationship.objects.create( source=source, target=target) activity = '''Created Problem Relationship: <b>%s</b> effects <b>%s</b>''' % ( source.problem_name, target.problem_name) else: ProblemRelationship.objects.get(source=source, target=target).delete() activity = '''Removed Problem Relationship: <b>%s</b> effects <b>%s</b>''' % ( source.problem_name, target.problem_name) if activity: add_problem_activity(source, request.user, activity) add_problem_activity(target, request.user, activity) op_add_event(request.user, source.patient, activity) resp['success'] = True return ajax_response(resp)
def problem_relationship_auto_pinning_for_3_times_matched(): """ https://trello.com/c/TWI2l0UU If any two SNOMED CT CONCEPT_IDs are set as relationship more than 3 times then set this as a relationship for all patients who have those two problems. :return: """ print( 'Starting cron problem_relationship_auto_pinning_for_3_times_matched...' ) # Default actor for cron job actor = User.objects.filter(profile__role='admin').first() # Find all paired problem have same SNOMED CT id relationships = ProblemRelationship.objects.filter(source__concept_id__isnull=False, target__concept_id__isnull=False) \ .values('source__concept_id', 'target__concept_id').annotate(total=Count('source__concept_id')).filter( total__gte=3) # find_all_problem_relationship_more_than_3_time() print('Find all paired problem have same SNOMED CT...') print(relationships) print('') for relation in relationships: print("Processing problem relationship source: {}- target {}".format( relation['source__concept_id'], relation['target__concept_id'])) for p in User.objects.filter(profile__role='patient').all(): problem_pairs = Problem.objects.filter(patient_id=p.id).filter( concept_id__in=[ relation['source__concept_id'], relation['target__concept_id'] ]) print("Processing patient:({}) {}".format(p.id, p)) print("Number of problems in pair: {}".format( problem_pairs.count())) if problem_pairs.exists() and problem_pairs.count() == 2: source = Problem.objects.get( patient_id=p.id, concept_id=relation['source__concept_id']) target = Problem.objects.get( patient_id=p.id, concept_id=relation['target__concept_id']) if not ProblemRelationship.objects.filter( source=source, target=target).exists(): ProblemRelationship.objects.create(source=source, target=target) activity = "Created Problem Relationship(automation pinning): <b>{0}</b> effects <b>{1}</b>".format( source.problem_name, target.problem_name) # Add log add_problem_activity(source, actor, activity) add_problem_activity(target, actor, activity) op_add_event(actor, source.patient, activity) print("Activity log: {}".format(activity)) print('') print( 'Finished cron problem_relationship_auto_pinning_for_3_times_matched...' ) print('')
def add_sharing_problems(request, patient_id, sharing_patient_id, problem_id): resp = {'success': False} sharing_patient = SharingPatient.objects.get(shared_id=patient_id, sharing_id=sharing_patient_id) problem = Problem.objects.get(id=problem_id) sharing_patient.problems.add(problem) actor_profile = UserProfile.objects.get(user=request.user) activity = "Added access for patient <b>%s</b> " % sharing_patient.sharing.username add_problem_activity(problem, request.user, activity) resp = {'success': True} return ajax_response(resp)
def change_name(request, problem_id): resp = {'success': False} actor = request.user actor_profile = UserProfile.objects.get(user=actor) term = request.POST.get('term') concept_id = request.POST.get('code', None) problem = Problem.objects.get(id=problem_id) if Problem.objects.filter(problem_name=term, patient=problem.patient).exists(): return ajax_response({"msg": "Problem already added"}) old_problem_concept_id = problem.concept_id old_problem_name = problem.problem_name if datetime.now() > datetime.strptime( problem.start_date.strftime('%d/%m/%Y') + ' ' + problem.start_time.strftime('%H:%M:%S'), "%d/%m/%Y %H:%M:%S") + timedelta(hours=24): problem.old_problem_name = old_problem_name problem.problem_name = term problem.concept_id = concept_id problem.save() physician = request.user if old_problem_concept_id and problem.concept_id: summary = '<b>%s (%s)</b> was changed to <b>%s (%s)</b>' % ( old_problem_name, old_problem_concept_id, problem.problem_name, problem.concept_id) elif old_problem_concept_id: summary = '<b>%s (%s)</b> was changed to <b>%s</b>' % ( old_problem_name, old_problem_concept_id, problem.problem_name) elif problem.concept_id: summary = '<b>%s</b> was changed to <b>%s (%s)</b>' % ( old_problem_name, problem.problem_name, problem.concept_id) else: summary = '<b>%s</b> was changed to <b>%s</b>' % (old_problem_name, problem.problem_name) op_add_event(physician, problem.patient, summary, problem) add_problem_activity(problem, request.user, summary) resp['success'] = True resp['problem'] = ProblemSerializer(problem).data return ajax_response(resp)
def delete_problem_image(request, problem_id, image_id): resp = {'success': False} actor_profile = UserProfile.objects.get(user=request.user) PatientImage.objects.get(id=image_id).delete() problem = Problem.objects.select_related("patient").get(id=problem_id) patient = problem.patient physician = request.user summary = '''Deleted <u>image</u> from <u>problem</u> : <b>%s</b>''' % problem.problem_name op_add_event(physician, patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity, 'input') resp['success'] = True return ajax_response(resp)
def track_problem_click(request, problem_id): actor = request.user actor_profile = UserProfile.objects.get(user=actor) if actor_profile.role in ['physician', 'admin']: problem = Problem.objects.get(id=problem_id) patient = problem.patient summary = "Clicked <u>problem</u>: <b>%s</b>" % problem.problem_name op_add_event(actor, patient, summary) activity = "Visited <u>problem</u>: <b>%s</b>" % problem.problem_name add_problem_activity(problem, request.user, activity) resp = {} return ajax_response(resp)
def update_todo_status(request, todo_id): resp = {'success': False} accomplished = request.POST.get('accomplished') == 'true' physician = request.user # patient = todo.patient todo = ToDo.objects.get(id=todo_id) todo.accomplished = accomplished todo.save(update_fields=["accomplished"]) # set problem authentication set_problem_authentication_false(request, todo) problem_name = todo.problem.problem_name if todo.problem else "" accomplished_label = 'accomplished' if accomplished else "not accomplished" summary = """ Updated status of <u>todo</u> : <a href="#/todo/{}"><b>{}</b></a> , <u>problem</u> <b>{}</b> to <b>{}</b> """.format(todo.id, todo.todo, problem_name, accomplished_label) op_add_todo_event(physician, todo.patient, summary, todo) actor_profile = UserProfile.objects.get(user=request.user) if todo.problem: add_problem_activity(todo.problem, request.user, summary, 'output') if accomplished: op_add_event(physician, todo.patient, summary, todo.problem, True) # todo activity activity = "Updated status of this todo to <b>{}</b>.".format(accomplished_label) add_todo_activity(todo, request.user, activity) # todos = ToDo.objects.filter(patient=patient) # accomplished_todos = [todo for todo in todos if todo.accomplished] # pending_todos = [todo for todo in todos if not todo.accomplished] resp['success'] = True resp['todo'] = TodoSerializer(todo).data # resp['accomplished_todos'] = TodoSerializer(accomplished_todos, many=True).data # resp['pending_todos'] = TodoSerializer(pending_todos, many=True).data return ajax_response(resp)
def update_start_date(request, problem_id): resp = {'success': False} actor = request.user actor_profile = UserProfile.objects.get(user=actor) start_date = request.POST.get('start_date') problem = Problem.objects.get(id=problem_id) problem.start_date = get_new_date(start_date) problem.save() physician = request.user patient = problem.patient summary = '''Changed <u>problem</u> : <b>%s</b> start date to <b>%s</b>''' % ( problem.problem_name, problem.start_date) op_add_event(physician, patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity) resp['success'] = True return ajax_response(resp)
def update_by_ptw(request): resp = {'success': False} actor_profile = UserProfile.objects.get(user=request.user) timeline_data = json.loads(request.body)['timeline_data'] for problem_json in timeline_data['problems']: problem = ProblemService.update_from_timeline_data(problem_json) patient = problem.patient problem.authenticated = actor_profile.role in ['physician', 'admin'] problem.save() physician = request.user summary = '''Changed <u>problem</u> :<b>%s</b> start date to <b>%s</b>''' % ( problem.problem_name, problem.start_date) op_add_event(physician, patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity) resp['success'] = True return ajax_response(resp)
def add_wiki_note(request, problem_id): """ @depre :param request: :param problem_id: :return: """ resp = {'success': False} try: problem = Problem.objects.get(id=problem_id) author_profile = UserProfile.objects.get(user=request.user) except (Problem.DoesNotExist, UserProfile.DoesNotExist) as e: return ajax_response(resp) # Check if user is able to view patient # Todo actor = request.user actor_profile = UserProfile.objects.get(user=actor) note = request.POST.get('note') physician = request.user patient = problem.patient new_note = ProblemNote.objects.create_wiki_note(request.user, problem, note) activity = 'Added wiki note: <b>%s</b>' % note add_problem_activity(problem, request.user, activity, 'input') op_add_event(physician, patient, activity, problem) # https://trello.com/c/hkdbHZjw auto_generate_note_todo(actor_profile, patient, problem, request, resp) resp['note'] = ProblemNoteSerializer(new_note).data resp['success'] = True return ajax_response(resp)
def change_name(request, patient_id, goal_id): resp = {} patient = User.objects.get(id=patient_id) new_goal = request.POST.get("goal") goal = Goal.objects.get(id=goal_id, patient=patient) goal.goal = new_goal goal.save() status_labels = {'goal': goal.goal, 'new_goal': new_goal} physician = request.user summary = 'Change <u>goal</u>: <b>%(goal)s</b> <u>name</u> to <b>%(new_goal)s</b>' summary = summary % status_labels op_add_event(physician, patient, summary, goal.problem) if goal.problem: add_problem_activity(goal.problem, request.user, summary, 'output') resp['goal'] = GoalSerializer(goal).data resp['success'] = True return ajax_response(resp)
def add_problem_todo(request, problem_id): resp = {'success': False} problem = Problem.objects.get(id=problem_id) actor_profile = UserProfile.objects.get(user=request.user) problem.authenticated = actor_profile.role in ['physician', 'admin'] problem.save() patient = problem.patient todo = request.POST.get('name') due_date = request.POST.get('due_date', None) if due_date: due_date = parser.parse(due_date, dayfirst=False).date() # due_date = datetime.strptime(due_date, '%m/%d/%Y').date() new_todo = ToDo(patient=patient, problem=problem, todo=todo, due_date=due_date) a1c_id = request.POST.get('a1c_id', None) if a1c_id: a1c = AOneC.objects.get(id=int(a1c_id)) new_todo.a1c = a1c order = ToDo.objects.all().aggregate(Max('order')) if not order['order__max']: order = 1 else: order = order['order__max'] + 1 new_todo.order = order new_todo.save() colon_cancer_id = request.POST.get('colon_cancer_id', None) if colon_cancer_id: colon = ColonCancerScreening.objects.get(id=int(colon_cancer_id)) if not Label.objects.filter(name="screening", css_class="todo-label-yellow", is_all=True).exists(): label = Label(name="screening", css_class="todo-label-yellow", is_all=True) label.save() else: label = Label.objects.get(name="screening", css_class="todo-label-yellow", is_all=True) new_todo.colon_cancer = colon new_todo.save() new_todo.labels.add(label) physician = request.user summary = '''Added <u>todo</u> : <a href="#/todo/%s"><b>%s</b></a> to <u>problem</u> : <b>%s</b>''' % ( new_todo.id, todo, problem.problem_name) op_add_event(physician, patient, summary, problem) activity = summary add_problem_activity(problem, request.user, activity, 'output') summary = '''Added <u>todo</u> <a href="#/todo/%s"><b>%s</b></a> for <u>problem</u> <b>%s</b>''' % ( new_todo.id, new_todo.todo, problem.problem_name) op_add_todo_event(physician, patient, summary, new_todo, True) # todo activity activity = ''' Added this todo. ''' add_todo_activity(new_todo, request.user, activity) resp['success'] = True resp['todo'] = TodoSerializer(new_todo).data return ajax_response(resp)
def problem_notes_function(request, problem_id): """ :param request: :param problem_id: :return: """ # TODO: Add a new problem's note (either wiki or history) resp = {'success': False} if request.method == "POST": responseBody = json.loads(request.body) actor = request.user actor_profile = UserProfile.objects.get(user=actor) note = responseBody.get( 'note') # <- TODO: Sanitize to prevent SQL Injection Attack note_type = responseBody.get( 'note_type') # <- TODO: Validate note type # physician = request.user # Validation try: problem = Problem.objects.get(id=problem_id) # author_profile = UserProfile.objects.get(user=request.user) except (Problem.DoesNotExist, UserProfile.DoesNotExist): return ajax_response(resp) # Adding new note new_note = ProblemNote.objects.create_problem_note( actor, problem, note, note_type) # Save problem activity activity = 'Added wiki note: <b>%s</b>' % note add_problem_activity(problem, request.user, activity, 'input') # Add operation event op_add_event(actor, problem.patient, activity, problem) # Auto generate to do correspond to note https://trello.com/c/hkdbHZjw auto_generate_note_todo(actor_profile, problem.patient, problem, request, resp) resp['note'] = ProblemNoteSerializer(new_note).data resp['success'] = True if request.method == "GET": note_type = request.GET.get('type') # this is required before = request.GET.get('before', 1000000000) # this is optional limit = request.GET.get('limit', 10) # TODO: Getting list of notes with default reserve chronology (most recent on top) notes = ProblemNote.objects.filter( problem_id=problem_id, note_type=note_type, id__lt=before).order_by('-created_on')[0:limit] resp['notes'] = ProblemNoteSerializer(notes.all(), many=True).data resp['total'] = ProblemNote.objects.filter(problem_id=problem_id, note_type=note_type, id__lt=before).count() resp['success'] = True if request.method == "DELETE": pass return ajax_response(resp)
def add_patient_problem(request, patient_id): resp = {'success': False} actor = request.user actor_profile = UserProfile.objects.get(user=actor) term = request.POST.get('term') concept_id = request.POST.get('code', None) physician = request.user patient = User.objects.get(id=int(patient_id)) if Problem.objects.filter(problem_name=term, patient__id=patient_id).exists(): resp["msg"] = "Problem already being added" return ajax_response(resp) new_problem = Problem.objects.create_new_problem(patient_id, term, concept_id, actor_profile) # https://trello.com/c/0OlwGwCB # Only add if problem is diabetes and patient have not if "44054006" == concept_id: if not ObservationComponent.objects.filter( component_code="2345-7", observation__subject=patient).exists(): # Add data(observation) Glucose type observation = Observation.objects.create(subject=patient, author=request.user, name="Glucose", code="2345-7", color="#FFD2D2") observation.save() # Add data unit observation_unit = ObservationUnit.objects.create( observation=observation, value_unit="mg/dL") observation_unit.is_used = True # will be changed in future when having conversion observation_unit.save() # Add data component observation_component = ObservationComponent() observation_component.observation = observation observation_component.component_code = "2345-7" observation_component.name = "Glucose" observation_component.save() else: observation = ObservationComponent.objects.get( component_code="2345-7", observation__subject=patient) # Pin to problem ObservationPinToProblem.objects.create(author_id=request.user.id, observation=observation, problem=new_problem) # Event summary = "Added <u>problem</u> <b>{}</b>".format(term) op_add_event(physician, new_problem.patient, summary, new_problem) # Activity activity = "Added <u>problem</u>: <b>{}</b>".format(term) add_problem_activity(new_problem, request.user, activity) resp['success'] = True resp['problem'] = ProblemSerializer(new_problem).data return ajax_response(resp)