def get_form_view_context_and_template(request, domain, form, langs, messages=messages): xform_questions = [] xform = None form_errors = [] xform_validation_errored = False try: xform = form.wrapped_xform() except XFormException as e: form_errors.append(u"Error in form: %s" % e) except Exception as e: logging.exception(e) form_errors.append(u"Unexpected error in form: %s" % e) if xform and xform.exists(): if xform.already_has_meta(): messages.warning( request, "This form has a meta block already! " "It may be replaced by CommCare HQ's standard meta block." ) try: form.validate_form() xform_questions = xform.get_questions(langs, include_triggers=True, form=form) except etree.XMLSyntaxError as e: form_errors.append(u"Syntax Error: %s" % e) except AppEditingError as e: form_errors.append(u"Error in application: %s" % e) except XFormValidationError: xform_validation_errored = True # showing these messages is handled by validate_form_for_build ajax pass except XFormException as e: form_errors.append(u"Error in form: %s" % e) # any other kind of error should fail hard, # but for now there are too many for that to be practical except Exception as e: if settings.DEBUG: raise notify_exception(request, 'Unexpected Build Error') form_errors.append(u"Unexpected System Error: %s" % e) else: # remove upload questions (attachemnts) until MM Case Properties # are released to general public is_previewer = toggles.MM_CASE_PROPERTIES.enabled(request.user.username) xform_questions = [q for q in xform_questions if q["tag"] != "upload" or is_previewer] try: form_action_errors = form.validate_for_build() if not form_action_errors: form.add_stuff_to_xform(xform) except CaseError as e: messages.error(request, u"Error in Case Management: %s" % e) except XFormException as e: messages.error(request, unicode(e)) except Exception as e: if settings.DEBUG: raise logging.exception(unicode(e)) messages.error(request, u"Unexpected Error: %s" % e) try: languages = xform.get_languages() except Exception: languages = [] for err in form_errors: messages.error(request, err) module_case_types = [] app = form.get_app() all_modules = list(app.get_modules()) for module in all_modules: for case_type in module.get_case_types(): module_case_types.append({ 'id': module.unique_id, 'module_name': trans(module.name, langs), 'case_type': case_type, 'module_type': module.doc_type }) if not form.unique_id: form.get_unique_id() app.save() form_has_schedule = isinstance(form, AdvancedForm) and form.get_module().has_schedule module_filter_preview = feature_previews.MODULE_FILTER.enabled(request.domain) context = { 'nav_form': form, 'xform_languages': languages, "xform_questions": xform_questions, 'case_reserved_words_json': load_case_reserved_words(), 'module_case_types': module_case_types, 'form_errors': form_errors, 'xform_validation_errored': xform_validation_errored, 'allow_cloudcare': isinstance(form, Form), 'allow_form_copy': isinstance(form, (Form, AdvancedForm)), 'allow_form_filtering': (module_filter_preview or (not isinstance(form, CareplanForm) and not form_has_schedule)), 'allow_form_workflow': not isinstance(form, CareplanForm), 'allow_usercase': domain_has_privilege(request.domain, privileges.USER_CASE), 'is_usercase_in_use': is_usercase_in_use(request.domain), 'is_module_filter_enabled': (feature_previews.MODULE_FILTER.enabled(request.domain) and app.enable_module_filtering), 'edit_name_url': reverse('edit_form_attr', args=[app.domain, app.id, form.unique_id, 'name']), 'case_xpath_pattern_matches': CASE_XPATH_PATTERN_MATCHES, 'case_xpath_substring_matches': CASE_XPATH_SUBSTRING_MATCHES, 'user_case_xpath_pattern_matches': USER_CASE_XPATH_PATTERN_MATCHES, 'user_case_xpath_substring_matches': USER_CASE_XPATH_SUBSTRING_MATCHES, } if tours.NEW_APP.is_enabled(request.user): request.guided_tour = tours.NEW_APP.get_tour_data() if context['allow_form_workflow'] and toggles.FORM_LINK_WORKFLOW.enabled(domain): module = form.get_module() def qualified_form_name(form, auto_link): module_name = trans(form.get_module().name, langs) form_name = trans(form.name, langs) star = '* ' if auto_link else ' ' return u"{}{} -> {}".format(star, module_name, form_name) modules = filter(lambda m: m.case_type == module.case_type, all_modules) if getattr(module, 'root_module_id', None) and module.root_module not in modules: modules.append(module.root_module) auto_linkable_forms = list(itertools.chain.from_iterable(list(m.get_forms()) for m in modules)) def linkable_form(candidate_form): auto_link = candidate_form in auto_linkable_forms return { 'unique_id': candidate_form.unique_id, 'name': qualified_form_name(candidate_form, auto_link), 'auto_link': auto_link } context['linkable_forms'] = [ linkable_form(candidate_form) for candidate_module in all_modules for candidate_form in candidate_module.get_forms() ] if isinstance(form, CareplanForm): context.update({ 'mode': form.mode, 'fixed_questions': form.get_fixed_questions(), 'custom_case_properties': [ {'key': key, 'path': path} for key, path in form.custom_case_updates.items() ], 'case_preload': [ {'key': key, 'path': path} for key, path in form.case_preload.items() ], }) return "app_manager/form_view_careplan.html", context elif isinstance(form, AdvancedForm): def commtrack_programs(): if app.commtrack_enabled: programs = Program.by_domain(app.domain) return [{'value': program.get_id, 'label': program.name} for program in programs] else: return [] all_programs = [{'value': '', 'label': _('All Programs')}] context.update({ 'show_custom_ref': toggles.APP_BUILDER_CUSTOM_PARENT_REF.enabled(request.user.username), 'commtrack_programs': all_programs + commtrack_programs(), }) context.update(get_schedule_context(form)) return "app_manager/form_view_advanced.html", context else: context.update({ 'show_custom_ref': toggles.APP_BUILDER_CUSTOM_PARENT_REF.enabled(request.user.username), }) return "app_manager/form_view.html", context
def get_form_view_context_and_template(request, domain, form, langs, messages=messages): xform_questions = [] xform = None form_errors = [] xform_validation_errored = False xform_validation_missing = False try: xform = form.wrapped_xform() except XFormException as e: form_errors.append(u"Error in form: %s" % e) except Exception as e: logging.exception(e) form_errors.append(u"Unexpected error in form: %s" % e) if xform and xform.exists(): if xform.already_has_meta(): messages.warning( request, "This form has a meta block already! " "It may be replaced by CommCare HQ's standard meta block." ) try: xform_questions = xform.get_questions(langs, include_triggers=True) form.validate_form() except etree.XMLSyntaxError as e: form_errors.append(u"Syntax Error: %s" % e) except AppEditingError as e: form_errors.append(u"Error in application: %s" % e) except XFormValidationError: xform_validation_errored = True # showing these messages is handled by validate_form_for_build ajax pass except XFormValidationFailed: xform_validation_missing = True messages.warning(request, _("Unable to validate form due to server error.")) except XFormException as e: form_errors.append(u"Error in form: %s" % e) # any other kind of error should fail hard, # but for now there are too many for that to be practical except Exception as e: if settings.DEBUG: raise notify_exception(request, 'Unexpected Build Error') form_errors.append(u"Unexpected System Error: %s" % e) else: # remove upload questions (attachemnts) until MM Case Properties # are released to general public is_previewer = toggles.MM_CASE_PROPERTIES.enabled(request.user.username) xform_questions = [q for q in xform_questions if q["tag"] != "upload" or is_previewer] if not form_errors and not xform_validation_missing and not xform_validation_errored: try: form_action_errors = form.validate_for_build() if not form_action_errors: form.add_stuff_to_xform(xform) except CaseError as e: messages.error(request, u"Error in Case Management: %s" % e) except XFormException as e: messages.error(request, unicode(e)) except Exception as e: if settings.DEBUG: raise logging.exception(unicode(e)) messages.error(request, u"Unexpected Error: %s" % e) try: languages = xform.get_languages() except Exception: languages = [] for err in form_errors: messages.error(request, err) module_case_types = [] app = form.get_app() all_modules = list(app.get_modules()) for module in all_modules: for case_type in module.get_case_types(): module_case_types.append({ 'id': module.unique_id, 'module_name': trans(module.name, langs), 'case_type': case_type, 'module_type': module.doc_type }) if not form.unique_id: form.get_unique_id() app.save() form_has_schedule = isinstance(form, AdvancedForm) and form.get_module().has_schedule context = { 'nav_form': form, 'xform_languages': languages, "xform_questions": xform_questions, 'case_reserved_words_json': load_case_reserved_words(), 'module_case_types': module_case_types, 'form_errors': form_errors, 'xform_validation_errored': xform_validation_errored, 'xform_validation_missing': xform_validation_missing, 'allow_cloudcare': isinstance(form, Form), 'allow_form_copy': isinstance(form, (Form, AdvancedForm)), 'allow_form_filtering': not isinstance(form, CareplanForm) and not form_has_schedule, 'allow_form_workflow': not isinstance(form, CareplanForm), 'uses_form_workflow': form.post_form_workflow == WORKFLOW_FORM, 'allow_usercase': domain_has_privilege(request.domain, privileges.USER_CASE), 'is_usercase_in_use': is_usercase_in_use(request.domain), 'is_module_filter_enabled': app.enable_module_filtering, 'edit_name_url': reverse('edit_form_attr', args=[app.domain, app.id, form.unique_id, 'name']), 'case_xpath_pattern_matches': CASE_XPATH_PATTERN_MATCHES, 'case_xpath_substring_matches': CASE_XPATH_SUBSTRING_MATCHES, 'user_case_xpath_pattern_matches': USER_CASE_XPATH_PATTERN_MATCHES, 'user_case_xpath_substring_matches': USER_CASE_XPATH_SUBSTRING_MATCHES, 'custom_instances': [ {'instanceId': instance.instance_id, 'instancePath': instance.instance_path} for instance in form.custom_instances ], } if tours.NEW_APP.is_enabled(request.user): request.guided_tour = tours.NEW_APP.get_tour_data() if context['allow_form_workflow'] and toggles.FORM_LINK_WORKFLOW.enabled(domain): module = form.get_module() def qualified_form_name(form, auto_link): module_name = trans(form.get_module().name, langs) form_name = trans(form.name, langs) star = '* ' if auto_link else ' ' return u"{}{} -> {}".format(star, module_name, form_name) modules = filter(lambda m: m.case_type == module.case_type, all_modules) if getattr(module, 'root_module_id', None) and module.root_module not in modules: modules.append(module.root_module) auto_linkable_forms = list(itertools.chain.from_iterable(list(m.get_forms()) for m in modules)) def linkable_form(candidate_form): auto_link = candidate_form in auto_linkable_forms return { 'unique_id': candidate_form.unique_id, 'name': qualified_form_name(candidate_form, auto_link), 'auto_link': auto_link } context['linkable_forms'] = [ linkable_form(candidate_form) for candidate_module in all_modules for candidate_form in candidate_module.get_forms() ] if isinstance(form, CareplanForm): context.update({ 'mode': form.mode, 'fixed_questions': form.get_fixed_questions(), 'custom_case_properties': [ {'key': key, 'path': path} for key, path in form.custom_case_updates.items() ], 'case_preload': [ {'key': key, 'path': path} for key, path in form.case_preload.items() ], }) return "app_manager/v1/form_view_careplan.html", context elif isinstance(form, AdvancedForm): def commtrack_programs(): if app.commtrack_enabled: programs = Program.by_domain(app.domain) return [{'value': program.get_id, 'label': program.name} for program in programs] else: return [] all_programs = [{'value': '', 'label': _('All Programs')}] context.update({ 'show_custom_ref': toggles.APP_BUILDER_CUSTOM_PARENT_REF.enabled(request.user.username), 'commtrack_programs': all_programs + commtrack_programs(), }) context.update(get_schedule_context(form)) return "app_manager/v1/form_view_advanced.html", context else: context.update({ 'show_custom_ref': toggles.APP_BUILDER_CUSTOM_PARENT_REF.enabled(request.user.username), }) return "app_manager/v1/form_view.html", context
def get_form_view_context_and_template(request, domain, form, langs, messages=messages): xform_questions = [] xform = None form_errors = [] xform_validation_errored = False xform_validation_missing = False try: xform = form.wrapped_xform() except XFormException as e: form_errors.append(u"Error in form: %s" % e) except Exception as e: logging.exception(e) form_errors.append(u"Unexpected error in form: %s" % e) if xform and xform.exists(): if xform.already_has_meta(): messages.warning( request, "This form has a meta block already! " "It may be replaced by CommCare HQ's standard meta block.") try: xform_questions = xform.get_questions(langs, include_triggers=True) form.validate_form() except etree.XMLSyntaxError as e: form_errors.append(u"Syntax Error: %s" % e) except AppEditingError as e: form_errors.append(u"Error in application: %s" % e) except XFormValidationError: xform_validation_errored = True # showing these messages is handled by validate_form_for_build ajax pass except XFormValidationFailed: xform_validation_missing = True messages.warning(request, _("Unable to validate form due to server error.")) except XFormException as e: form_errors.append(u"Error in form: %s" % e) # any other kind of error should fail hard, # but for now there are too many for that to be practical except Exception as e: if settings.DEBUG: raise notify_exception(request, 'Unexpected Build Error') form_errors.append(u"Unexpected System Error: %s" % e) else: # remove upload questions (attachments) until MM Case Properties # are released to general public is_previewer = toggles.MM_CASE_PROPERTIES.enabled_for_request( request) xform_questions = [ q for q in xform_questions if q["tag"] != "upload" or is_previewer ] if not form_errors and not xform_validation_missing and not xform_validation_errored: try: form_action_errors = form.validate_for_build() if not form_action_errors: form.add_stuff_to_xform(xform) except CaseError as e: messages.error(request, u"Error in Case Management: %s" % e) except XFormException as e: messages.error(request, unicode(e)) except Exception as e: if settings.DEBUG: raise logging.exception(unicode(e)) messages.error(request, u"Unexpected Error: %s" % e) try: languages = xform.get_languages() except Exception: languages = [] for err in form_errors: messages.error(request, err) module_case_types = [] app = form.get_app() all_modules = list(app.get_modules()) for module in all_modules: for case_type in module.get_case_types(): module_case_types.append({ 'id': module.unique_id, 'module_name': trans(module.name, langs), 'case_type': case_type, 'module_type': module.doc_type }) module = form.get_module() if not form.unique_id: form.get_unique_id() app.save() allow_usercase = domain_has_privilege(request.domain, privileges.USER_CASE) valid_index_names = DEFAULT_CASE_INDEX_IDENTIFIERS.values() if allow_usercase: valid_index_names.append(USERCASE_PREFIX[0:-1]) # strip trailing slash form_has_schedule = isinstance(form, AdvancedForm) and module.has_schedule case_config_options = { 'caseType': form.get_case_type(), 'moduleCaseTypes': module_case_types, 'propertiesMap': get_all_case_properties(app), 'propertyDescriptions': get_case_property_description_dict(domain), 'questions': xform_questions, 'reserved_words': load_case_reserved_words(), 'usercasePropertiesMap': get_usercase_properties(app), } context = { 'nav_form': form, 'xform_languages': languages, 'form_errors': form_errors, 'xform_validation_errored': xform_validation_errored, 'xform_validation_missing': xform_validation_missing, 'allow_form_copy': isinstance(form, (Form, AdvancedForm)), 'allow_form_filtering': not form_has_schedule, 'allow_form_workflow': True, 'uses_form_workflow': form.post_form_workflow == WORKFLOW_FORM, 'allow_usercase': allow_usercase, 'is_usercase_in_use': is_usercase_in_use(request.domain), 'is_module_filter_enabled': app.enable_module_filtering, 'root_requires_same_case': module.root_requires_same_case(), 'is_case_list_form': form.is_case_list_form, 'edit_name_url': reverse('edit_form_attr', args=[app.domain, app.id, form.unique_id, 'name']), 'form_filter_patterns': { 'case': CASE_XPATH_PATTERN_MATCHES, 'case_substring': CASE_XPATH_SUBSTRING_MATCHES, 'usercase': USER_CASE_XPATH_PATTERN_MATCHES, 'usercase_substring': USER_CASE_XPATH_SUBSTRING_MATCHES, }, 'custom_instances': [{ 'instanceId': instance.instance_id, 'instancePath': instance.instance_path } for instance in form.custom_instances], 'can_preview_form': request.couch_user.has_permission(domain, 'edit_data'), 'form_icon': None, } if add_ons.show("custom_icon_badges", request, form.get_app()): context[ 'form_icon'] = form.custom_icon if form.custom_icon else CustomIcon( ) if context['allow_form_workflow'] and toggles.FORM_LINK_WORKFLOW.enabled( domain): def qualified_form_name(form, auto_link): module_name = trans(module.name, langs) form_name = trans(form.name, langs) star = '* ' if auto_link else ' ' return u"{}{} -> {}".format(star, module_name, form_name) modules = filter(lambda m: m.case_type == module.case_type, all_modules) if getattr(module, 'root_module_id', None) and module.root_module not in modules: modules.append(module.root_module) auto_linkable_forms = list( itertools.chain.from_iterable( list(m.get_forms()) for m in modules)) def linkable_form(candidate_form): auto_link = candidate_form in auto_linkable_forms return { 'unique_id': candidate_form.unique_id, 'name': qualified_form_name(candidate_form, auto_link), 'auto_link': auto_link } context['linkable_forms'] = [ linkable_form(candidate_form) for candidate_module in all_modules for candidate_form in candidate_module.get_forms() ] if isinstance(form, AdvancedForm): def commtrack_programs(): if app.commtrack_enabled: programs = Program.by_domain(app.domain) return [{ 'value': program.get_id, 'label': program.name } for program in programs] else: return [] all_programs = [{'value': '', 'label': _('All Programs')}] case_config_options.update({ 'commtrack_enabled': app.commtrack_enabled, 'commtrack_programs': all_programs + commtrack_programs(), 'module_id': module.unique_id, 'save_url': reverse("edit_advanced_form_actions", args=[app.domain, app.id, form.unique_id]), }) if form.form_type == "shadow_form": case_config_options.update({ 'actions': form.extra_actions, 'isShadowForm': True, }) else: case_config_options.update({ 'actions': form.actions, 'isShadowForm': False, }) if getattr(module, 'has_schedule', False): schedule_options = get_schedule_context(form) schedule_options.update({ 'phase': schedule_options['schedule_phase'], 'questions': xform_questions, 'save_url': reverse("edit_visit_schedule", args=[app.domain, app.id, form.unique_id]), 'schedule': form.schedule, }) context.update({ 'schedule_options': schedule_options, }) else: context.update({ 'show_custom_ref': toggles.APP_BUILDER_CUSTOM_PARENT_REF.enabled_for_request(request), }) case_config_options.update({ 'actions': form.actions, 'allowUsercase': allow_usercase, 'save_url': reverse("edit_form_actions", args=[app.domain, app.id, form.unique_id]), 'valid_index_names': valid_index_names, }) context.update({'case_config_options': case_config_options}) return "app_manager/form_view.html", context