示例#1
0
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)
示例#2
0
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(request.user.username):
            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(request.user.username):
            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'))

                # 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 = [
                    m.case_type for m in app.modules if m.case_type
                ]
                if len(app_case_types):
                    module.case_type = app_case_types[0]
                else:
                    module.case_type = 'case'
            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)
示例#3
0
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)
示例#4
0
 def test_update_attachment(self):
     self.form.requires = 'case'
     self.form.source = self.get_xml('attachment')
     self.form.actions.update_case = UpdateCaseAction(update={'photo': '/data/thepicture'})
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('update_attachment_case'), self.form.render_xform())
示例#5
0
 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())
示例#6
0
 def test_update_case(self):
     self.form.requires = 'case'
     self.form.actions.update_case = UpdateCaseAction(update={'question1': '/data/question1'})
     self.form.actions.update_case.condition.type = 'always'
     self.assertXmlEqual(self.get_xml('update_case'), self.form.render_xform())