(request, domain, app_id, module_unique_id, attr)
return HttpResponse(json.dumps(resp))
Called to edit any (supported) module attribute, given by attr
Called to edit any (supported) module attribute, given by attr
Called to edit any ( supported ) module attribute given by attr
def edit_module_attr(request, domain, app_id, module_unique_id, attr):
    """
    Called to edit any (supported) module attribute, given by attr
    """
    attributes = {
        "all": None,
        "auto_select_case": None,
        "case_list": ('case_list-show', 'case_list-label'),
        "case_list-menu_item_media_audio": None,
        "case_list-menu_item_media_image": None,
        'case_list-menu_item_use_default_image_for_all': None,
        'case_list-menu_item_use_default_audio_for_all': None,
        "case_list_form_id": None,
        "case_list_form_label": None,
        "case_list_form_expression": None,
        "case_list_form_media_audio": None,
        "case_list_form_media_image": None,
        'case_list_form_use_default_image_for_all': None,
        'case_list_form_use_default_audio_for_all': None,
        "case_list_post_form_workflow": None,
        "case_type": None,
        "additional_case_types": [],
        'comment': None,
        "display_separately": None,
        "has_schedule": None,
        "media_audio": None,
        "media_image": None,
        "module_filter": None,
        "name": None,
        "parent_module": None,
        "put_in_root": None,
        "report_context_tile": None,
        "root_module_id": None,
        "source_module_id": None,
        "task_list": ('task_list-show', 'task_list-label'),
        "excl_form_ids": None,
        "display_style": None,
        "custom_icon_form": None,
        "custom_icon_text_body": None,
        "custom_icon_xpath": None,
        "use_default_image_for_all": None,
        "use_default_audio_for_all": None,
        "session_endpoint_id": None,
    }

    if attr not in attributes:
        return HttpResponseBadRequest()

    def should_edit(attribute):
        if attribute == attr:
            return True
        if 'all' == attr:
            if attributes[attribute]:
                for param in attributes[attribute]:
                    if not request.POST.get(param):
                        return False
                return True
            else:
                return request.POST.get(attribute) is not None

    app = get_app(domain, app_id)

    try:
        module = app.get_module_by_unique_id(module_unique_id)
    except ModuleNotFoundException:
        # temporary fallback
        module = app.get_module(module_unique_id)

    lang = request.COOKIES.get('lang', app.langs[0])
    resp = {'update': {}, 'corrections': {}}
    if should_edit("custom_icon_form"):
        error_message = handle_custom_icon_edits(request, module, lang)
        if error_message:
            return json_response(
                {'message': error_message},
                status_code=400
            )

    if should_edit("case_type"):
        case_type = request.POST.get("case_type", None)
        if case_type == USERCASE_TYPE and not isinstance(module, AdvancedModule):
            return HttpResponseBadRequest('"{}" is a reserved case type'.format(USERCASE_TYPE))
        elif case_type and not is_valid_case_type(case_type, module):
            return HttpResponseBadRequest("case type is improperly formatted")
        else:
            old_case_type = module["case_type"]
            module["case_type"] = case_type

            # rename other reference to the old case type
            all_advanced_modules = []
            modules_with_old_case_type_exist = False
            for mod in app.modules:
                if isinstance(mod, AdvancedModule):
                    all_advanced_modules.append(mod)

                modules_with_old_case_type_exist |= mod.case_type == old_case_type
            for mod in all_advanced_modules:
                for form in mod.forms:
                    for action in form.actions.get_load_update_actions():
                        if action.case_type == old_case_type and action.details_module == module_unique_id:
                            action.case_type = case_type

                    if mod.unique_id == module_unique_id or not modules_with_old_case_type_exist:
                        for action in form.actions.get_open_actions():
                            if action.case_type == old_case_type:
                                action.case_type = case_type

    if should_edit("put_in_root"):
        module["put_in_root"] = json.loads(request.POST.get("put_in_root"))
    if should_edit("report_context_tile"):
        module["report_context_tile"] = request.POST.get("report_context_tile") == "true"
    if should_edit("display_style"):
        module["display_style"] = request.POST.get("display_style")
    if should_edit("source_module_id") and module["source_module_id"] != request.POST.get("source_module_id"):
        module["source_module_id"] = request.POST.get("source_module_id")
        if handle_shadow_child_modules(app, module):
            # Reload the page to show new shadow child modules
            resp['redirect'] = reverse('view_module', args=[domain, app_id, module_unique_id])
    if should_edit("display_separately"):
        module["display_separately"] = json.loads(request.POST.get("display_separately"))
    if should_edit("parent_module"):
        parent_module = request.POST.get("parent_module")
        module.parent_select.module_id = parent_module
        if module_case_hierarchy_has_circular_reference(module):
            return HttpResponseBadRequest(_("The case hierarchy contains a circular reference."))
    if should_edit("auto_select_case"):
        module["auto_select_case"] = request.POST.get("auto_select_case") == 'true'

    if app.enable_module_filtering and should_edit('module_filter'):
        module['module_filter'] = request.POST.get('module_filter')

    if should_edit('case_list_form_id'):
        module.case_list_form.form_id = request.POST.get('case_list_form_id')
    if should_edit('case_list_form_label'):
        module.case_list_form.label[lang] = request.POST.get('case_list_form_label')
    if should_edit('case_list_form_expression'):
        module.case_list_form.relevancy_expression = request.POST.get('case_list_form_expression')
    if should_edit('case_list_post_form_workflow'):
        module.case_list_form.post_form_workflow = request.POST.get('case_list_post_form_workflow')

    if should_edit("name"):
        name = request.POST.get("name", None)
        module["name"][lang] = name
        resp['update'] = {'.variable-module_name': clean_trans(module.name, [lang])}
    if should_edit('comment'):
        module.comment = request.POST.get('comment')
    for SLUG in ('case_list', 'task_list'):
        show = '{SLUG}-show'.format(SLUG=SLUG)
        label = '{SLUG}-label'.format(SLUG=SLUG)
        if request.POST.get(show) == 'true' and (request.POST.get(label) == ''):
            # Show item, but empty label, was just getting ignored
            return HttpResponseBadRequest("A label is required for {SLUG}".format(SLUG=SLUG))
        if should_edit(SLUG):
            module[SLUG].show = json.loads(request.POST[show])
            module[SLUG].label[lang] = request.POST[label]

    if should_edit("root_module_id"):
        # Make this a child module of 'root_module_id'
        old_root = module['root_module_id']
        if not request.POST.get("root_module_id"):
            module["root_module_id"] = None
        else:
            module["root_module_id"] = request.POST.get("root_module_id")

        # Add or remove children of shadows as required
        shadow_parents = (
            m for m in app.get_modules()
            if m.module_type == "shadow"
            and (m.source_module_id == request.POST.get("root_module_id") or m.source_module_id == old_root)
        )
        for shadow_parent in shadow_parents:
            if handle_shadow_child_modules(app, shadow_parent):
                resp['redirect'] = reverse('view_module', args=[domain, app_id, module_unique_id])

        if not old_root and module['root_module_id']:
            track_workflow(request.couch_user.username, "User associated module with a parent")
        elif old_root and not module['root_module_id']:
            track_workflow(request.couch_user.username, "User orphaned a child module")

    if should_edit('additional_case_types'):
        module.search_config.additional_case_types = list(set(request.POST.getlist('additional_case_types')))

    if should_edit('excl_form_ids') and isinstance(module, ShadowModule):
        excl = request.POST.getlist('excl_form_ids')
        excl.remove('0')  # Placeholder value to make sure excl_form_ids is POSTed when no forms are excluded
        module.excluded_form_ids = excl

    if should_edit('session_endpoint_id'):
        raw_endpoint_id = request.POST['session_endpoint_id']
        try:
            set_session_endpoint(module, raw_endpoint_id, app)
        except InvalidSessionEndpoint as e:
            return HttpResponseBadRequest(str(e))

    handle_media_edits(request, module, should_edit, resp, lang)
    handle_media_edits(request, module.case_list_form, should_edit, resp, lang, prefix='case_list_form_')
    if hasattr(module, 'case_list'):
        handle_media_edits(request, module.case_list, should_edit, resp, lang, prefix='case_list-menu_item_')

    app.save(resp)
    resp['case_list-show'] = module.requires_case_details()
    return HttpResponse(json.dumps(resp))
