def add_item(self, item, form, case_id, question, answer, audit_log_id_ref): answer = oc_format_date(answer) answer = oc_format_time(answer, self._domain.get_default_timezone()) oc_user = self._get_oc_user(form.auth_context['user_id']) if getattr(form, 'deprecated_form_id', None) and question in self.question_items[ form.deprecated_form_id]: # This form has been edited on HQ. Fetch original item item_dict, study_event = self.question_items[ form.deprecated_form_id][question] if item_dict['value'] != answer: self.edit_item(item_dict, form, question, answer, audit_log_id_ref, oc_user) else: item_dict, study_event = self.get_item_dict( item, form, case_id, question) if item_dict and item_dict['value'] != answer: # This form has been submitted more than once for a non-repeating item group. This is an edit. self.edit_item(item_dict, form, question, answer, audit_log_id_ref, oc_user) else: item_dict['value'] = answer if AUDIT_LOGS: audit_log_id_ref['id'] += 1 item_dict['audit_logs'] = [{ 'id': 'AL_{}'.format(audit_log_id_ref['id']), 'user_id': oc_user.user_id, 'username': oc_user.username, 'full_name': oc_user.full_name, 'timestamp': form.received_on, 'audit_type': 'Item data value updated', 'reason': 'initial value', 'new_value': answer, 'value_type': question, }] mu_oid = get_item_measurement_unit(self._domain_name, item) if mu_oid: item_dict['measurement_unit_oid'] = mu_oid if study_event.start_datetime is None or form.form['meta'][ 'timeStart'] < study_event.start_datetime: study_event.start_datetime = form.form['meta']['timeStart'] if study_event.end_datetime is None or form.form['meta'][ 'timeEnd'] > study_event.end_datetime: study_event.end_datetime = form.form['meta']['timeEnd']
def subject_rows(self): audit_log_id = 0 # To exclude audit logs, set `custom.openclinica.const.AUDIT_LOGS = False` for case in self.get_subject_cases(): subject = Subject(getattr(case, CC_SUBJECT_KEY), getattr(case, CC_STUDY_SUBJECT_ID), self.domain) for form in originals_first(case.get_forms()): updates = form.form['case'].get('update', {}) if updates: for question, answer in updates.iteritems(): item = get_question_item(self.domain, form.xmlns, question) if item is None: # This is a CommCare-only question or form continue audit_log_id += 1 subject.add_item(item, form, question, oc_format_date(answer), audit_log_id) subject.close_form(form) yield subject
def add_data(self, data, form, audit_log_id_ref): def get_next_item(question_list): for question_ in question_list: item_ = get_question_item(self._domain, form.xmlns, question_) if item_: return item_ return None # If a CommCare form is an OpenClinica repeating item group, then we would need to add a new item # group. This isn't relevant for this project. # if form.xmlns in REPEATING_ITEM_GROUP_FORMS: # pass for key, value in data.iteritems(): if key in _reserved_keys: continue if isinstance(value, list): # Repeat group # NOTE: We need to assume that repeat groups can't be edited in later form submissions item = get_next_item(value) if item is None: # None of the questions in this group are OpenClinica items continue self.add_item_group(item, form) for v in value: # TODO: More testing if not isinstance(v, dict): raise OpenClinicaIntegrationError( 'CommCare question value is an unexpected data type. Form XMLNS: "{}"'.format( form.xmlns)) self.add_data(v, form, audit_log_id_ref) elif isinstance(value, dict): # Group self.add_data(value, form, audit_log_id_ref) else: # key is a question and value is its answer item = get_question_item(self._domain, form.xmlns, key) if item is None: # This is a CommCare-only question or form continue self.add_item(item, form, key, oc_format_date(value), audit_log_id_ref)
def add_item(self, item, form, case_id, question, answer, audit_log_id_ref): answer = oc_format_date(answer) answer = oc_format_time(answer, self._domain.get_default_timezone()) oc_user = self._get_oc_user(form.auth_context['user_id']) if getattr(form, 'deprecated_form_id', None) and question in self.question_items[form.deprecated_form_id]: # This form has been edited on HQ. Fetch original item item_dict, study_event = self.question_items[form.deprecated_form_id][question] if item_dict['value'] != answer: self.edit_item(item_dict, form, question, answer, audit_log_id_ref, oc_user) else: item_dict, study_event = self.get_item_dict(item, form, case_id, question) if item_dict and item_dict['value'] != answer: # This form has been submitted more than once for a non-repeating item group. This is an edit. self.edit_item(item_dict, form, question, answer, audit_log_id_ref, oc_user) else: item_dict['value'] = answer if AUDIT_LOGS: audit_log_id_ref['id'] += 1 item_dict['audit_logs'] = [{ 'id': 'AL_{}'.format(audit_log_id_ref['id']), 'user_id': oc_user.user_id, 'username': oc_user.username, 'full_name': oc_user.full_name, 'timestamp': form.received_on, 'audit_type': 'Item data value updated', 'reason': 'initial value', 'new_value': answer, 'value_type': question, }] mu_oid = get_item_measurement_unit(self._domain_name, item) if mu_oid: item_dict['measurement_unit_oid'] = mu_oid if study_event.start_datetime is None or form.form['meta']['timeStart'] < study_event.start_datetime: study_event.start_datetime = form.form['meta']['timeStart'] if study_event.end_datetime is None or form.form['meta']['timeEnd'] > study_event.end_datetime: study_event.end_datetime = form.form['meta']['timeEnd']