def test(self): questions_json = [{ "tag": "input", "repeat": None, "group": None, "value": "/data/question4", "label": "Question 4", "type": "Text", }] form_data = { "@uiVersion": "1", "@xmlns": "http://openrosa.org/formdesigner/D096EE34-DF37-466C-B6D9-950A36D570AD", "@name": "Untitled Form", "question4": "foo", "#type": "data", "case": { "@xmlns": "http://commcarehq.org/case/transaction/v2", "@date_modified": "2013-12-23T16:24:20Z", "create": { "case_type": "case", "case_name": "foo", "owner_id": "9ee0367ad4051f0fb33c75eae67d750e" }, "@user_id": "9ee0367ad4051f0fb33c75eae67d750e", "update": "", "@case_id": "6bc190f6-ddeb-4a42-b445-8fa348b50806" }, "meta": { "@xmlns": "http://openrosa.org/jr/xforms", "username": "******", "instanceID": "0398f186-c35f-4437-8b6b-41800807e485", "userID": "9ee0367ad4051f0fb33c75eae67d750e", "timeEnd": "2013-12-23T16:24:20Z", "appVersion": { "@xmlns": "http://commcarehq.org/xforms", "#text": "2.0" }, "timeStart": "2013-12-23T16:24:10Z", "deviceID": "cloudcare" }, "@version": "10" } questions = questions_in_hierarchy([FormQuestionResponse(q) for q in questions_json]) self.assertEqual( [q.to_json() for q in zip_form_data_and_questions(strip_form_data(form_data), questions, path_context='/data/')], [{ "tag": "input", "repeat": None, "group": None, "value": "/data/question4", "label": "Question 4", "response": "foo", "type": "Text", }] )
def _get_form_row(self, form, language): form_summary_rows = [] for question in form.get_questions(self.app.langs, include_triggers=True, include_groups=True, include_translations=True): question_response = FormQuestionResponse(question) form_summary_rows.append( FormSummaryRow( question_id=question_response.value, label=_translate_name(question_response.translations, language), translations=question_response.translations, type=question_response.type, repeat=question_response.repeat, group=question_response.group, options="\n".join([ u"{} - {}".format( _translate_name(option.translations, language), option.value) for option in question_response.options ]), calculate=question_response.calculate, relevant=question_response.relevant, constraint=question_response.constraint, required="true" if question_response.required else "false", comment=question_response.comment, default_value=question_response.setvalue, )) return tuple(form_summary_rows)
def get_form_data(domain, app, include_shadow_forms=True): from corehq.apps.reports.formdetails.readable import FormQuestionResponse from corehq.apps.app_manager.models import ShadowForm modules = [] errors = [] for module in app.get_modules(): forms = [] module_meta = { 'id': module.unique_id, 'name': module.name, 'short_comment': module.short_comment, 'module_type': module.module_type, 'is_surveys': module.is_surveys, 'module_filter': module.module_filter, } form_list = module.get_forms() if not include_shadow_forms: form_list = [f for f in form_list if not isinstance(f, ShadowForm)] for form in form_list: form_meta = { 'id': form.unique_id, 'name': form.name, 'short_comment': form.short_comment, 'action_type': form.get_action_type(), 'form_filter': form.form_filter, } try: questions = form.get_questions(app.langs, include_triggers=True, include_groups=True, include_translations=True) form_meta['questions'] = [ FormQuestionResponse(q).to_json() for q in questions ] except XFormException as e: form_meta['error'] = { 'details': six.text_type(e), 'edit_url': reverse('form_source', args=[domain, app._id, form.unique_id]), } form_meta['module'] = copy(module_meta) errors.append(form_meta) else: forms.append(form_meta) module_meta['forms'] = forms modules.append(module_meta) return modules, errors
def test_get_readable_data_for_submission(self): formxml = get_simple_form_xml('123') xform = submit_form_locally(formxml, self.domain).xform actual, _ = get_readable_data_for_submission(xform) expected = [{ 'value': u'/data/dalmation_count', 'label': u'dalmation_count', 'response': u'yes' }] self.assertJSONEqual( json.dumps([q.to_json() for q in actual]), json.dumps([FormQuestionResponse(q).to_json() for q in expected]))
def get_form_data(self, in_data): modules = [] errors = [] for module in self.app.get_modules(): forms = [] module_meta = { 'id': module.unique_id, 'name': module.name, 'short_comment': module.short_comment, } for form in module.get_forms(): form_meta = { 'id': form.unique_id, 'name': form.name, 'short_comment': form.short_comment, } try: questions = form.get_questions(self.app.langs, include_triggers=True, include_groups=True, include_translations=True) form_meta['questions'] = [ FormQuestionResponse(q).to_json() for q in questions ] except XFormException as e: form_meta['error'] = { 'details': unicode(e), 'edit_url': reverse('form_source', args=[ self.domain, self.app_id, module.id, form.id ]) } form_meta['module'] = copy(module_meta) errors.append(form_meta) else: forms.append(form_meta) module_meta['forms'] = forms modules.append(module_meta) return { 'response': modules, 'errors': errors, 'success': True, }
def get_form_data(domain, app): from corehq.apps.reports.formdetails.readable import FormQuestionResponse modules = [] errors = [] for module in app.get_modules(): forms = [] module_meta = { 'id': module.unique_id, 'name': module.name, 'short_comment': module.short_comment, 'module_type': module.module_type, 'is_surveys': module.is_surveys, } for form in module.get_forms(): form_meta = { 'id': form.unique_id, 'name': form.name, 'short_comment': form.short_comment, 'action_type': form.get_action_type(), } try: questions = form.get_questions(app.langs, include_triggers=True, include_groups=True, include_translations=True) form_meta['questions'] = [ FormQuestionResponse(q).to_json() for q in questions ] except XFormException as e: form_meta['error'] = { 'details': unicode(e), 'edit_url': reverse('form_source', args=[domain, app._id, module.id, form.id]) } form_meta['module'] = copy(module_meta) errors.append(form_meta) else: forms.append(form_meta) module_meta['forms'] = forms modules.append(module_meta) return modules, errors
def test_get_readable_data_for_submission(self): formxml = FormSubmissionBuilder(form_id='123', form_properties={ 'dalmation_count': 'yes' }).as_xml_string() xform = submit_form_locally(formxml, self.domain).xform actual, _ = get_readable_data_for_submission(xform) expected = [{ 'value': '/data/dalmation_count', 'label': 'dalmation_count', 'response': 'yes' }] self.assertJSONEqual( json.dumps([q.to_json() for q in actual]), json.dumps([FormQuestionResponse(q).to_json() for q in expected]))
def xform_display(request, domain, form_unique_id): try: form, app = Form.get_form(form_unique_id, and_app=True) except ResourceNotFound: raise Http404() if domain != app.domain: raise Http404() langs = [request.GET.get('lang')] + app.langs questions = form.get_questions(langs, include_triggers=True, include_groups=True) if request.GET.get('format') == 'html': questions = [FormQuestionResponse(q) for q in questions] return render(request, "app_manager/xform_display.html", {'questions': questions_in_hierarchy(questions)}) else: return json_response(questions)
def _get_form_row(self, form, language, case_meta): form_summary_rows = [] for question in form.get_questions(self.app.langs, include_triggers=True, include_groups=True, include_translations=True): question_response = FormQuestionResponse(question) form_summary_rows.append( FormSummaryRow( question_id=question_response.value, label=_translate_name(question_response.translations, language), translations=question_response.translations, type=question_response.type, repeat=question_response.repeat, group=question_response.group, option_labels="\n".join([ _translate_name(option.translations, language) for option in question_response.options ]), option_values=", ".join([ option.value for option in question_response.options ]), calculate=question_response.calculate, relevant=question_response.relevant, constraint=question_response.constraint, required="true" if question_response.required else "false", comment=question_response.comment, default_value=question_response.setvalue, load_properties="\n".join([ "{} - {}".format(prop.case_type, prop.property) for prop in case_meta.get_load_properties( form.unique_id, question['value']) ]), save_properties="\n".join([ "{} - {}".format(prop.case_type, prop.property) for prop in case_meta.get_save_properties( form.unique_id, question['value']) ]), )) return tuple(form_summary_rows)
def test_repeat(self): questions_json = [{ 'tag': 'input', 'type': 'Text', 'label': 'Text', 'value': '/data/question1', }, { 'tag': 'input', 'type': 'Int', 'label': 'How many names?', 'value': '/data/question18', }, { 'tag': 'repeat', 'type': 'Repeat', 'label': 'Repeat', 'value': '/data/question12', }, { 'tag': 'input', 'type': 'Text', 'label': 'Name', 'value': '/data/question12/name', 'repeat': '/data/question12', 'group': '/data/question12', }, { 'tag': 'trigger', 'type': 'Trigger', 'label': 'Label', 'value': '/data/question2', }, { 'tag': 'select1', 'type': 'Select', 'label': 'Single Answer', 'value': '/data/question3', 'options': [{ 'value': 'item1', 'label': 'Item 1' }, { 'value': 'item2', 'label': 'Item 2' }] }] form_data = { "@uiVersion": "1", "@xmlns": "http://openrosa.org/formdesigner/432B3A7F-6EEE-4033-8740-ACCB0804C4FC", "@name": "Untitled Form", "question18": "3", "#type": "data", "question12": [{ "name": "Jack" }, { "name": "Jill" }, { "name": "Up the hill" }], "meta": { "@xmlns": "http://openrosa.org/jr/xforms", "username": "******", "instanceID": "172981b6-5eeb-4be8-bbc7-ad52f808e803", "userID": "a07d4bd967a9c205287f767509600931", "timeEnd": "2014-04-28T18:27:05Z", "appVersion": { "@xmlns": "http://commcarehq.org/xforms", "#text": "CommCare ODK, version \"2.11.0\"(29272). App v8. CommCare Version 2.11. Build 29272, built on: February-14-2014" }, "timeStart": "2014-04-28T18:26:38Z", "deviceID": "990004280784863" }, "question1": "T", "question3": "item2", "question2": "OK", "@version": "8" } expected = [{ 'tag': 'input', 'type': 'Text', 'label': 'Text', 'value': '/data/question1', 'response': 'T', 'calculate': None, }, { 'tag': 'input', 'type': 'Int', 'label': 'How many names?', 'value': '/data/question18', 'response': '3', 'calculate': None, }, { 'tag': 'repeat', 'type': 'Repeat', 'label': 'Repeat', 'value': '/data/question12', 'response': True, 'calculate': None, 'children': [{ 'children': [{ 'tag': 'input', 'type': 'Text', 'label': 'Name', 'value': '/data/question12/name', 'repeat': '/data/question12', 'group': '/data/question12', 'response': 'Jack', 'calculate': None, }], 'response': True, }, { 'children': [{ 'tag': 'input', 'type': 'Text', 'label': 'Name', 'value': '/data/question12/name', 'repeat': '/data/question12', 'group': '/data/question12', 'response': 'Jill', 'calculate': None, }], 'response': True }, { 'children': [{ 'tag': 'input', 'type': 'Text', 'label': 'Name', 'value': '/data/question12/name', 'repeat': '/data/question12', 'group': '/data/question12', 'response': 'Up the hill', 'calculate': None, }], 'response': True }], }, { 'tag': 'trigger', 'type': 'Trigger', 'label': 'Label', 'value': '/data/question2', 'response': 'OK', 'calculate': None, }, { 'tag': 'select1', 'type': 'Select', 'label': 'Single Answer', 'value': '/data/question3', 'options': [{ 'value': 'item1', 'label': 'Item 1' }, { 'value': 'item2', 'label': 'Item 2' }], 'response': 'item2', 'calculate': None, }] actual = get_readable_form_data( form_data, [FormQuestionResponse(q) for q in questions_json]) self.assertJSONEqual( json.dumps([q.to_json() for q in actual]), json.dumps([FormQuestionResponse(q).to_json() for q in expected]))
def test(self): questions_json = [{ "tag": "input", "repeat": None, "group": None, "value": "/data/question4", "label": "Question 4", "type": "Text", 'calculate': None, 'required': False, 'relevant': None, }] form_data = { "@uiVersion": "1", "@xmlns": "http://openrosa.org/formdesigner/D096EE34-DF37-466C-B6D9-950A36D570AD", "@name": "Untitled Form", "question4": "foo", "#type": "data", "case": { "@xmlns": "http://commcarehq.org/case/transaction/v2", "@date_modified": "2013-12-23T16:24:20Z", "create": { "case_type": "case", "case_name": "foo", "owner_id": "9ee0367ad4051f0fb33c75eae67d750e" }, "@user_id": "9ee0367ad4051f0fb33c75eae67d750e", "update": "", "@case_id": "6bc190f6-ddeb-4a42-b445-8fa348b50806" }, "meta": { "@xmlns": "http://openrosa.org/jr/xforms", "username": "******", "instanceID": "0398f186-c35f-4437-8b6b-41800807e485", "userID": "9ee0367ad4051f0fb33c75eae67d750e", "timeEnd": "2013-12-23T16:24:20Z", "appVersion": { "@xmlns": "http://commcarehq.org/xforms", "#text": "2.0" }, "timeStart": "2013-12-23T16:24:10Z", "deviceID": "cloudcare" }, "@version": "10" } questions = [FormQuestionResponse(q) for q in questions_json] actual = get_readable_form_data(form_data, questions) self.assertJSONEqual( json.dumps([q.to_json() for q in actual]), json.dumps([{ "tag": "input", "repeat": None, "group": None, "value": "/data/question4", "label": "Question 4", "response": "foo", "type": "Text", 'calculate': None, 'comment': None, 'required': False, 'relevant': None, }]))