(request, domain, app_id, attr)
return HttpResponse(json.dumps(resp))
Called to edit any (supported) app attribute, given by attr
Called to edit any (supported) app attribute, given by attr
Called to edit any ( supported ) app attribute given by attr
def edit_app_attr(request, domain, app_id, attr):
    """
    Called to edit any (supported) app attribute, given by attr

    """
    app = get_app(domain, app_id)

    try:
        hq_settings = json.loads(request.body.decode('utf-8'))['hq']
    except ValueError:
        hq_settings = request.POST

    can_use_case_sharing = has_privilege(request, privileges.CASE_SHARING_GROUPS)

    attributes = [
        'all',
        'recipients', 'name',
        'text_input', 'platform', 'build_spec',
        'use_custom_suite', 'custom_suite',
        'admin_password',
        'comment',
        'use_j2me_endpoint',
        # Application only
        'cloudcare_enabled',
        'case_sharing',
        'translation_strategy',
        'auto_gps_capture',
        # RemoteApp only
        'profile_url',
        'manage_urls',
        'mobile_ucr_restore_version',
    ]
    if attr not in attributes:
        return HttpResponseBadRequest()

    def should_edit(attribute):
        return attribute == attr or ('all' == attr and attribute in hq_settings)

    def parse_sync_interval(interval):
        try:
            return int(interval)
        except ValueError:
            pass

    resp = {"update": {}}
    # For either type of app

    def _always_allowed(x):
        return True

    easy_attrs = (
        ('build_spec', BuildSpec.from_string, _always_allowed),
        ('practice_mobile_worker_id', None, _always_allowed),
        ('case_sharing', None, lambda x: can_use_case_sharing or getattr(app, x)),
        ('cloudcare_enabled', None, _always_allowed),
        ('manage_urls', None, _always_allowed),
        ('name', None, _always_allowed),
        ('platform', None, _always_allowed),
        ('recipients', None, _always_allowed),
        ('text_input', None, _always_allowed),
        ('use_custom_suite', None, _always_allowed),
        ('secure_submissions', None, _always_allowed),
        ('translation_strategy', None, _always_allowed),
        ('auto_gps_capture', None, _always_allowed),
        ('use_grid_menus', None, _always_allowed),
        ('grid_form_menus', None, _always_allowed),
        ('target_commcare_flavor', None, _always_allowed),
        ('comment', None, _always_allowed),
        ('custom_base_url', None, _always_allowed),
        ('use_j2me_endpoint', None, _always_allowed),
        ('mobile_ucr_restore_version', None, _always_allowed),
        ('location_fixture_restore', None, _always_allowed),
    )
    for attribute, transformation, can_set_attr in easy_attrs:
        if should_edit(attribute):
            value = hq_settings[attribute]
            if transformation:
                value = transformation(value)
            if can_set_attr(attribute):
                setattr(app, attribute, value)
            if is_linked_app(app) and attribute in app.supported_settings:
                app.linked_app_attrs.update({
                    attribute: value,
                })

    if should_edit("name"):
        clear_app_cache(request, domain)
        name = hq_settings['name']
        resp['update'].update({
            '.variable-app_name': name,
            '[data-id="{id}"]'.format(id=app_id): ApplicationsTab.make_app_title(app),
        })

    if should_edit("build_spec"):
        resp['update']['commcare-version'] = app.commcare_minor_release

    if should_edit("practice_mobile_worker_id"):
        user_id = hq_settings['practice_mobile_worker_id']
        if not app.enable_practice_users:
            app.practice_mobile_worker_id = None
        elif user_id:
            get_and_assert_practice_user_in_domain(user_id, request.domain)

    if should_edit("admin_password"):
        admin_password = hq_settings.get('admin_password')
        if admin_password:
            app.set_admin_password(admin_password)

    # For Normal Apps
    if should_edit("cloudcare_enabled"):
        if app.get_doc_type() not in ("Application",):
            raise Exception("App type %s does not support Web Apps" % app.get_doc_type())
        if not has_privilege(request, privileges.CLOUDCARE):
            app.cloudcare_enabled = False

    def require_remote_app():
        if not is_remote_app(app):
            raise Exception("App type %s does not support profile url" % app.get_doc_type())

    # For RemoteApps
    if should_edit("profile_url"):
        require_remote_app()
        app['profile_url'] = hq_settings['profile_url']
    if should_edit("manage_urls"):
        require_remote_app()

    app.save(resp)
    # this is a put_attachment, so it has to go after everything is saved
    if should_edit("custom_suite"):
        app.set_custom_suite(hq_settings['custom_suite'])

    return HttpResponse(json.dumps(resp))
