def _make_module(self, app, module_id, case_type): m = app.add_module(Module.new_module('Module{}'.format(module_id), lang='en')) m.case_type = case_type mf = app.new_form(module_id, 'form {}'.format(case_type), lang='en') mf.actions.open_case = OpenCaseAction(name_path="/data/question1", external_id=None) mf.actions.open_case.condition.type = 'always' return m
def test_open_case_external_id(self): self.form.actions.open_case = OpenCaseAction( name_update=ConditionalCaseUpdate(question_path="/data/question1"), external_id='/data/question1') self.form.actions.open_case.condition.type = 'always' self.assertXmlEqual(self.get_xml('open_case_external_id'), self.form.render_xform())
def form_opens_case(form, case_type=None, is_subcase=False, parent_tag=None, is_extension=False): if form.form_type == 'module_form': if is_subcase: form.actions.subcases.append(OpenSubCaseAction( case_type=case_type, case_name="/data/name", condition=FormActionCondition(type='always') )) else: form.actions.open_case = OpenCaseAction(name_path="/data/name", external_id=None) form.actions.open_case.condition.type = 'always' else: case_type = case_type or form.get_module().case_type action = AdvancedOpenCaseAction( case_type=case_type, case_tag='open_{}'.format(case_type), name_path='/data/name' ) if is_subcase: if not parent_tag: parent_tag = form.actions.load_update_cases[-1].case_tag action.case_indices = [CaseIndex(tag=parent_tag, relationship='extension' if is_extension else 'child')] form.actions.open_cases.append(action)
def setUp(self): self.app = Application.new_app('domain', 'New App') self.app.version = 3 self.parent_module = self.app.add_module( Module.new_module('New Module', lang='en')) self.parent_form = self.app.new_form(0, 'New Form', lang='en') self.parent_module.case_type = 'parent_test_case_type' self.parent_form.source = self.get_xml('original_form', override_path=('data', )) self.parent_form.actions.open_case = OpenCaseAction( name_path="/data/question1", external_id=None) self.parent_form.actions.open_case.condition.type = 'always' self.module = self.app.add_module( AdvancedModule.new_module('New Module', lang='en')) form = AdvancedForm(name={"en": "Untitled Form"}) self.module.forms.append(form) self.form = self.module.get_form(-1) self.module.case_type = 'test_case_type' self.form.source = self.get_xml('subcase_original') child_module_1 = self.app.add_module( Module.new_module('New Module', lang='en')) child_module_1.case_type = 'child1' child_module_2 = self.app.add_module( Module.new_module('New Module', lang='en')) child_module_2.case_type = 'child2' self.is_usercase_in_use_patch = patch( 'corehq.apps.app_manager.models.is_usercase_in_use') self.is_usercase_in_use_mock = self.is_usercase_in_use_patch.start()
def test_subcase_repeat_mixed_form(self): app = Application.new_app(None, "Untitled Application") module_0 = app.add_module(Module.new_module('parent', None)) module_0.unique_id = 'm0' module_0.case_type = 'parent' form = app.new_form(0, "Form", None, attachment=self.get_xml('subcase_repeat_mixed_form_pre')) module_1 = app.add_module(Module.new_module('subcase', None)) module_1.unique_id = 'm1' module_1.case_type = 'subcase' form.actions.open_case = OpenCaseAction(name_path="/data/parent_name") form.actions.open_case.condition.type = 'always' form.actions.subcases.append(OpenSubCaseAction( case_type=module_1.case_type, case_name="/data/first_child_name", condition=FormActionCondition(type='always') )) # subcase in the middle that has a repeat context form.actions.subcases.append(OpenSubCaseAction( case_type=module_1.case_type, case_name="/data/repeat_child/repeat_child_name", repeat_context='/data/repeat_child', condition=FormActionCondition(type='always') )) form.actions.subcases.append(OpenSubCaseAction( case_type=module_1.case_type, case_name="/data/last_child_name", condition=FormActionCondition(type='always') )) self.assertXmlEqual(self.get_xml('subcase_repeat_mixed_form_post'), app.get_module(0).get_form(0).render_xform())
def new_module(request, domain, app_id): "Adds a module to an app" app = get_app(domain, app_id) from corehq.apps.app_manager.views.utils import get_default_followup_form_xml lang = request.COOKIES.get('lang', app.langs[0]) name = request.POST.get('name') module_type = request.POST.get('module_type', 'case') if module_type == 'case' or module_type == 'survey': # survey option added for V2 if module_type == 'case': name = name or 'Case List' else: name = name or 'Surveys' module = app.add_module(Module.new_module(name, lang)) module_id = module.id form_id = None unstructured = add_ons.show("empty_case_lists", request, app) if module_type == 'case': if not unstructured: form_id = 0 # registration form register = app.new_form(module_id, _("Registration Form"), lang) register.actions.open_case = OpenCaseAction(condition=FormActionCondition(type='always')) register.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) # one followup form msg = _("This is your follow up form. " "Delete this label and add questions for any follow up visits.") attachment = get_default_followup_form_xml(context={'lang': lang, 'default_label': msg}) followup = app.new_form(module_id, _("Followup Form"), lang, attachment=attachment) followup.requires = "case" followup.actions.update_case = UpdateCaseAction(condition=FormActionCondition(type='always')) _init_module_case_type(module) else: form_id = 0 app.new_form(module_id, _("Survey"), lang) app.save() response = back_to_main(request, domain, app_id=app_id, module_id=module_id, form_id=form_id) response.set_cookie('suppress_build_errors', 'yes') return response elif module_type in MODULE_TYPE_MAP: fn = MODULE_TYPE_MAP[module_type][FN] validations = MODULE_TYPE_MAP[module_type][VALIDATIONS] error = next((v[1] for v in validations if v[0](app)), None) if error: messages.warning(request, error) return back_to_main(request, domain, app_id=app.id) else: return fn(request, domain, app, name, lang) else: logger.error('Unexpected module type for new module: "%s"' % module_type) return back_to_main(request, domain, app_id=app_id)
def test_case_list_form_basic(self): self._add_module_and_form(Module) self.form.actions.open_case = OpenCaseAction(name_path="/data/question1", external_id=None) self.form.actions.open_case.condition.type = 'always' self.module.case_list_form.form_id = self.form.get_unique_id() self.assertXmlEqual(self.get_xml('case_list_form_basic_form'), self.form.render_xform())
def _make_module(self, app, module_id, case_type): m = app.add_module(Module.new_module('Module{}'.format(module_id), lang='en')) m.case_type = case_type mf = app.new_form(module_id, 'form {}'.format(case_type), lang='en', attachment=self.get_xml('standard_questions').decode('utf-8')) mf.actions.open_case = OpenCaseAction(name_path="/data/question1", external_id=None) mf.actions.open_case.condition.type = 'always' return m
def test_open_update_case(self): self.form.actions.open_case = OpenCaseAction( name_path="/data/question1", external_id=None) self.form.actions.open_case.condition.type = 'always' self.form.actions.update_case = UpdateCaseAction( update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' self.assertXmlEqual(self.get_xml('open_update_case'), self.form.render_xform())
def setUp(self): self.app = Application.new_app('domain', 'New App', APP_V2) self.app.version = 3 self.module = self.app.add_module(Module.new_module('New Module', lang='en')) self.form = self.app.new_form(0, 'New Form', lang='en') self.module.case_type = 'test_case_type' self.form.source = self.get_xml('original') self.form.actions.open_case = OpenCaseAction(name_path="/data/question1", external_id=None) self.form.actions.open_case.condition.type = 'always' self.careplan_module = new_careplan_module(self.app, None, None, self.module)
def add_reg_form_to_module(module_): reg_form = module_.new_form('Register Subject', None) reg_form.get_unique_id() reg_form.source = self.get_subject_form_source('Register Subject') reg_form.actions.open_case = OpenCaseAction( name_path='/data/' + CC_SUBJECT_KEY, external_id=None, condition=FormActionCondition(type='always')) reg_form.actions.update_case = UpdateCaseAction( update=self.case_update, condition=FormActionCondition(type='always'))
def test_open_case(self): self.form.actions.open_case = OpenCaseAction( name_update=ConditionalCaseUpdate( question_path="/data/question1"), ) self.form.actions.open_case.condition.type = 'always' self.form.actions.update_case = UpdateCaseAction( update={ 'question1': ConditionalCaseUpdate( question_path='/data/question1') }) self.form.actions.update_case.condition.type = 'always' self.assertXmlEqual(self.get_xml('multi_open_update_case'), self.form.render_xform())
def add_form(self, app, case_type=None, module_id=None): if module_id is None: module_id = len(app.modules) m = app.add_module( Module.new_module('Module{}'.format(module_id), lang='en')) if case_type: m.case_type = case_type form = app.new_form(module_id, 'form {}'.format(case_type), lang='en') if case_type: form.actions.open_case = OpenCaseAction( name_path="/data/question1", external_id=None) form.actions.open_case.condition.type = 'always' return form
def test_open_case(self): """Opening cases still works with registry search""" self.form.actions.open_case = OpenCaseAction( name_update=ConditionalCaseUpdate(question_path="/data/question1"), external_id=None) self.form.actions.open_case.condition.type = 'always' self.form.actions.update_case = UpdateCaseAction( update={ 'question1': ConditionalCaseUpdate( question_path='/data/question1') }) self.form.actions.update_case.condition.type = 'always' self.assertXmlEqual(self.get_xml('open_update_case'), self.form.render_xform())
def new_form(request, domain, app_id, module_unique_id): """ Adds a form to an app (under a module) """ app = get_app(domain, app_id) try: module = app.get_module_by_unique_id(module_unique_id) except ModuleNotFoundException: raise HttpResponseBadRequest lang = request.COOKIES.get('lang', app.langs[0]) form_type = request.POST.get('form_type', 'form') case_action = request.POST.get('case_action', 'none') if case_action == 'open': name = _('Register') elif case_action == 'update': name = _('Followup') elif module.is_training_module: name = _('Lesson') else: name = _('Survey') if form_type == "shadow": if module.module_type == "advanced": form = module.new_shadow_form(name, lang) else: raise Exception("Shadow forms may only be created under shadow modules") else: form = module.new_form(name, lang) if form_type != "shadow": if case_action == 'update': form.requires = 'case' form.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) elif case_action == 'open': form.actions.open_case = OpenCaseAction( condition=FormActionCondition(type='always')) form.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) app.save() return back_to_main( request, domain, app_id=app.id, module_unique_id=module.unique_id, form_unique_id=form.unique_id )
def setUp(self): self.app = Application.new_app(DOMAIN, "Untitled Application") self.app._id = '123' self.app.build_spec = BuildSpec(version='2.53.0', build_number=1) self.module = self.app.add_module(Module.new_module("Followup", None)) self.form = self.app.new_form(0, "Untitled Form", None, attachment=get_simple_form("xmlns1.0")) self.form.requires = 'case' self.module.case_type = 'case' self.module.case_details.long.columns.append( DetailColumn.wrap(dict( header={"en": "name"}, model="case", format="plain", field="whatever", )) ) self.module.search_config = CaseSearch( properties=[ CaseSearchProperty(name='name', label={'en': 'Name'}), CaseSearchProperty(name='favorite_color', label={'en': 'Favorite Color'}, itemset=Itemset( instance_id='item-list:colors', instance_uri='jr://fixture/item-list:colors', nodeset="instance('item-list:colors')/colors_list/colors", label='name', sort='name', value='value'), ) ], data_registry="myregistry", data_registry_workflow=REGISTRY_WORKFLOW_LOAD_CASE, additional_registry_cases=["'another case ID'"], ) self.reg_module = self.app.add_module(Module.new_module("Registration", None)) self.reg_form = self.app.new_form(1, "Untitled Form", None, attachment=get_simple_form("xmlns1.0")) self.reg_module.case_type = 'case' self.reg_form.actions.open_case = OpenCaseAction( name_update=ConditionalCaseUpdate(question_path="/data/question1") ) self.reg_form.actions.open_case.condition.type = 'always' self.reg_form.post_form_workflow = WORKFLOW_FORM self.reg_form.form_links = [ FormLink(form_id=self.form.get_unique_id()) ] # wrap to have assign_references called self.app = Application.wrap(self.app.to_json()) # reset to newly wrapped module self.module = self.app.modules[0] self.form = self.module.forms[0]
def test_open_case_and_subcase(self): app = Application.new_app('domain', "Untitled Application") module = app.add_module(Module.new_module('parent', None)) module.case_type = 'phone' module.unique_id = 'm0' form = app.new_form(0, "Untitled Form", None) form.xmlns = 'http://m0-f0' form.actions.open_case = OpenCaseAction(name_path="/data/question1") form.actions.open_case.condition.type = 'always' form.actions.subcases.append( OpenSubCaseAction(case_type='tablet', case_name="/data/question1", condition=FormActionCondition(type='always'))) self.assertXmlPartialEqual(self.get_xml('open_case_and_subcase'), app.create_suite(), "./entry[1]")
def test_subcase_repeat_mixed(self, *args): app = Application.new_app(None, "Untitled Application") module_0 = app.add_module(Module.new_module('parent', None)) module_0.unique_id = 'm0' module_0.case_type = 'parent' form = app.new_form(0, "Form", None) form.actions.open_case = OpenCaseAction( name_update=ConditionalCaseUpdate(question_path="/data/question1")) form.actions.open_case.condition.type = 'always' child_case_type = 'child' form.actions.subcases.append( OpenSubCaseAction(case_type=child_case_type, name_update=ConditionalCaseUpdate( question_path="/data/question1"), condition=FormActionCondition(type='always'))) # subcase in the middle that has a repeat context form.actions.subcases.append( OpenSubCaseAction(case_type=child_case_type, name_update=ConditionalCaseUpdate( question_path="/data/repeat/question1"), repeat_context='/data/repeat', condition=FormActionCondition(type='always'))) form.actions.subcases.append( OpenSubCaseAction(case_type=child_case_type, name_update=ConditionalCaseUpdate( question_path="/data/question1"), condition=FormActionCondition(type='always'))) expected = """ <partial> <session> <datum id="case_id_new_parent_0" function="uuid()"/> <datum id="case_id_new_child_1" function="uuid()"/> <datum id="case_id_new_child_3" function="uuid()"/> </session> </partial> """ self.assertXmlPartialEqual(expected, app.create_suite(), './entry[1]/session')
def form_opens_case(form, case_type=None, is_subcase=False): if form.form_type == 'module_form': if is_subcase: form.actions.subcases.append( OpenSubCaseAction( case_type=case_type, case_name="/data/name", condition=FormActionCondition(type='always'))) else: form.actions.open_case = OpenCaseAction(name_path="/data/name", external_id=None) form.actions.open_case.condition.type = 'always' else: case_type = case_type or form.get_module().case_type action = AdvancedOpenCaseAction( case_type=case_type, case_tag='open_{}'.format(case_type), name_path='/data/name') if is_subcase: action.parent_tag = form.actions.load_update_cases[-1].case_tag form.actions.open_cases.append(action)
def _init_biometrics_enroll_module(app, lang): """ Creates Enrolment Module for Biometrics """ module = app.add_module(Module.new_module(_("Registration"), lang)) form_name = _("Enroll New Person") context = { 'xmlns_uuid': str(uuid.uuid4()).upper(), 'form_name': form_name, 'name_label': _("What is your name?"), 'simprints_enrol_label': _("Scan Fingerprints"), 'lang': lang, } context.update(app.biometric_context) attachment = render_to_string("app_manager/simprints_enrolment_form.xml", context=context) enroll = app.new_form(module.id, form_name, lang, attachment=attachment) enroll.actions.open_case = OpenCaseAction( name_path="/data/name", condition=FormActionCondition(type='always'), ) enroll.actions.update_case = UpdateCaseAction( update={ 'simprintsId': '/data/simprintsId', 'rightIndex': '/data/rightIndex', 'rightThumb': '/data/rightThumb', 'leftIndex': '/data/leftIndex', 'leftThumb': '/data/leftThumb', }, condition=FormActionCondition(type='always'), ) module.case_type = 'person' return module, enroll.get_unique_id()
def new_module(request, domain, app_id): "Adds a module to an app" app = get_app(domain, app_id) lang = request.COOKIES.get('lang', app.langs[0]) name = request.POST.get('name') module_type = request.POST.get('module_type', 'case') if module_type == 'case' or module_type == 'survey': # survey option added for V2 if toggles.APP_MANAGER_V2.enabled(domain): if module_type == 'case': name = name or 'Case List' else: name = name or 'Surveys' module = app.add_module(Module.new_module(name, lang)) module_id = module.id form_id = None if toggles.APP_MANAGER_V2.enabled(domain): if module_type == 'case': # registration form register = app.new_form(module_id, "Register", lang) with open( os.path.join(os.path.dirname(__file__), '..', 'static', 'app_manager', 'xml', 'registration_form.xml')) as f: register.source = f.read() register.actions.open_case = OpenCaseAction( condition=FormActionCondition(type='always'), name_path=u'/data/name') register.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) # make case type unique across app app_case_types = set([ module.case_type for module in app.modules if module.case_type ]) module.case_type = 'case' suffix = 0 while module.case_type in app_case_types: suffix = suffix + 1 module.case_type = 'case-{}'.format(suffix) else: form = app.new_form(module_id, "Survey", lang) form_id = 0 else: app.new_form(module_id, "Untitled Form", lang) app.save() response = back_to_main(request, domain, app_id=app_id, module_id=module_id, form_id=form_id) response.set_cookie('suppress_build_errors', 'yes') return response elif module_type in MODULE_TYPE_MAP: fn = MODULE_TYPE_MAP[module_type][FN] validations = MODULE_TYPE_MAP[module_type][VALIDATIONS] error = next((v[1] for v in validations if v[0](app)), None) if error: messages.warning(request, error) return back_to_main(request, domain, app_id=app.id) else: return fn(request, domain, app, name, lang) else: logger.error('Unexpected module type for new module: "%s"' % module_type) return back_to_main(request, domain, app_id=app_id)
def test_open_case_external_id(self): self.form.actions.open_case = OpenCaseAction( name_path="/data/question1", external_id='/data/question1') self.form.actions.open_case.condition.type = 'always' check_xml_line_by_line(self, self.get_xml('open_case_external_id'), self.form.render_xform())
def new_module(request, domain, app_id): "Adds a module to an app" app = get_app(domain, app_id) lang = request.COOKIES.get('lang', app.langs[0]) name = request.POST.get('name') module_type = request.POST.get('module_type', 'case') if module_type == 'case' or module_type == 'survey': # survey option added for V2 if toggles.APP_MANAGER_V2.enabled(domain): if module_type == 'case': name = name or 'Case List' else: name = name or 'Surveys' module = app.add_module(Module.new_module(name, lang)) module_id = module.id form_id = None if toggles.APP_MANAGER_V2.enabled(domain): if module_type == 'case': # registration form register = app.new_form(module_id, "Register", lang) register.actions.open_case = OpenCaseAction( condition=FormActionCondition(type='always')) register.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) # set up reg from case list module.case_list_form.form_id = register.unique_id module.case_list_form.label = register.name register.form_filter = "false()" # one followup form followup = app.new_form(module_id, "Followup", lang) followup.requires = "case" followup.actions.update_case = UpdateCaseAction( condition=FormActionCondition(type='always')) # make case type unique across app app_case_types = set([ module.case_type for module in app.modules if module.case_type ]) module.case_type = 'case' suffix = 0 while module.case_type in app_case_types: suffix = suffix + 1 module.case_type = 'case-{}'.format(suffix) else: app.new_form(module_id, "Survey", lang) form_id = 0 else: app.new_form(module_id, "Untitled Form", lang) app.save() response = back_to_main(request, domain, app_id=app_id, module_id=module_id, form_id=form_id) response.set_cookie('suppress_build_errors', 'yes') return response elif module_type in MODULE_TYPE_MAP: fn = MODULE_TYPE_MAP[module_type][FN] validations = MODULE_TYPE_MAP[module_type][VALIDATIONS] error = next((v[1] for v in validations if v[0](app)), None) if error: messages.warning(request, error) return back_to_main(request, domain, app_id=app.id) else: return fn(request, domain, app, name, lang) else: logger.error('Unexpected module type for new module: "%s"' % module_type) return back_to_main(request, domain, app_id=app_id)