(request, domain, app_id)
Adds a module to an app
Adds a module to an app
Adds a module to an app
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 == 'biometrics':
        enroll_module, enroll_form_id = _init_biometrics_enroll_module(app, lang)
        _init_biometrics_identify_module(app, lang, enroll_form_id)

        app.save()

        response = back_to_main(request, domain, app_id=app_id,
                                module_id=enroll_module.id, form_id=0)
        response.set_cookie('suppress_build_errors', 'yes', secure=settings.SECURE_COOKIES)
        return response
    elif 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', secure=settings.SECURE_COOKIES)
        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 new_module ( request , domain , app_id ) : 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 == 'biometrics' : enroll_module , enroll_form_id = _init_biometrics_enroll_module ( app , lang ) _init_biometrics_identify_module ( app , lang , enroll_form_id ) app . save ( ) response = back_to_main ( request , domain , app_id = app_id , module_id = enroll_module . id , form_id = 0 ) response . set_cookie ( 'suppress_build_errors' , 'yes' , secure = settings . SECURE_COOKIES ) return response elif 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' , secure = settings . SECURE_COOKIES ) 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 )