def reject_shared_patients(self, *args, **kw): activity_id = kw.get('activity_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry['nh.eobs.api'] activities = api.get_assigned_activities( cr, uid, activity_type='nh.clinical.patient.follow', context=context) res = {} for a in activities: if a['id'] == int(activity_id): res = a res['status'] = True break response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully rejected stand-in invite', description='You are not following {0} patient(s) from {1}'.format( res['count'], res['user']), data=res) try: api.cancel(cr, uid, int(activity_id), {}, context=context) except osv.except_osv: res = {'reason': 'Unable to cancel the activity.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Unable to reject stand-in invite', description='An error occurred when trying to ' 'reject the stand-in invite', data=res) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_patient_info(self, *args, **kw): patient_id = kw.get('patient_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') patient_info = api_pool.get_patients(cr, uid, [int(patient_id)], context=context) if len(patient_info) > 0: response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title=patient_info[0]['full_name'], description='Information on {0}'.format( patient_info[0]['full_name'] ), data=patient_info[0] ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE ) else: response_data = {'error': 'Patient not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Patient not found', description='Unable to get patient with ID provided', data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def get_patient_obs(self, *args, **kw): patient_id = kw.get('patient_id') # TODO: add a check if is None (?) obs_type = kw.get('obs_type') cr, uid = request.cr, request.uid api_pool = request.registry('nh.eobs.api') patient_list = api_pool.get_patients(cr, uid, [int(patient_id)]) if len(patient_list) > 0: patient = patient_list[0] observations = api_pool.get_activities_for_patient( cr, uid, patient_id=int(patient_id), activity_type=obs_type) observations.reverse() response_data = {'obs': observations, 'obsType': obs_type} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='{0}'.format(patient['full_name']), description='Observations for {0}'.format( patient['full_name']), data=response_data) else: response_data = {'error': 'Data not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Data not found', description='Unable to find data with ID and ob name provided', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_patient_barcode(self, *args, **kw): # TODO: add a check if is None (?) hospital_number = kw.get('hospital_number') cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') try: patient_info = api_pool.get_patient_info(cr, uid, hospital_number, context=context) if len(patient_info) > 0: response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title=patient_info[0]['full_name'], description='Information on {0}'.format( patient_info[0]['full_name']), data=patient_info[0]) else: response_data = {'error': 'Patient not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Patient not found', description='Unable to get patient with ID provided', data=response_data) except osv.except_osv: response_data = {'error': 'Patient not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Patient not found', description='Unable to get patient with ID provided', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_clinical(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid = request.cr, request.uid api_pool = request.registry('nh.eobs.api') kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', None) data_task_id = kw_copy.get('taskId', None) if data_timestamp is not None: del kw_copy['startTimestamp'] if data_task_id is not None: del kw_copy['taskId'] for key, value in kw_copy.items(): if not value: del kw_copy[key] # Try to get the cancel reason and add it to the dict if successful. cancel_reason = kw_copy.get('reason') if cancel_reason: kw_copy['reason'] = int(cancel_reason) try: api_pool.cancel(cr, uid, int(task_id), kw_copy) except osv.except_osv: response_data = { 'error': 'The server returned an error while trying ' 'to cancel the task.' } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Cancellation unsuccessful', description='Unable to cancel the notification', data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE ) response_data = {'related_tasks': [], 'status': 4} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Cancellation successful', description='The notification was successfully cancelled', data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def complete_notification(self, cr, uid, task_id, vals=None, context=None): """ Complete notifcation and return JSON object ready to send back to frontend :param cr: Odoo Cursor :param uid: User doing the action :param task_id: ID of the notification to complete :param vals: dict of values to send as part of complete :param context: Odoo context :return: JSON string of response """ if not vals: vals = {} task_id = int(task_id) env = Environment(cr, uid, {}) api_pool = env.registry('nh.eobs.api') try: api_pool.complete(cr, uid, task_id, vals) except osv.except_osv: response_data = { 'error': 'The server returned an error while trying ' 'to complete the task.' } return ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Submission unsuccessful', description='Unable to complete the notification', data=response_data ) activity_model = env.registry('nh.activity') activity = activity_model.read(cr, uid, task_id) patient_name = activity.get('patient_id')[1] description = 'All escalation tasks for <strong>{}</strong> ' \ 'have been completed'.format(patient_name) excluded_notification_models = [ 'nh.clinical.notification.nurse', 'nh.clinical.notification.hca' ] if activity.get('data_model') in excluded_notification_models: description = 'The notification was successfully submitted' triggered_tasks = \ self.get_triggered_tasks(cr, uid, task_id, context=context) response_data = {'related_tasks': triggered_tasks, 'status': 1} return ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Submission successful', description=description, data=response_data )
def confirm_clinical(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') kw_copy = kw.copy() if kw else {} if 'taskId' in kw_copy: del kw_copy['taskId'] if 'frequency' in kw_copy: kw_copy['frequency'] = int(kw_copy['frequency']) if 'location_id' in kw_copy: kw_copy['location_id'] = int(kw_copy['location_id']) # TODO: add a check if method 'complete' fails(?) api.complete(cr, uid, int(task_id), kw_copy) triggered_ids = activity_api.search( cr, uid, [['creator_id', '=', int(task_id)]]) triggered_tasks_read = activity_api.read(cr, uid, triggered_ids, []) triggered_tasks = [] for trig_task in triggered_tasks_read: access = api.check_activity_access(cr, uid, trig_task['id'], context=context) is_not_ob = 'ews' not in trig_task['data_model'] if access and is_not_ob: triggered_tasks.append(trig_task) response_data = {'related_tasks': triggered_tasks, 'status': 1} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Submission successful', description='The notification was successfully submitted', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def share_patients(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api = request.registry['nh.eobs.api'] user_api = request.registry['res.users'] kw_copy = kw.copy() if kw else {} user_ids = [int(usid) for usid in kw_copy['user_ids'].split(',')] patient_ids = [int(pid) for pid in kw_copy['patient_ids'].split(',')] users = user_api.read(cr, uid, user_ids, ['display_name'], context=context) for user_id in user_ids: api.follow_invite(cr, uid, patient_ids, user_id, context=context) reason = 'An invite has been sent to follow the selected patients.' response_data = { 'reason': reason, 'shared_with': [user['display_name'] for user in users] } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Invitation sent', description='An invite has been sent to follow ' 'the selected patients to: ', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_patient_obs(self, *args, **kw): patient_id = kw.get('patient_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') patient_list = api_pool.get_patients(cr, uid, [int(patient_id)]) if len(patient_list) > 0: patient = patient_list[0] ews = api_pool.get_activities_for_patient( cr, uid, patient_id=int(patient_id), activity_type='ews' ) for ew in ews: for e in ew: if e in ['date_terminated', 'create_date', 'write_date', 'date_started']: if not ew[e]: continue ew[e] = fields.datetime.context_timestamp( cr, uid, datetime.strptime(ew[e], DTF), context=context ).strftime(DTF) response_data = { 'obs': ews, 'obsType': 'ews' } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='{0}'.format(patient['full_name']), description='Observations for {0}'.format( patient['full_name'] ), data=response_data ) else: response_data = {'error': 'Patient not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Patient not found', description='Unable to find patient with ID provided', data=response_data ) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_clinical(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid = request.cr, request.uid api_pool = request.registry('nh.eobs.api') kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', None) data_task_id = kw_copy.get('taskId', None) if data_timestamp is not None: del kw_copy['startTimestamp'] if data_task_id is not None: del kw_copy['taskId'] for key, value in kw_copy.items(): if not value: del kw_copy[key] # Try to get the cancel reason and add it to the dict if successful. cancel_reason = kw_copy.get('reason') if cancel_reason: kw_copy['reason'] = int(cancel_reason) try: api_pool.cancel(cr, uid, int(task_id), kw_copy) except osv.except_osv: response_data = { 'error': 'The server returned an error while trying ' 'to cancel the task.' } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Cancellation unsuccessful', description='Unable to cancel the notification', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE) response_data = {'related_tasks': [], 'status': 4} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Cancellation successful', description='The notification was successfully cancelled', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_notification(self, cr, uid, task_id, vals=None, context=None): """ Cancel notification and send JSON response ready to send back to the frontend :param cr: Odoo cursor :param uid: User ID cancelling notification :param task_id: ID of the notification being cancelled :param vals: dictionary of values to send as part of cancel :param context: Odoo context :return: JSON string of response """ if not vals: vals = {} env = Environment(cr, uid, {}) api_pool = env.registry('nh.eobs.api') task_id = int(task_id) try: api_pool.cancel(cr, uid, task_id, vals) except osv.except_osv: response_data = { 'error': 'The server returned an error while trying ' 'to cancel the task.' } return ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Cancellation unsuccessful', description='Unable to cancel the notification', data=response_data) activity_model = env.registry('nh.activity') patient_name = activity_model.read(cr, uid, task_id, ['patient_id'])['patient_id'][1] description = 'All escalation tasks for <strong>{}</strong> ' \ 'have been completed'.format(patient_name) triggered_tasks = self.get_triggered_tasks(cr, uid, task_id, context=context) response_data = {'related_tasks': triggered_tasks, 'status': 4} return ResponseJSON.get_json_data(status=ResponseJSON.STATUS_SUCCESS, title='Cancellation successful', description=description, data=response_data)
def cancel_take_task_ajax(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context task_id = int(task_id) activity_reg = request.registry['nh.activity'] api_reg = request.registry['nh.eobs.api'] task = activity_reg.read(cr, uid, task_id, ['user_id'], context=context) if task and task.get('user_id') and task['user_id'][0] != uid: response_data = {'reason': "Can't cancel other user's task."} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_FAIL, title='Unable to release task', description='The task you are trying to release ' 'is being carried out by another user', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE) else: try: api_reg.unassign(cr, uid, task_id, context=context) except osv.except_osv: response_data = {'reason': 'Unable to unassign task.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Unable to release task', description='An error occurred when trying to ' 'release the task back into the task pool', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE) response_data = { 'reason': 'Task was successfully unassigned from you.' } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully released task', description='The task has now been released ' 'back into the task pool', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def process_ajax_form(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') ob_str = 'nh.clinical.patient.observation.' + observation ob_pool = request.registry(ob_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, ob_pool, str, context=context) kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', None) data_task_id = kw_copy.get('taskId', None) data_device_id = kw_copy.get('device_id', None) if data_timestamp is not None: del kw_copy['startTimestamp'] if data_task_id is not None: del kw_copy['taskId'] if task_id is not None: del kw_copy['task_id'] if observation is not None: del kw_copy['observation'] if data_device_id is not None: del kw_copy['device_id'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, _logger.debug) if data_timestamp is not None: converted_data['date_started'] = \ datetime.fromtimestamp(int(data_timestamp)).strftime(DTF) if data_device_id is not None: converted_data['device_id'] = data_device_id api.complete(cr, uid, int(task_id), converted_data, context) activity = activity_api.browse(cr, uid, int(task_id)) obs = activity.data_ref description = self.get_submission_message(obs) response_data = obs.get_submission_response_data() response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if obs.is_partial else '', ob_pool.get_description()), description=description, data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def take_task_ajax(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context task_id = int(task_id) activity_reg = request.registry['nh.activity'] api_reg = request.registry['nh.eobs.api'] task = activity_reg.read(cr, uid, task_id, ['user_id'], context=context) if task and task.get('user_id') and task['user_id'][0] != uid: response_data = {'reason': 'Task assigned to another user.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_FAIL, title='Unable to take task', description='This task is already assigned to another user', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE) else: try: api_reg.assign(cr, uid, task_id, {'user_id': uid}, context=context) except osv.except_osv: response_data = {'reason': 'Unable to assign to user.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Unable to take task', description='An error occurred when ' 'trying to take the task', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE) response_data = {'reason': 'Task was free to take.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Task successfully taken', description='You can now perform this task', data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_colleagues(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api = request.registry['nh.eobs.api'] colleagues = api.get_share_users(cr, uid, context=context) response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Colleagues on shift', description='Choose colleagues for stand-in', data={'colleagues': colleagues}) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_reasons(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Reason for cancelling task?', description='Please state reason for cancelling task', data=api_pool.get_cancel_reasons(cr, uid, context=context)) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_partial_reasons(self, *args, **kw): observation = kw.get('observation') obs_pool = request.registry( 'nh.clinical.patient.observation.{0}'.format(observation)) response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Reason for partial observation', description='Please state reason for submitting ' 'partial observation', data=obs_pool._partial_reasons) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_reasons(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Why is this action not required?', description='Please state the reason ' 'why this action is not required', data=api_pool.get_cancel_reasons(cr, uid, context=context)) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def process_ajax_form(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') ob_str = 'nh.clinical.patient.observation.'+observation ob_pool = request.registry(ob_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, ob_pool, str, context=context) kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', None) data_task_id = kw_copy.get('taskId', None) data_device_id = kw_copy.get('device_id', None) if data_timestamp is not None: del kw_copy['startTimestamp'] if data_task_id is not None: del kw_copy['taskId'] if task_id is not None: del kw_copy['task_id'] if observation is not None: del kw_copy['observation'] if data_device_id is not None: del kw_copy['device_id'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, _logger.debug) if data_timestamp is not None: converted_data['date_started'] = \ datetime.fromtimestamp(int(data_timestamp)).strftime(DTF) if data_device_id is not None: converted_data['device_id'] = data_device_id api.complete(cr, uid, int(task_id), converted_data, context) activity = activity_api.browse(cr, uid, int(task_id)) obs = activity.data_ref description = self.get_submission_message(obs) response_data = obs.get_submission_response_data() response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if obs.is_partial else '', ob_pool.get_description(append_observation=True)), description=description, data=response_data) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_patient_obs(self, *args, **kw): patient_id = kw.get('patient_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') patient_list = api_pool.get_patients(cr, uid, [int(patient_id)]) if len(patient_list) > 0: patient = patient_list[0] ews = api_pool.get_activities_for_patient( cr, uid, patient_id=int(patient_id), activity_type='ews') for ew in ews: for e in ew: if e in [ 'date_terminated', 'create_date', 'write_date', 'date_started' ]: if not ew[e]: continue ew[e] = fields.datetime.context_timestamp( cr, uid, datetime.strptime(ew[e], DTF), context=context).strftime(DTF) response_data = {'obs': ews, 'obsType': 'ews'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='{0}'.format(patient['full_name']), description='Observations for {0}'.format( patient['full_name']), data=response_data) else: response_data = {'error': 'Patient not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Patient not found', description='Unable to find patient with ID provided', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def cancel_reasons(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Reason for cancelling task?', description='Please state reason for cancelling task', data=api_pool.get_cancel_reasons(cr, uid, context=context) ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def claim_patients(self, *args, **kw): cr, uid, context = request.cr, request.uid, request.context api = request.registry['nh.eobs.api'] kw_copy = kw.copy() if kw else {} patient_ids = [int(pid) for pid in kw_copy['patient_ids'].split(',')] api.remove_followers(cr, uid, patient_ids, context=context) response_data = {'reason': 'Followers removed successfully.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Patients claimed', description='Followers removed successfully', data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_patient_obs(self, *args, **kw): patient_id = kw.get('patient_id') # TODO: add a check if is None (?) obs_type = kw.get('obs_type') cr, uid = request.cr, request.uid api_pool = request.registry('nh.eobs.api') patient_list = api_pool.get_patients(cr, uid, [int(patient_id)]) if len(patient_list) > 0: patient = patient_list[0] observations = api_pool.get_activities_for_patient( cr, uid, patient_id=int(patient_id), activity_type=obs_type ) observations.reverse() response_data = { 'obs': observations, 'obsType': obs_type } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='{0}'.format(patient['full_name']), description='Observations for {0}'.format( patient['full_name'] ), data=response_data ) else: response_data = {'error': 'Data not found.'} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_ERROR, title='Data not found', description='Unable to find data with ID and ob name provided', data=response_data ) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def get_partial_reasons(self, *args, **kw): observation = kw.get('observation') obs_pool = request.registry( 'nh.clinical.patient.observation.{0}'.format(observation)) response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Reason for partial observation', description='Please state reason for submitting ' 'partial observation', data=obs_pool._partial_reasons ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def get_shared_patients(self, *args, **kw): activity_id = kw.get('activity_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry['nh.eobs.api'] activities = api.get_assigned_activities( cr, uid, activity_type='nh.clinical.patient.follow', context=context) res = [] for a in activities: if a['id'] == int(activity_id): res = api.get_patients(cr, uid, a['patient_ids'], context=context) break response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Patients shared with you', description='These patients have been shared for you to follow', data=res) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def confirm_clinical(self, *args, **kw): task_id = kw.get('task_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') kw_copy = kw.copy() if kw else {} if 'taskId' in kw_copy: del kw_copy['taskId'] if 'frequency' in kw_copy: kw_copy['frequency'] = int(kw_copy['frequency']) if 'location_id' in kw_copy: kw_copy['location_id'] = int(kw_copy['location_id']) # TODO: add a check if method 'complete' fails(?) api.complete(cr, uid, int(task_id), kw_copy) triggered_ids = activity_api.search( cr, uid, [['creator_id', '=', int(task_id)]] ) triggered_tasks_read = activity_api.read(cr, uid, triggered_ids, []) triggered_tasks = [] for trig_task in triggered_tasks_read: access = api.check_activity_access(cr, uid, trig_task['id'], context=context) is_not_ob = 'ews' not in trig_task['data_model'] if access and is_not_ob: triggered_tasks.append(trig_task) response_data = {'related_tasks': triggered_tasks, 'status': 1} response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Submission successful', description='The notification was successfully submitted', data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
class TestResponseJSON(openerp.tests.SingleTransactionCase): def setUp(self): super(TestResponseJSON, self).setUp() self.response_json = ResponseJSON() def test_method_get_json_data_passing_all_arguments(self): status = self.response_json.STATUS_SUCCESS title = 'Operation executed' description = 'The operation required via API was successful executed' data = { 'patient_list': [ 'patient_1', 'patient_2', 'patient_3', 'patient_4' ], 'related_action_list': [ 'admission', 'observation', 'discharge' ] } json_data = self.response_json.get_json_data( status, title=title, description=description, data=data ) self.assertIn(status, json_data) self.assertIn(title, json_data) self.assertIn(description, json_data) for k, v in data.iteritems(): self.assertIn(k, json_data) for e in v: self.assertIn(e, json_data) python_data = json.loads(json_data) # Convert the data back to Python self.assertIsInstance(python_data, dict) self.assertEqual(len(python_data), 4) self.assertIn('status', python_data) self.assertIsInstance(python_data['status'], basestring) self.assertEqual(python_data['status'], status) self.assertIn('title', python_data) self.assertIsInstance(python_data['title'], basestring) self.assertEqual(python_data['title'], title) self.assertIn('description', python_data) self.assertIsInstance(python_data['description'], basestring) self.assertEqual(python_data['description'], description) self.assertIn('data', python_data) self.assertIsInstance(python_data['data'], dict) for k, v in data.iteritems(): self.assertIn(k, python_data['data']) for e in v: self.assertIn(e, python_data['data'][k]) def test_method_get_json_data_passing_only_status(self): status = self.response_json.STATUS_FAIL json_data = self.response_json.get_json_data(status) self.assertIn(status, json_data) python_data = json.loads(json_data) # Convert the data back to Python self.assertIsInstance(python_data, dict) self.assertEqual(len(python_data), 4) self.assertIn('status', python_data) self.assertIsInstance(python_data['status'], basestring) self.assertEqual(python_data['status'], status) # Test that the DEFAULT value for # ALL THE KEYS (except 'status') is 'False' self.assertIn('title', python_data) self.assertEqual(python_data['title'], False) self.assertIn('description', python_data) self.assertEqual(python_data['description'], False) self.assertIn('data', python_data) self.assertEqual(python_data['data'], False) def test_method_get_json_data_passing_no_arguments(self): json_data = self.response_json.get_json_data() self.assertIn(self.response_json.STATUS_ERROR, json_data) python_data = json.loads(json_data) # Convert the data back to Python self.assertIsInstance(python_data, dict) self.assertEqual(len(python_data), 4) self.assertIn('title', python_data) self.assertIn('description', python_data) self.assertIn('data', python_data) self.assertIn('status', python_data) self.assertIsInstance(python_data['status'], basestring) # Test that the DEFAULT STATUS is 'error' self.assertEqual( python_data['status'], self.response_json.STATUS_ERROR )
def process_patient_observation_form(self, *args, **kw): # TODO: add a check if is None (?) obs_model_name = kw.get('observation') # TODO: add a check if is None (?) patient_id = kw.get('patient_id') cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') obs_str = 'nh.clinical.patient.observation.' + obs_model_name observation_pool = request.registry(obs_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, observation_pool, str, context=context) kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', False) data_task_id = kw_copy.get('taskId', False) data_device_id = kw_copy.get('device_id', False) data_recorded_concerns = kw_copy.get('recorded_concerns', False) data_dietary_needs = kw_copy.get('dietary_needs', False) if data_timestamp: del kw_copy['startTimestamp'] if data_task_id: del kw_copy['taskId'] if obs_model_name is not None: del kw_copy['observation'] if patient_id is not None: del kw_copy['patient_id'] if data_device_id: del kw_copy['device_id'] if data_recorded_concerns: del kw_copy['recorded_concerns'] if data_dietary_needs: del kw_copy['dietary_needs'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, _logger.debug) if data_timestamp: converted_data['date_started'] = datetime.fromtimestamp( int(data_timestamp)).strftime(DTF) if data_device_id: converted_data['device_id'] = data_device_id if data_recorded_concerns: converted_data['recorded_concerns'] = \ [[6, 0, map(int, data_recorded_concerns.split(','))]] if data_dietary_needs: converted_data['dietary_needs'] = \ [[6, 0, map(int, data_dietary_needs.split(','))]] vals_data = {} if obs_model_name == 'neurological': if 'eyes' in converted_data: vals_data['eyes'] = converted_data['eyes'] if 'verbal' in converted_data: vals_data['verbal'] = converted_data['verbal'] if 'motor' in converted_data: vals_data['motor'] = converted_data['motor'] # elif obs_model_name == 'food_fluid': # if 'passed_urine' in converted_data: # vals_data['passed_urine'] = converted_data['passed_urine'] # if 'bowels_open' in converted_data: # vals_data['bowels_open'] = converted_data['bowels_open'] else: vals_data = converted_data new_activity_id = api.create_activity_for_patient(cr, uid, int(patient_id), obs_model_name, vals_data=vals_data, context=context) api.complete(cr, uid, int(new_activity_id), converted_data, context) new_activity = activity_api.browse(cr, uid, new_activity_id) obs = new_activity.data_ref description = self.get_submission_message(obs) response_data = obs.get_submission_response_data() response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if obs.is_partial else '', observation_pool.get_description()), description=description, data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def process_patient_observation_form(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) patient_id = kw.get('patient_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') obs_str = 'nh.clinical.patient.observation.'+observation observation_pool = request.registry(obs_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, observation_pool, str, context=context) kw_copy = kw.copy() if kw else {} test = {} data_timestamp = kw_copy.get('startTimestamp', False) data_task_id = kw_copy.get('taskId', False) data_device_id = kw_copy.get('device_id', False) if data_timestamp: del kw_copy['startTimestamp'] if data_task_id: del kw_copy['taskId'] if observation is not None: del kw_copy['observation'] if patient_id is not None: del kw_copy['patient_id'] if data_device_id: del kw_copy['device_id'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, test) if data_timestamp: converted_data['date_started'] = datetime.fromtimestamp( int(data_timestamp)).strftime(DTF) if data_device_id: converted_data['device_id'] = data_device_id new_activity = api.create_activity_for_patient(cr, uid, int(patient_id), observation, context=context) api.complete(cr, uid, int(new_activity), converted_data, context) triggered_ids = activity_api.search( cr, uid, [['creator_id', '=', int(new_activity)]] ) triggered_tasks_read = activity_api.read(cr, uid, triggered_ids, []) triggered_tasks = [] for trig_task in triggered_tasks_read: access = api.check_activity_access(cr, uid, trig_task['id']) is_not_ob = observation not in trig_task['data_model'] is_open = trig_task['state'] not in ['completed', 'cancelled'] if access and is_open and is_not_ob: triggered_tasks.append(trig_task) partial = 'partial_reason' in kw_copy and kw_copy['partial_reason'] response_data = {'related_tasks': triggered_tasks, 'status': 1} rel_tasks = 'Here are related tasks based on the observation' if len( triggered_tasks ) > 0 else '' response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if partial else '', observation_pool._description ), description=rel_tasks, data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def calculate_obs_score(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') model = 'nh.clinical.patient.observation.' + observation converter_pool = request.registry('ir.fields.converter') observation_pool = request.registry(model) converter = converter_pool.for_model(cr, uid, observation_pool, str, context=context) data = kw.copy() if kw else {} section = 'patient' if 'startTimestamp' in data: del data['startTimestamp'] if 'taskId' in data: section = 'task' del data['taskId'] if observation is not None: del data['observation'] if observation == 'ews': observation = 'news' for key, value in data.items(): if not value or key not in [ 'avpu_text', 'blood_pressure_systolic', 'body_temperature', 'indirect_oxymetry_spo2', 'oxygen_administration_flag', 'pulse_rate', 'respiration_rate' ]: del data[key] converted_data = converter(data, _logger.debug) score_dict = api_pool.get_activity_score(cr, uid, model, converted_data, context=context) if not score_dict: exceptions.abort(400) modal_vals = {} score_type = observation.upper() if observation != 'neurological' \ else 'Coma Scale' # TODO: Need to add patient name in somehow modal_vals['title'] = 'Submit {score_type} score of {score}'.format( score_type=score_type, score=score_dict.get('score', '')) if 'clinical_risk' in score_dict: modal_vals['content'] = '<p><strong>' \ 'Clinical risk: {risk}</strong>' \ '</p><p>' \ 'Please confirm you want to ' \ 'submit this score</p>'.format( risk=score_dict['clinical_risk']) else: modal_vals['content'] = '<p>Please confirm you want to ' \ 'submit this score</p>' response_data = { 'score': score_dict, 'modal_vals': modal_vals, 'status': 3, 'next_action': 'json_{}_form_action'.format(section) } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title=modal_vals['title'], description=modal_vals['content'], data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def process_patient_observation_form(self, *args, **kw): # TODO: add a check if is None (?) obs_model_name = kw.get('observation') # TODO: add a check if is None (?) patient_id = kw.get('patient_id') cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') obs_str = 'nh.clinical.patient.observation.'+obs_model_name observation_pool = request.registry(obs_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, observation_pool, str, context=context) kw_copy = kw.copy() if kw else {} data_timestamp = kw_copy.get('startTimestamp', False) data_task_id = kw_copy.get('taskId', False) data_device_id = kw_copy.get('device_id', False) if data_timestamp: del kw_copy['startTimestamp'] if data_task_id: del kw_copy['taskId'] if obs_model_name is not None: del kw_copy['observation'] if patient_id is not None: del kw_copy['patient_id'] if data_device_id: del kw_copy['device_id'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, _logger.debug) if data_timestamp: converted_data['date_started'] = datetime.fromtimestamp( int(data_timestamp)).strftime(DTF) if data_device_id: converted_data['device_id'] = data_device_id vals_data = {} if obs_model_name == 'neurological' or obs_model_name == 'gcs': if 'eyes' in converted_data: vals_data['eyes'] = converted_data['eyes'] if 'verbal' in converted_data: vals_data['verbal'] = converted_data['verbal'] if 'motor' in converted_data: vals_data['motor'] = converted_data['motor'] elif obs_model_name == 'food_fluid': if 'passed_urine' in converted_data: vals_data['passed_urine'] = converted_data['passed_urine'] if 'bowels_open' in converted_data: vals_data['bowels_open'] = converted_data['bowels_open'] else: vals_data = converted_data new_activity_id = api.create_activity_for_patient( cr, uid, int(patient_id), obs_model_name, vals_data=vals_data, context=context ) api.complete(cr, uid, int(new_activity_id), converted_data, context) new_activity = activity_api.browse(cr, uid, new_activity_id) obs = new_activity.data_ref description = self.get_submission_message(obs) response_data = obs.get_submission_response_data() response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if obs.is_partial else '', observation_pool.get_description(append_observation=True) ), description=description, data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def calculate_obs_score(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api_pool = request.registry('nh.eobs.api') model = 'nh.clinical.patient.observation.' + observation converter_pool = request.registry('ir.fields.converter') observation_pool = request.registry(model) converter = converter_pool.for_model( cr, uid, observation_pool, str, context=context ) data = kw.copy() if kw else {} section = 'patient' if 'startTimestamp' in data: del data['startTimestamp'] if 'taskId' in data: section = 'task' del data['taskId'] if observation is not None: del data['observation'] if observation == 'ews': observation = 'news' for key, value in data.items(): if not value or key not in [ 'avpu_text', 'blood_pressure_systolic', 'body_temperature', 'indirect_oxymetry_spo2', 'oxygen_administration_flag', 'pulse_rate', 'respiration_rate' ]: del data[key] converted_data = converter(data, _logger.debug) score_dict = api_pool.get_activity_score( cr, uid, model, converted_data, context=context ) if not score_dict: exceptions.abort(400) modal_vals = {} score_type = observation.upper() if observation != 'neurological' \ else 'Coma Scale' # TODO: Need to add patient name in somehow modal_vals['title'] = 'Submit {score_type} score of {score}'.format( score_type=score_type, score=score_dict.get('score', '') ) if 'clinical_risk' in score_dict: modal_vals['content'] = '<p><strong>' \ 'Clinical risk: {risk}</strong>' \ '</p><p>' \ 'Please confirm you want to ' \ 'submit this score</p>'.format( risk=score_dict['clinical_risk']) else: modal_vals['content'] = '<p>Please confirm you want to ' \ 'submit this score</p>' response_data = { 'score': score_dict, 'modal_vals': modal_vals, 'status': 3, 'next_action': 'json_{}_form_action'.format(section) } response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title=modal_vals['title'], description=modal_vals['content'], data=response_data ) return request.make_response( response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE )
def process_patient_observation_form(self, *args, **kw): observation = kw.get('observation') # TODO: add a check if is None (?) patient_id = kw.get('patient_id') # TODO: add a check if is None (?) cr, uid, context = request.cr, request.uid, request.context api = request.registry('nh.eobs.api') activity_api = request.registry('nh.activity') obs_str = 'nh.clinical.patient.observation.' + observation observation_pool = request.registry(obs_str) converter_pool = request.registry('ir.fields.converter') converter = converter_pool.for_model(cr, uid, observation_pool, str, context=context) kw_copy = kw.copy() if kw else {} test = {} data_timestamp = kw_copy.get('startTimestamp', False) data_task_id = kw_copy.get('taskId', False) data_device_id = kw_copy.get('device_id', False) if data_timestamp: del kw_copy['startTimestamp'] if data_task_id: del kw_copy['taskId'] if observation is not None: del kw_copy['observation'] if patient_id is not None: del kw_copy['patient_id'] if data_device_id: del kw_copy['device_id'] for key, value in kw_copy.items(): if not value: del kw_copy[key] converted_data = converter(kw_copy, test) if data_timestamp: converted_data['date_started'] = datetime.fromtimestamp( int(data_timestamp)).strftime(DTF) if data_device_id: converted_data['device_id'] = data_device_id new_activity = api.create_activity_for_patient(cr, uid, int(patient_id), observation, context=context) api.complete(cr, uid, int(new_activity), converted_data, context) triggered_ids = activity_api.search( cr, uid, [['creator_id', '=', int(new_activity)]]) triggered_tasks_read = activity_api.read(cr, uid, triggered_ids, []) triggered_tasks = [] for trig_task in triggered_tasks_read: access = api.check_activity_access(cr, uid, trig_task['id']) is_not_ob = observation not in trig_task['data_model'] is_open = trig_task['state'] not in ['completed', 'cancelled'] if access and is_open and is_not_ob: triggered_tasks.append(trig_task) partial = 'partial_reason' in kw_copy and kw_copy['partial_reason'] response_data = {'related_tasks': triggered_tasks, 'status': 1} rel_tasks = 'Here are related tasks based on the observation' if len( triggered_tasks) > 0 else '' response_json = ResponseJSON.get_json_data( status=ResponseJSON.STATUS_SUCCESS, title='Successfully Submitted{0} {1}'.format( ' Partial' if partial else '', observation_pool._description), description=rel_tasks, data=response_data) return request.make_response(response_json, headers=ResponseJSON.HEADER_CONTENT_TYPE)
def set_rapid_tranq(self, request, **kwargs): """ Handles requests to the `rapid_tranq` endpoint for the controller. When called with a 'GET' method a `rapid_tranq` parameter is returned in the body of the response with the current value. When called with a 'POST' method a `rapid_tranq` parameter is expected in the body of the request with the new value to be set on the field. A `rapid_tranq` parameter is also returned in the body of the response with the new, updated `rapid_tranq` value. Additionally a GET message can pass a `check` parameter with a boolean value ('true' or 'false') to test what the effect of a POST would be. The response will be the same as for the POST, but additionally will return a title and description with appropriate messages. If there will be no change in the state of the resource then the messages advise the user to reload their page, as of writing this that is the only known scenario a case where the user attempts to update the `rapid_tranq` field to a value it already has. Otherwise if the POST would result in an update, confirmation messages are returned. :param request: Request object passed from a controller. :param kwargs: Keyword arguments passed from a controller. :return: JSON encoded string. :rtype: str """ # Validate patient ID. patient_id = kwargs.get('patient_id') try: patient_id = int(patient_id) except ValueError: exceptions.abort(404, "Invalid patient ID.") spell_model = self.env['nh.clinical.spell'] spell_activity = spell_model.get_spell_activity_by_patient_id( patient_id) if not spell_activity: exceptions.abort(404, "No spell found for patient with that ID.") existing_value = spell_activity.data_ref.rapid_tranq def parse_set_value(kwargs): set_value = kwargs.get('rapid_tranq') if not set_value: set_value = kwargs.get('check') if set_value == 'true': set_value = True elif set_value == 'false': set_value = False else: message = "New rapid tranq value should be either 'true' or " \ "'false'." exceptions.abort(400, message) return set_value status = ResponseJSON.STATUS_SUCCESS title = False description = False rapid_tranq_model = self.env['nh.clinical.pme.rapid_tranq'] # Handle differently depending on HTTP method. method = request.httprequest.method if method == 'GET' and 'check' in kwargs: set_value = parse_set_value(kwargs) spell = spell_activity.data_ref check_response = rapid_tranq_model.check_set_rapid_tranq( set_value, spell) try: status = check_response['status'] title = check_response['title'] description = check_response['description'] except KeyError: raise ValueError( "Dictionary returned from check_set_rapid_tranq method did" " not contain the keys needed to form a complete response." ) elif method == 'POST': existing_value = rapid_tranq_model.toggle_rapid_tranq( spell_activity) else: exceptions.abort(405) response_data = { 'rapid_tranq': existing_value } response_json = ResponseJSON.get_json_data( status=status, data=response_data, title=title, description=description ) return response_json
def setUp(self): super(TestResponseJSON, self).setUp() self.response_json = ResponseJSON()
def set_rapid_tranq(self, request, **kwargs): """ Handles requests to the `rapid_tranq` endpoint for the controller. When called with a 'GET' method a `rapid_tranq` parameter is returned in the body of the response with the current value. When called with a 'POST' method a `rapid_tranq` parameter is expected in the body of the request with the new value to be set on the field. A `rapid_tranq` parameter is also returned in the body of the response with the new, updated `rapid_tranq` value. Additionally a GET message can pass a `check` parameter with a boolean value ('true' or 'false') to test what the effect of a POST would be. The response will be the same as for the POST, but additionally will return a title and description with appropriate messages. If there will be no change in the state of the resource then the messages advise the user to reload their page, as of writing this that is the only known scenario a case where the user attempts to update the `rapid_tranq` field to a value it already has. Otherwise if the POST would result in an update, confirmation messages are returned. :param request: Request object passed from a controller. :param kwargs: Keyword arguments passed from a controller. :return: JSON encoded string. :rtype: str """ # Validate patient ID. patient_id = kwargs.get('patient_id') try: patient_id = int(patient_id) except ValueError: exceptions.abort(404, "Invalid patient ID.") spell_model = self.env['nh.clinical.spell'] spell_activity = spell_model.get_spell_activity_by_patient_id( patient_id) if not spell_activity: exceptions.abort(404, "No spell found for patient with that ID.") existing_value = spell_activity.data_ref.rapid_tranq def parse_set_value(kwargs): set_value = kwargs.get('rapid_tranq') if not set_value: set_value = kwargs.get('check') if set_value == 'true': set_value = True elif set_value == 'false': set_value = False else: message = "New rapid tranq value should be either 'true' or " \ "'false'." exceptions.abort(400, message) return set_value status = ResponseJSON.STATUS_SUCCESS title = False description = False rapid_tranq_model = self.env['nh.clinical.pme.rapid_tranq'] # Handle differently depending on HTTP method. method = request.httprequest.method if method == 'GET' and 'check' in kwargs: set_value = parse_set_value(kwargs) spell = spell_activity.data_ref check_response = rapid_tranq_model.check_set_rapid_tranq( set_value, spell) try: status = check_response['status'] title = check_response['title'] description = check_response['description'] except KeyError: raise ValueError( "Dictionary returned from check_set_rapid_tranq method did" " not contain the keys needed to form a complete response." ) elif method == 'POST': existing_value = rapid_tranq_model.toggle_rapid_tranq( spell_activity) else: exceptions.abort(405) response_data = {'rapid_tranq': existing_value} response_json = ResponseJSON.get_json_data(status=status, data=response_data, title=title, description=description) return response_json