<commit_before>from __future__ import absolute_import

from __future__ import unicode_literals
import json
import logging

from django.urls import reverse
from django.utils.translation import ugettext as _
from couchdbkit.exceptions import ResourceConflict
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_GET
from django.conf import settings
from django.contrib import messages

from corehq.apps.app_manager import add_ons
from corehq.apps.app_manager.app_schemas.casedb_schema import get_casedb_schema
from corehq.apps.app_manager.app_schemas.session_schema import get_session_schema
from corehq.apps.domain.decorators import track_domain_request

from dimagi.utils.logging import notify_exception

from corehq.apps.app_manager.views.apps import get_apps_base_context
from corehq.apps.app_manager.views.notifications import get_facility_for_form, notify_form_opened

from corehq.apps.app_manager.exceptions import AppManagerException, \
    FormNotFoundException

from corehq.apps.app_manager.views.utils import back_to_main, bail
from corehq import toggles, privileges
from corehq.apps.accounting.utils import domain_has_privilege
from corehq.apps.app_manager.const import (
    SCHEDULE_CURRENT_VISIT_NUMBER,
    SCHEDULE_NEXT_DUE,
    SCHEDULE_UNSCHEDULED_VISIT,
    SCHEDULE_GLOBAL_NEXT_VISIT_DATE,
)
from corehq.apps.app_manager.util import (
    app_callout_templates,
    is_usercase_in_use,
)
from corehq.apps.cloudcare.utils import should_show_preview_app
from corehq.apps.fixtures.fixturegenerators import item_lists_by_domain
from corehq.apps.app_manager.dbaccessors import get_app
from corehq.apps.app_manager.models import (
    Form,
    ModuleNotFoundException,
)
from corehq.apps.app_manager.decorators import require_can_edit_apps
from corehq.apps.app_manager.templatetags.xforms_extras import translate
from corehq.apps.analytics.tasks import send_hubspot_form, HUBSPOT_FORM_BUILDER_FORM_ID
from corehq.apps.hqwebapp.templatetags.hq_shared_tags import cachebuster
from corehq.util.context_processors import websockets_override


logger = logging.getLogger(__name__)


@require_can_edit_apps
@track_domain_request(calculated_prop='cp_n_form_builder_entered')
def form_source(request, domain, app_id, form_unique_id):
    app = get_app(domain, app_id)
    if app and app.copy_of:
        # redirect to "main" app rather than specific build
        return HttpResponseRedirect(reverse(
            "view_app", args=[domain, app.copy_of]
        ))

    try:
        form = app.get_form(form_unique_id)
    except FormNotFoundException:
        return bail(request, domain, app_id, not_found="form")

    try:
        module = form.get_module()
    except AttributeError:
        return bail(request, domain, app_id, not_found="module")

    return _get_form_designer_view(request, domain, app, module, form)


@require_can_edit_apps
def form_source_legacy(request, domain, app_id, module_id=None, form_id=None):
    """
    This view has been kept around to not break any documentation on example apps
    and partner-distributed documentation on existing apps.
    PLEASE DO NOT DELETE.
    """
    app = get_app(domain, app_id)

    if app and app.copy_of:
        # redirect to "main" app rather than specific build
        return HttpResponseRedirect(reverse(
            "view_app", args=[domain, app.copy_of]
        ))

    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")

    return _get_form_designer_view(request, domain, app, module, form)


def _get_form_designer_view(request, domain, app, module, 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,
                            form_unique_id=form.unique_id)

    send_hubspot_form(HUBSPOT_FORM_BUILDER_FORM_ID, request)

    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
<commit_msg>Redirect in common form designer code with a message<commit_after>from __future__ import absolute_import

from __future__ import unicode_literals
import json
import logging

from django.urls import reverse
from django.utils.translation import ugettext as _
from couchdbkit.exceptions import ResourceConflict
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import require_GET
from django.conf import settings
from django.contrib import messages

from corehq.apps.app_manager import add_ons
from corehq.apps.app_manager.app_schemas.casedb_schema import get_casedb_schema
from corehq.apps.app_manager.app_schemas.session_schema import get_session_schema
from corehq.apps.domain.decorators import track_domain_request

from dimagi.utils.logging import notify_exception

from corehq.apps.app_manager.views.apps import get_apps_base_context
from corehq.apps.app_manager.views.notifications import get_facility_for_form, notify_form_opened

from corehq.apps.app_manager.exceptions import AppManagerException, \
    FormNotFoundException

from corehq.apps.app_manager.views.utils import back_to_main, bail
from corehq import toggles, privileges
from corehq.apps.accounting.utils import domain_has_privilege
from corehq.apps.app_manager.const import (
    SCHEDULE_CURRENT_VISIT_NUMBER,
    SCHEDULE_NEXT_DUE,
    SCHEDULE_UNSCHEDULED_VISIT,
    SCHEDULE_GLOBAL_NEXT_VISIT_DATE,
)
from corehq.apps.app_manager.util import (
    app_callout_templates,
    is_usercase_in_use,
)
from corehq.apps.cloudcare.utils import should_show_preview_app
from corehq.apps.fixtures.fixturegenerators import item_lists_by_domain
from corehq.apps.app_manager.dbaccessors import get_app
from corehq.apps.app_manager.models import (
    Form,
    ModuleNotFoundException,
)
from corehq.apps.app_manager.decorators import require_can_edit_apps
from corehq.apps.app_manager.templatetags.xforms_extras import translate
from corehq.apps.analytics.tasks import send_hubspot_form, HUBSPOT_FORM_BUILDER_FORM_ID
from corehq.apps.hqwebapp.templatetags.hq_shared_tags import cachebuster
from corehq.util.context_processors import websockets_override


logger = logging.getLogger(__name__)


@require_can_edit_apps
@track_domain_request(calculated_prop='cp_n_form_builder_entered')
def form_source(request, domain, app_id, form_unique_id):
    app = get_app(domain, app_id)

    try:
        form = app.get_form(form_unique_id)
    except FormNotFoundException:
        return bail(request, domain, app_id, not_found="form")

    try:
        module = form.get_module()
    except AttributeError:
        return bail(request, domain, app_id, not_found="module")

    return _get_form_designer_view(request, domain, app, module, form)


@require_can_edit_apps
def form_source_legacy(request, domain, app_id, module_id=None, form_id=None):
    """
    This view has been kept around to not break any documentation on example apps
    and partner-distributed documentation on existing apps.
    PLEASE DO NOT DELETE.
    """
    app = get_app(domain, app_id)

    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")

    return _get_form_designer_view(request, domain, app, module, form)


def _get_form_designer_view(request, domain, app, module, form):
    if app and app.copy_of:
        messages.warning(request, _(
            "You tried to edit a form that was from a previous release, so "
            "we have directed you to the latest version of your application."
        ))
        return back_to_main(request, domain, app_id=app.id)

    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,
                            form_unique_id=form.unique_id)

    send_hubspot_form(HUBSPOT_FORM_BUILDER_FORM_ID, request)

    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

{'fraction_non_alphanumeric': 0.06665223977494049, 'fraction_numerical': 0.0023804371348193033, 'mean_word_length': 4.278697886921759, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5554286', 'n_tokens_mistral': 2862, 'n_tokens_neox': 2811, 'n_words': 701}