def edit_module_attr ( request , domain , app_id , module_unique_id , attr ) : attributes = { "all" : None , "auto_select_case" : None , "case_list" : ( 'case_list-show' , 'case_list-label' ) , "case_list-menu_item_media_audio" : None , "case_list-menu_item_media_image" : None , 'case_list-menu_item_use_default_image_for_all' : None , 'case_list-menu_item_use_default_audio_for_all' : None , "case_list_form_id" : None , "case_list_form_label" : None , "case_list_form_expression" : None , "case_list_form_media_audio" : None , "case_list_form_media_image" : None , 'case_list_form_use_default_image_for_all' : None , 'case_list_form_use_default_audio_for_all' : None , "case_list_post_form_workflow" : None , "case_type" : None , "additional_case_types" : [ ] , 'comment' : None , "display_separately" : None , "has_schedule" : None , "media_audio" : None , "media_image" : None , "module_filter" : None , "name" : None , "parent_module" : None , "put_in_root" : None , "report_context_tile" : None , "root_module_id" : None , "source_module_id" : None , "task_list" : ( 'task_list-show' , 'task_list-label' ) , "excl_form_ids" : None , "display_style" : None , "custom_icon_form" : None , "custom_icon_text_body" : None , "custom_icon_xpath" : None , "use_default_image_for_all" : None , "use_default_audio_for_all" : None , "session_endpoint_id" : None , } if attr not in attributes : return HttpResponseBadRequest ( ) def should_edit ( attribute ) : if attribute == attr : return True if 'all' == attr : if attributes [ attribute ] : for param in attributes [ attribute ] : if not request . POST . get ( param ) : return False return True else : return request . POST . get ( attribute ) is not None app = get_app ( domain , app_id ) try : module = app . get_module_by_unique_id ( module_unique_id ) except ModuleNotFoundException : # temporary fallback module = app . get_module ( module_unique_id ) lang = request . COOKIES . get ( 'lang' , app . langs [ 0 ] ) resp = { 'update' : { } , 'corrections' : { } } if should_edit ( "custom_icon_form" ) : error_message = handle_custom_icon_edits ( request , module , lang ) if error_message : return json_response ( { 'message' : error_message } , status_code = 400 ) if should_edit ( "case_type" ) : case_type = request . POST . get ( "case_type" , None ) if case_type == USERCASE_TYPE and not isinstance ( module , AdvancedModule ) : return HttpResponseBadRequest ( '"{}" is a reserved case type' . format ( USERCASE_TYPE ) ) elif case_type and not is_valid_case_type ( case_type , module ) : return HttpResponseBadRequest ( "case type is improperly formatted" ) else : old_case_type = module [ "case_type" ] module [ "case_type" ] = case_type # rename other reference to the old case type all_advanced_modules = [ ] modules_with_old_case_type_exist = False for mod in app . modules : if isinstance ( mod , AdvancedModule ) : all_advanced_modules . append ( mod ) modules_with_old_case_type_exist |= mod . case_type == old_case_type for mod in all_advanced_modules : for form in mod . forms : for action in form . actions . get_load_update_actions ( ) : if action . case_type == old_case_type and action . details_module == module_unique_id : action . case_type = case_type if mod . unique_id == module_unique_id or not modules_with_old_case_type_exist : for action in form . actions . get_open_actions ( ) : if action . case_type == old_case_type : action . case_type = case_type if should_edit ( "put_in_root" ) : module [ "put_in_root" ] = json . loads ( request . POST . get ( "put_in_root" ) ) if should_edit ( "report_context_tile" ) : module [ "report_context_tile" ] = request . POST . get ( "report_context_tile" ) == "true" if should_edit ( "display_style" ) : module [ "display_style" ] = request . POST . get ( "display_style" ) if should_edit ( "source_module_id" ) and module [ "source_module_id" ] != request . POST . get ( "source_module_id" ) : module [ "source_module_id" ] = request . POST . get ( "source_module_id" ) if handle_shadow_child_modules ( app , module ) : # Reload the page to show new shadow child modules resp [ 'redirect' ] = reverse ( 'view_module' , args = [ domain , app_id , module_unique_id ] ) if should_edit ( "display_separately" ) : module [ "display_separately" ] = json . loads ( request . POST . get ( "display_separately" ) ) if should_edit ( "parent_module" ) : parent_module = request . POST . get ( "parent_module" ) module . parent_select . module_id = parent_module if module_case_hierarchy_has_circular_reference ( module ) : return HttpResponseBadRequest ( _ ( "The case hierarchy contains a circular reference." ) ) if should_edit ( "auto_select_case" ) : module [ "auto_select_case" ] = request . POST . get ( "auto_select_case" ) == 'true' if app . enable_module_filtering and should_edit ( 'module_filter' ) : module [ 'module_filter' ] = request . POST . get ( 'module_filter' ) if should_edit ( 'case_list_form_id' ) : module . case_list_form . form_id = request . POST . get ( 'case_list_form_id' ) if should_edit ( 'case_list_form_label' ) : module . case_list_form . label [ lang ] = request . POST . get ( 'case_list_form_label' ) if should_edit ( 'case_list_form_expression' ) : module . case_list_form . relevancy_expression = request . POST . get ( 'case_list_form_expression' ) if should_edit ( 'case_list_post_form_workflow' ) : module . case_list_form . post_form_workflow = request . POST . get ( 'case_list_post_form_workflow' ) if should_edit ( "name" ) : name = request . POST . get ( "name" , None ) module [ "name" ] [ lang ] = name resp [ 'update' ] = { '.variable-module_name' : clean_trans ( module . name , [ lang ] ) } if should_edit ( 'comment' ) : module . comment = request . POST . get ( 'comment' ) for SLUG in ( 'case_list' , 'task_list' ) : show = '{SLUG}-show' . format ( SLUG = SLUG ) label = '{SLUG}-label' . format ( SLUG = SLUG ) if request . POST . get ( show ) == 'true' and ( request . POST . get ( label ) == '' ) : # Show item, but empty label, was just getting ignored return HttpResponseBadRequest ( "A label is required for {SLUG}" . format ( SLUG = SLUG ) ) if should_edit ( SLUG ) : module [ SLUG ] . show = json . loads ( request . POST [ show ] ) module [ SLUG ] . label [ lang ] = request . POST [ label ] if should_edit ( "root_module_id" ) : # Make this a child module of 'root_module_id' old_root = module [ 'root_module_id' ] if not request . POST . get ( "root_module_id" ) : module [ "root_module_id" ] = None else : module [ "root_module_id" ] = request . POST . get ( "root_module_id" ) # Add or remove children of shadows as required shadow_parents = ( m for m in app . get_modules ( ) if m . module_type == "shadow" and ( m . source_module_id == request . POST . get ( "root_module_id" ) or m . source_module_id == old_root ) ) for shadow_parent in shadow_parents : if handle_shadow_child_modules ( app , shadow_parent ) : resp [ 'redirect' ] = reverse ( 'view_module' , args = [ domain , app_id , module_unique_id ] ) if not old_root and module [ 'root_module_id' ] : track_workflow ( request . couch_user . username , "User associated module with a parent" ) elif old_root and not module [ 'root_module_id' ] : track_workflow ( request . couch_user . username , "User orphaned a child module" ) if should_edit ( 'additional_case_types' ) : module . search_config . additional_case_types = list ( set ( request . POST . getlist ( 'additional_case_types' ) ) ) if should_edit ( 'excl_form_ids' ) and isinstance ( module , ShadowModule ) : excl = request . POST . getlist ( 'excl_form_ids' ) excl . remove ( '0' ) # Placeholder value to make sure excl_form_ids is POSTed when no forms are excluded module . excluded_form_ids = excl if should_edit ( 'session_endpoint_id' ) : raw_endpoint_id = request . POST [ 'session_endpoint_id' ] try : set_session_endpoint ( module , raw_endpoint_id , app ) except InvalidSessionEndpoint as e : return HttpResponseBadRequest ( str ( e ) ) handle_media_edits ( request , module , should_edit , resp , lang ) handle_media_edits ( request , module . case_list_form , should_edit , resp , lang , prefix = 'case_list_form_' ) if hasattr ( module , 'case_list' ) : handle_media_edits ( request , module . case_list , should_edit , resp , lang , prefix = 'case_list-menu_item_' ) app . save ( resp ) resp [ 'case_list-show' ] = module . requires_case_details ( ) return HttpResponse ( json . dumps ( resp ) )