def _delete_demo_user_restores(domain_name): from corehq.apps.ota.models import DemoUserRestore from corehq.apps.users.dbaccessors import get_practice_mode_mobile_workers try: users = get_practice_mode_mobile_workers(domain_name) except ESError: # Fallback in case of ES Error users = get_all_commcare_users_by_domain(domain_name) for user in users: if user.demo_restore_id: try: DemoUserRestore.objects.get(id=user.demo_restore_id).delete() except DemoUserRestore.DoesNotExist: pass
def get_apps_base_context(request, domain, app): lang, langs = get_langs(request, app) if getattr(request, 'couch_user', None): timezone = get_timezone_for_user(request.couch_user, domain) else: timezone = None context = { 'lang': lang, 'langs': langs, 'domain': domain, 'app': app, 'app_subset': { 'commcare_minor_release': app.commcare_minor_release, 'doc_type': app.get_doc_type(), 'form_counts_by_module': [len(m.forms) for m in app.modules] if not app.is_remote_app() else [], 'version': app.version, } if app else {}, 'timezone': timezone, } if app and not app.is_remote_app(): app.assert_app_v2() show_advanced = (toggles.APP_BUILDER_ADVANCED.enabled(domain) or getattr(app, 'commtrack_enabled', False)) show_biometric = (toggles.BIOMETRIC_INTEGRATION.enabled(domain) and app.is_biometric_enabled) disable_report_modules = ( is_master_linked_domain(domain) and not toggles.MOBILE_UCR_LINKED_DOMAIN.enabled(domain)) # ideally this should be loaded on demand practice_users = [] if app.enable_practice_users: try: practice_users = get_practice_mode_mobile_workers( request.domain) except ESError: notify_exception(request, 'Error getting practice mode mobile workers') latest_version_for_build_profiles = {} if toggles.RELEASE_BUILDS_PER_PROFILE.enabled(domain): latest_version_for_build_profiles = get_latest_enabled_versions_per_profile( app.get_id) context.update({ 'show_advanced': show_advanced, 'show_biometric': show_biometric, 'show_report_modules': toggles.MOBILE_UCR.enabled(domain), 'disable_report_modules': disable_report_modules, 'show_shadow_modules': toggles.APP_BUILDER_SHADOW_MODULES.enabled(domain), 'show_shadow_module_v1': toggles.V1_SHADOW_MODULES.enabled(domain), 'show_shadow_forms': show_advanced, 'show_training_modules': toggles.TRAINING_MODULE.enabled(domain) and app.enable_training_modules, 'practice_users': [{ "id": u['_id'], "text": u["username"] } for u in practice_users], 'latest_version_for_build_profiles': latest_version_for_build_profiles, }) return context
def get_app_view_context(request, app): """ This provides the context to render commcare settings on Edit Application Settings page This is where additional app or domain specific context can be added to any individual commcare-setting defined in commcare-app-settings.yaml or commcare-profile-settings.yaml """ context = {} settings_layout = copy.deepcopy(get_commcare_settings_layout(app)) for section in settings_layout: new_settings = [] for setting in section['settings']: toggle_name = setting.get('toggle') if toggle_name and not toggle_enabled(request, toggle_name): continue privilege_name = setting.get('privilege') if privilege_name and not has_privilege(request, privilege_name): continue disable_if_true = setting.get('disable_if_true') if disable_if_true and getattr(app, setting['id']): continue if is_linked_app(app): if setting['id'] in app.supported_settings: if setting['id'] not in app.linked_app_attrs: setting['is_inherited'] = True new_settings.append(setting) section['settings'] = new_settings app_view_options = { 'permissions': { 'cloudcare': has_privilege(request, privileges.CLOUDCARE), 'case_sharing_groups': has_privilege(request, privileges.CASE_SHARING_GROUPS), }, 'sections': settings_layout, 'urls': { 'save': reverse("edit_commcare_settings", args=(app.domain, app.id)), }, 'user': { 'is_previewer': request.couch_user.is_previewer(), }, 'values': get_settings_values(app), 'warning': _("This is not an allowed value for this field"), } if (app.get_doc_type() == 'Application' and toggles.CUSTOM_PROPERTIES.enabled(request.domain) and 'custom_properties' in getattr(app, 'profile', {})): custom_properties_array = [{ 'key': p[0], 'value': p[1] } for p in app.profile.get('custom_properties').items()] app_view_options.update({'customProperties': custom_properties_array}) context.update({ 'app_view_options': app_view_options, }) build_config = CommCareBuildConfig.fetch() options = build_config.get_menu() if not request.user.is_superuser and not toggles.IS_CONTRACTOR.enabled( request.user.username): options = [option for option in options if not option.superuser_only] options_map = defaultdict(lambda: {"values": [], "value_names": []}) for option in options: builds = options_map[option.build.major_release()] builds["values"].append(option.build.to_string()) builds["value_names"].append(option.get_label()) if "default" not in builds: app_ver = MAJOR_RELEASE_TO_VERSION[option.build.major_release()] builds["default"] = build_config.get_default(app_ver).to_string() def _get_setting(setting_type, setting_id): # get setting dict from settings_layout if not settings_layout: return None matched = [ x for x in [ setting for section in settings_layout for setting in section['settings'] ] if x['type'] == setting_type and x['id'] == setting_id ] if matched: return matched[0] else: return None build_spec_setting = _get_setting('hq', 'build_spec') if build_spec_setting: build_spec_setting['options_map'] = options_map build_spec_setting['default_app_version'] = app.application_version practice_user_setting = _get_setting('hq', 'practice_mobile_worker_id') if practice_user_setting and has_privilege( request, privileges.PRACTICE_MOBILE_WORKERS): try: practice_users = get_practice_mode_mobile_workers(request.domain) except ESError: notify_exception(request, 'Error getting practice mode mobile workers') practice_users = [] practice_user_setting['values'] = [''] + [ u['_id'] for u in practice_users ] practice_user_setting['value_names'] = [_('Not set')] + [ u['username'] for u in practice_users ] context.update({ 'bulk_ui_translation_upload': { 'action': reverse('upload_bulk_ui_translations', args=(app.domain, app.get_id)), 'download_url': reverse('download_bulk_ui_translations', args=(app.domain, app.get_id)), 'adjective': _("U\u200BI translation"), 'plural_noun': _("U\u200BI translations"), }, 'bulk_app_translation_upload': { 'action': reverse('upload_bulk_app_translations', args=(app.domain, app.get_id)), 'download_url': reverse('download_bulk_app_translations', args=(app.domain, app.get_id)), 'adjective': _("app translation"), 'plural_noun': _("app translations"), 'can_select_language': toggles.BULK_UPDATE_MULTIMEDIA_PATHS.enabled_for_request(request), 'can_validate_app_translations': toggles.VALIDATE_APP_TRANSLATIONS.enabled_for_request(request), }, }) context.update({ 'bulk_ui_translation_form': get_bulk_upload_form( context, context_key="bulk_ui_translation_upload", ), 'bulk_app_translation_form': get_bulk_upload_form( context, context_key="bulk_app_translation_upload", form_class=AppTranslationsBulkUploadForm, ), }) context.update({ 'smart_lang_display_enabled': getattr(app, 'smart_lang_display', False) }) context.update({ 'is_linked_app': is_linked_app(app), 'is_remote_app': is_remote_app(app), }) if is_linked_app(app): try: master_versions_by_id = app.get_latest_master_releases_versions() master_briefs = [ brief for brief in app.get_master_app_briefs() if brief.id in master_versions_by_id ] except RemoteRequestError: messages.error( request, "Unable to reach remote master server. Please try again later." ) master_versions_by_id = {} master_briefs = [] upstream_brief = {} for b in master_briefs: if b.id == app.upstream_app_id: upstream_brief = b context.update({ 'master_briefs': master_briefs, 'master_versions_by_id': master_versions_by_id, 'multiple_masters': app.enable_multi_master and len(master_briefs) > 1, 'upstream_version': app.upstream_version, 'upstream_brief': upstream_brief, 'upstream_url': _get_upstream_url(app, request.couch_user), 'upstream_url_template': _get_upstream_url(app, request.couch_user, master_app_id='---'), }) return context