def edit_app_attr ( request , domain , app_id , attr ) : app = get_app ( domain , app_id ) try : hq_settings = json . loads ( request . body . decode ( 'utf-8' ) ) [ 'hq' ] except ValueError : hq_settings = request . POST can_use_case_sharing = has_privilege ( request , privileges . CASE_SHARING_GROUPS ) attributes = [ 'all' , 'recipients' , 'name' , 'text_input' , 'platform' , 'build_spec' , 'use_custom_suite' , 'custom_suite' , 'admin_password' , 'comment' , 'use_j2me_endpoint' , # Application only 'cloudcare_enabled' , 'case_sharing' , 'translation_strategy' , 'auto_gps_capture' , # RemoteApp only 'profile_url' , 'manage_urls' , 'mobile_ucr_restore_version' , ] if attr not in attributes : return HttpResponseBadRequest ( ) def should_edit ( attribute ) : return attribute == attr or ( 'all' == attr and attribute in hq_settings ) def parse_sync_interval ( interval ) : try : return int ( interval ) except ValueError : pass resp = { "update" : { } } # For either type of app def _always_allowed ( x ) : return True easy_attrs = ( ( 'build_spec' , BuildSpec . from_string , _always_allowed ) , ( 'practice_mobile_worker_id' , None , _always_allowed ) , ( 'case_sharing' , None , lambda x : can_use_case_sharing or getattr ( app , x ) ) , ( 'cloudcare_enabled' , None , _always_allowed ) , ( 'manage_urls' , None , _always_allowed ) , ( 'name' , None , _always_allowed ) , ( 'platform' , None , _always_allowed ) , ( 'recipients' , None , _always_allowed ) , ( 'text_input' , None , _always_allowed ) , ( 'use_custom_suite' , None , _always_allowed ) , ( 'secure_submissions' , None , _always_allowed ) , ( 'translation_strategy' , None , _always_allowed ) , ( 'auto_gps_capture' , None , _always_allowed ) , ( 'use_grid_menus' , None , _always_allowed ) , ( 'grid_form_menus' , None , _always_allowed ) , ( 'target_commcare_flavor' , None , _always_allowed ) , ( 'comment' , None , _always_allowed ) , ( 'custom_base_url' , None , _always_allowed ) , ( 'use_j2me_endpoint' , None , _always_allowed ) , ( 'mobile_ucr_restore_version' , None , _always_allowed ) , ( 'location_fixture_restore' , None , _always_allowed ) , ) for attribute , transformation , can_set_attr in easy_attrs : if should_edit ( attribute ) : value = hq_settings [ attribute ] if transformation : value = transformation ( value ) if can_set_attr ( attribute ) : setattr ( app , attribute , value ) if is_linked_app ( app ) and attribute in app . supported_settings : app . linked_app_attrs . update ( { attribute : value , } ) if should_edit ( "name" ) : clear_app_cache ( request , domain ) name = hq_settings [ 'name' ] resp [ 'update' ] . update ( { '.variable-app_name' : name , '[data-id="{id}"]' . format ( id = app_id ) : ApplicationsTab . make_app_title ( app ) , } ) if should_edit ( "build_spec" ) : resp [ 'update' ] [ 'commcare-version' ] = app . commcare_minor_release if should_edit ( "practice_mobile_worker_id" ) : user_id = hq_settings [ 'practice_mobile_worker_id' ] if not app . enable_practice_users : app . practice_mobile_worker_id = None elif user_id : get_and_assert_practice_user_in_domain ( user_id , request . domain ) if should_edit ( "admin_password" ) : admin_password = hq_settings . get ( 'admin_password' ) if admin_password : app . set_admin_password ( admin_password ) # For Normal Apps if should_edit ( "cloudcare_enabled" ) : if app . get_doc_type ( ) not in ( "Application" , ) : raise Exception ( "App type %s does not support Web Apps" % app . get_doc_type ( ) ) if not has_privilege ( request , privileges . CLOUDCARE ) : app . cloudcare_enabled = False def require_remote_app ( ) : if not is_remote_app ( app ) : raise Exception ( "App type %s does not support profile url" % app . get_doc_type ( ) ) # For RemoteApps if should_edit ( "profile_url" ) : require_remote_app ( ) app [ 'profile_url' ] = hq_settings [ 'profile_url' ] if should_edit ( "manage_urls" ) : require_remote_app ( ) app . save ( resp ) # this is a put_attachment, so it has to go after everything is saved if should_edit ( "custom_suite" ) : app . set_custom_suite ( hq_settings [ 'custom_suite' ] ) return HttpResponse ( json . dumps ( resp ) )