def get_releases_context(request, domain, app_id): app = get_app(domain, app_id) context = get_apps_base_context(request, domain, app) can_send_sms = domain_has_privilege(domain, privileges.OUTBOUND_SMS) build_profile_access = domain_has_privilege(domain, privileges.BUILD_PROFILES) prompt_settings_form = PromptUpdateSettingsForm.from_app( app, request_user=request.couch_user) context.update({ 'release_manager': True, 'can_send_sms': can_send_sms, 'has_mobile_workers': get_doc_count_in_domain_by_class(domain, CommCareUser) > 0, 'sms_contacts': (get_sms_autocomplete_context( request, domain)['sms_contacts'] if can_send_sms else []), 'build_profile_access': build_profile_access, 'application_profile_url': reverse(LanguageProfilesView.urlname, args=[domain, app_id]), 'lastest_j2me_enabled_build': CommCareBuildConfig.latest_j2me_enabled_config().label, 'fetchLimit': request.GET.get('limit', DEFAULT_FETCH_LIMIT), 'latest_build_id': get_latest_build_id(domain, app_id), 'prompt_settings_url': reverse(PromptSettingsUpdateView.urlname, args=[domain, app_id]), 'prompt_settings_form': prompt_settings_form, }) if not app.is_remote_app(): context.update({ 'enable_update_prompts': app.enable_update_prompts, }) if not toggles.USER_TESTING_SIMPLIFY.enabled_for_request(request): ab = ab_tests.ABTest(ab_tests.APP_BUILDER_VIDEO, request) context.update({ 'ab_test': ab.context, 'show_video': ab.version == ab_tests.APP_BUILDER_VIDEO_ON, }) if len(app.modules) == 0: context.update({'intro_only': True}) # Multimedia is not supported for remote applications at this time. try: multimedia_state = app.check_media_state() context.update({ 'multimedia_state': multimedia_state, }) except ReportConfigurationNotFoundError: pass return context
def should_show_preview_app(request, domain_obj, username): live_preview_ab = ab_tests.ABTest(ab_tests.LIVE_PREVIEW, request) if domain_obj.is_onboarding_domain and live_preview_ab.version == ab_tests.LIVE_PREVIEW_ENABLED: return True elif domain_obj.is_onboarding_domain and not request.couch_user.is_dimagi: return False return PREVIEW_APP.enabled( domain_obj.name) or PREVIEW_APP.enabled(username)
def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated: # Redirect to a page which lets user choose whether or not to create a new account domains_for_user = Domain.active_for_user(request.user) if len(domains_for_user) == 0: return redirect("registration_domain") else: return redirect("homepage") response = super(UserRegistrationView, self).dispatch(request, *args, **kwargs) ab_tests.ABTest(ab_tests.APPCUES_TEMPLATE_APP, request).update_response(response) return response
def get(self, request, *args, **kwargs): app = get_app(request.domain, kwargs.pop('app_id')) ab_test = ab_tests.ABTest(ab_tests.DATA_FEEDBACK_LOOP, self.request) response = self.render_to_response({ 'app': app, 'formplayer_url': settings.FORMPLAYER_URL, "maps_api_key": settings.GMAPS_API_KEY, "environment": PREVIEW_APP_ENVIRONMENT, 'ab_test': ab_test.context, # sets ab test version if needed }) ab_test.update_response(response) return response
def register_new_user(self, data): reg_form = RegisterWebUserForm(data['data']) if reg_form.is_valid(): ab_test = ab_tests.ABTest(ab_tests.APPCUES_TEMPLATE_APP, self.request) appcues_ab_test = ab_test.context[ 'version'] == ab_tests.APPCUES_TEMPLATE_APP_OPTION_ON self._create_new_account(reg_form, additional_hubspot_data={ "appcues_test": "On" if appcues_ab_test else "Off", }) try: request_new_domain(self.request, reg_form, is_new_user=True) except NameUnavailableException: # technically, the form should never reach this as names are # auto-generated now. But, just in case... logging.error( "There as an issue generating a unique domain name " "for a user during new registration.") return { 'errors': { 'project name unavailable': [], } } username = reg_form.cleaned_data['email'] return { 'success': True, 'appcues_ab_test': appcues_ab_test, } logging.error( "There was an error processing a new user registration form." "This shouldn't happen as validation should be top-notch " "client-side. Here is what the errors are: {}".format( reg_form.errors)) return { 'errors': reg_form.errors, }
def _ab(self): return ab_tests.ABTest(ab_tests.NEW_USER_NUMBER, self.request)
def _ab_show_number(self): return ab_tests.ABTest(ab_tests.NEW_USER_NUMBER, self.request)
def form_designer(request, domain, app_id, module_id=None, form_id=None): def _form_uses_case(module, form): return module and module.case_type and form.requires_case() def _form_is_basic(form): return form.doc_type == 'Form' def _form_too_large(app, form): # form less than 0.1MB, anything larger starts to have # performance issues with fullstory return app.blobs['{}.xml'.format(form.unique_id)]['content_length'] > 102400 meta = get_meta(request) track_entered_form_builder_on_hubspot.delay(request.couch_user, request.COOKIES, meta) app = get_app(domain, app_id) module = None try: module = app.get_module(module_id) except ModuleNotFoundException: return bail(request, domain, app_id, not_found="module") try: form = module.get_form(form_id) except IndexError: return bail(request, domain, app_id, not_found="form") if form.no_vellum: messages.warning(request, _( "You tried to edit this form in the Form Builder. " "However, your administrator has locked this form against editing " "in the form builder, so we have redirected you to " "the form's front page instead." )) return back_to_main(request, domain, app_id=app_id, unique_form_id=form.unique_id) include_fullstory = False vellum_plugins = ["modeliteration", "itemset", "atwho"] if (toggles.COMMTRACK.enabled(domain)): vellum_plugins.append("commtrack") if toggles.VELLUM_SAVE_TO_CASE.enabled(domain): vellum_plugins.append("saveToCase") if (_form_uses_case(module, form) and _form_is_basic(form)): vellum_plugins.append("databrowser") vellum_features = toggles.toggles_dict(username=request.user.username, domain=domain) vellum_features.update(feature_previews.previews_dict(domain)) include_fullstory = not _form_too_large(app, form) vellum_features.update({ 'group_in_field_list': app.enable_group_in_field_list, 'image_resize': app.enable_image_resize, 'markdown_in_groups': app.enable_markdown_in_groups, 'lookup_tables': domain_has_privilege(domain, privileges.LOOKUP_TABLES), 'templated_intents': domain_has_privilege(domain, privileges.TEMPLATED_INTENTS), 'custom_intents': domain_has_privilege(domain, privileges.CUSTOM_INTENTS), 'rich_text': True, }) has_schedule = ( getattr(module, 'has_schedule', False) and getattr(form, 'schedule', False) and form.schedule.enabled ) scheduler_data_nodes = [] if has_schedule: scheduler_data_nodes = [ SCHEDULE_CURRENT_VISIT_NUMBER, SCHEDULE_NEXT_DUE, SCHEDULE_UNSCHEDULED_VISIT, SCHEDULE_GLOBAL_NEXT_VISIT_DATE, ] scheduler_data_nodes.extend([ u"next_{}".format(f.schedule_form_id) for f in form.get_phase().get_forms() if getattr(f, 'schedule', False) and f.schedule.enabled ]) if tours.VELLUM_CASE_MANAGEMENT.is_enabled(request.user) and form.requires_case(): request.guided_tour = tours.VELLUM_CASE_MANAGEMENT.get_tour_data() context = get_apps_base_context(request, domain, app) context.update(locals()) context.update({ 'vellum_debug': settings.VELLUM_DEBUG, 'nav_form': form, 'formdesigner': True, 'multimedia_object_map': app.get_object_map(), 'sessionid': request.COOKIES.get('sessionid'), 'features': vellum_features, 'plugins': vellum_plugins, 'app_callout_templates': next(app_callout_templates), 'scheduler_data_nodes': scheduler_data_nodes, 'include_fullstory': include_fullstory, 'notifications_enabled': request.user.is_superuser, 'notify_facility': get_facility_for_form(domain, app_id, form.unique_id), }) notify_form_opened(domain, request.couch_user, app_id, form.unique_id) live_preview_ab = ab_tests.ABTest(ab_tests.LIVE_PREVIEW, request) domain_obj = Domain.get_by_name(domain) context.update({ 'live_preview_ab': live_preview_ab.context, 'is_onboarding_domain': domain_obj.is_onboarding_domain, 'show_live_preview': should_show_preview_app( request, domain_obj, request.couch_user.username, ) }) template = get_app_manager_template( domain, 'app_manager/v1/form_designer.html', 'app_manager/v2/form_designer.html', ) response = render(request, template, context) live_preview_ab.update_response(response) return response
def view_generic(request, domain, app_id=None, module_id=None, form_id=None, copy_app_form=None, release_manager=False): """ This is the main view for the app. All other views redirect to here. """ if form_id and not module_id: return bail(request, domain, app_id) app = module = form = None try: if app_id: app = get_app(domain, app_id) if module_id: try: module = app.get_module(module_id) except ModuleNotFoundException: raise Http404() if not module.unique_id: module.get_or_create_unique_id() app.save() if form_id: try: form = module.get_form(form_id) except IndexError: raise Http404() except ModuleNotFoundException: return bail(request, domain, app_id) # Application states that should no longer exist if app: if app.application_version == APP_V1: _assert = soft_assert() _assert(False, 'App version 1.0', { 'domain': domain, 'app_id': app_id }) template = get_app_manager_template( domain, 'app_manager/v1/no_longer_supported.html', 'app_manager/v2/no_longer_supported.html', ) return render(request, template, { 'domain': domain, 'app': app, }) if not app.vellum_case_management: # Soft assert but then continue rendering; template will contain a user-facing warning _assert = soft_assert(['jschweers' + '@' + 'dimagi.com']) _assert(False, 'vellum_case_management=False', { 'domain': domain, 'app_id': app_id }) context = get_apps_base_context(request, domain, app) if app and app.copy_of: # don't fail hard. return HttpResponseRedirect( reverse( "view_app", args=[domain, app.copy_of] # TODO - is this right? )) # grandfather in people who set commcare sense earlier if app and 'use_commcare_sense' in app: if app['use_commcare_sense']: if 'features' not in app.profile: app.profile['features'] = {} app.profile['features']['sense'] = 'true' del app['use_commcare_sense'] app.save() context.update({ 'module': module, 'form': form, }) lang = context['lang'] if app and not module and hasattr(app, 'translations'): context.update({"translations": app.translations.get(lang, {})}) if form: template, form_context = get_form_view_context_and_template( request, domain, form, context['langs']) context.update({ 'case_properties': get_all_case_properties(app), 'usercase_properties': get_usercase_properties(app), }) context.update(form_context) elif module: template = get_module_template(domain, module) # make sure all modules have unique ids app.ensure_module_unique_ids(should_save=True) module_context = get_module_view_context(app, module, lang) context.update(module_context) elif app: context.update(get_app_view_context(request, app)) v2_template = ('app_manager/v2/app_view_release_manager.html' if release_manager else 'app_manager/v2/app_view_settings.html') template = get_app_manager_template(domain, 'app_manager/v1/app_view.html', v2_template) if release_manager: context.update(get_releases_context(request, domain, app_id)) context.update({ 'is_app_settings_page': not release_manager, }) else: from corehq.apps.dashboard.views import NewUserDashboardView if toggles.APP_MANAGER_V2.enabled(domain): context.update(NewUserDashboardView.get_page_context(domain)) template = NewUserDashboardView.template_name else: return HttpResponseRedirect( reverse(NewUserDashboardView.urlname, args=[domain])) # update multimedia context for forms and modules. menu_host = form or module if menu_host: default_file_name = 'module%s' % module_id if form_id: default_file_name = '%s_form%s' % (default_file_name, form_id) specific_media = { 'menu': { 'menu_refs': app.get_menu_media(module, module_id, form=form, form_index=form_id, to_language=lang), 'default_file_name': '{name}_{lang}'.format(name=default_file_name, lang=lang), } } if module and module.uses_media(): def _make_name(suffix): return "{default_name}_{suffix}_{lang}".format( default_name=default_file_name, suffix=suffix, lang=lang, ) specific_media['case_list_form'] = { 'menu_refs': app.get_case_list_form_media(module, module_id, to_language=lang), 'default_file_name': _make_name('case_list_form'), } specific_media['case_list_menu_item'] = { 'menu_refs': app.get_case_list_menu_item_media(module, module_id, to_language=lang), 'default_file_name': _make_name('case_list_menu_item'), } specific_media['case_list_lookup'] = { 'menu_refs': app.get_case_list_lookup_image(module, module_id), 'default_file_name': '{}_case_list_lookup'.format(default_file_name), } if hasattr(module, 'product_details'): specific_media['product_list_lookup'] = { 'menu_refs': app.get_case_list_lookup_image(module, module_id, type='product'), 'default_file_name': '{}_product_list_lookup'.format(default_file_name), } context.update({ 'multimedia': { "object_map": app.get_object_map(), 'upload_managers': { 'icon': MultimediaImageUploadController( "hqimage", reverse(ProcessImageFileUploadView.name, args=[app.domain, app.get_id])), 'audio': MultimediaAudioUploadController( "hqaudio", reverse(ProcessAudioFileUploadView.name, args=[app.domain, app.get_id])), }, } }) try: context['multimedia']['references'] = app.get_references() except ReportConfigurationNotFoundError: pass context['multimedia'].update(specific_media) error = request.GET.get('error', '') context.update({ 'error': error, 'app': app, }) # Pass form for Copy Application to template domain_names = [d.name for d in Domain.active_for_user(request.couch_user)] domain_names.sort() if app and copy_app_form is None: toggle_enabled = toggles.EXPORT_ZIPPED_APPS.enabled( request.user.username) copy_app_form = CopyApplicationForm( domain, app_id, export_zipped_apps_enabled=toggle_enabled) context.update({ 'domain_names': domain_names, }) linked_apps_enabled = toggles.LINKED_APPS.enabled(domain) context.update({ 'copy_app_form': copy_app_form, 'linked_apps_enabled': linked_apps_enabled, }) context['latest_commcare_version'] = get_commcare_versions( request.user)[-1] if app and app.doc_type == 'Application' and has_privilege( request, privileges.COMMCARE_LOGO_UPLOADER): uploader_slugs = ANDROID_LOGO_PROPERTY_MAPPING.keys() from corehq.apps.hqmedia.controller import MultimediaLogoUploadController from corehq.apps.hqmedia.views import ProcessLogoFileUploadView context.update({ "sessionid": request.COOKIES.get('sessionid'), 'uploaders': [ MultimediaLogoUploadController( slug, reverse( ProcessLogoFileUploadView.name, args=[domain, app_id, slug], )) for slug in uploader_slugs ], "refs": { slug: ApplicationMediaReference( app.logo_refs.get(slug, {}).get("path", slug), media_class=CommCareImage, module_id=app.logo_refs.get(slug, {}).get("m_id"), ).as_dict() for slug in uploader_slugs }, "media_info": { slug: app.logo_refs.get(slug) for slug in uploader_slugs if app.logo_refs.get(slug) }, }) live_preview_ab = ab_tests.ABTest(ab_tests.LIVE_PREVIEW, request) domain_obj = Domain.get_by_name(domain) context.update({ 'live_preview_ab': live_preview_ab.context, 'is_onboarding_domain': domain_obj.is_onboarding_domain, 'show_live_preview': (toggles.PREVIEW_APP.enabled(domain) or toggles.PREVIEW_APP.enabled(request.couch_user.username) or (domain_obj.is_onboarding_domain and live_preview_ab.version == ab_tests.LIVE_PREVIEW_ENABLED)) }) response = render(request, template, context) live_preview_ab.update_response(response) response.set_cookie('lang', encode_if_unicode(lang)) return response
def ab_persona_field(self): return ab_tests.ABTest(ab_tests.NEW_USER_PERSONA_FIELD, self.request)