def practice_user_id(): if not app.enable_practice_users: return '' try: practice_user_id = profile.get('practice_user_id') if practice_user_id: get_and_assert_practice_user_in_domain(practice_user_id, domain) return practice_user_id except PracticeUserException: return HttpResponse(status=400)
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)['hq'] except ValueError: hq_settings = request.POST 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 easy_attrs = ( ('build_spec', BuildSpec.from_string), ('practice_mobile_worker_id', None), ('case_sharing', None), ('cloudcare_enabled', None), ('manage_urls', None), ('name', None), ('platform', None), ('recipients', None), ('text_input', None), ('use_custom_suite', None), ('secure_submissions', None), ('translation_strategy', None), ('auto_gps_capture', None), ('use_grid_menus', None), ('grid_form_menus', None), ('target_commcare_flavor', None), ('comment', None), ('custom_base_url', None), ('use_j2me_endpoint', None), ('mobile_ucr_restore_version', None), ('location_fixture_restore', None), ) for attribute, transformation in easy_attrs: if should_edit(attribute): value = hq_settings[attribute] if transformation: value = transformation(value) setattr(app, 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(name, app.doc_type), }) 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 cloudcare" % app.get_doc_type()) if not has_privilege(request, privileges.CLOUDCARE): app.cloudcare_enabled = False def require_remote_app(): if app.get_doc_type() not in ("RemoteApp", ): 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): """ Called to edit any (supported) app attribute, given by attr """ app = get_app(domain, app_id) try: hq_settings = json.loads(request.body)['hq'] except ValueError: hq_settings = request.POST 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 easy_attrs = ( ('build_spec', BuildSpec.from_string), ('practice_mobile_worker_id', None), ('case_sharing', None), ('cloudcare_enabled', None), ('manage_urls', None), ('name', None), ('platform', None), ('recipients', None), ('text_input', None), ('use_custom_suite', None), ('secure_submissions', None), ('translation_strategy', None), ('auto_gps_capture', None), ('use_grid_menus', None), ('grid_form_menus', None), ('target_commcare_flavor', None), ('comment', None), ('custom_base_url', None), ('use_j2me_endpoint', None), ('mobile_ucr_restore_version', None), ('location_fixture_restore', None), ) for attribute, transformation in easy_attrs: if should_edit(attribute): value = hq_settings[attribute] if transformation: value = transformation(value) setattr(app, attribute, value) if app.get_doc_type() == 'LinkedApplication' 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(name, app.doc_type), }) 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 cloudcare" % app.get_doc_type()) if not has_privilege(request, privileges.CLOUDCARE): app.cloudcare_enabled = False def require_remote_app(): if app.get_doc_type() not in ("RemoteApp",): 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))