<commit_before>import json
import logging

from django.contrib import messages
from django.core.cache import cache
from django.http import Http404, HttpResponseRedirect
from django.http.response import HttpResponseServerError
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy, ugettext_noop
from django.views.decorators.http import require_http_methods

from memoized import memoized

from dimagi.utils.couch import get_redis_lock, release_lock
from dimagi.utils.web import json_response
from soil import DownloadBase
from soil.exceptions import TaskFailedError
from soil.util import expose_cached_download, get_download_context

from corehq import toggles
from corehq.apps.commtrack.util import unicode_slug
from corehq.apps.consumption.shortcuts import get_default_monthly_consumption
from corehq.apps.custom_data_fields.edit_model import CustomDataModelMixin
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.domain.views.base import BaseDomainView
from corehq.apps.hqwebapp.crispy import make_form_readonly
from corehq.apps.hqwebapp.decorators import use_jquery_ui, use_multiselect
from corehq.apps.hqwebapp.utils import get_bulk_upload_form
from corehq.apps.hqwebapp.views import no_permissions
from corehq.apps.locations.const import LOCK_LOCATIONS_TIMEOUT
from corehq.apps.locations.permissions import location_safe
from corehq.apps.locations.tasks import (
    download_locations_async,
    import_locations_async,
)
from corehq.apps.products.models import Product, SQLProduct
from corehq.apps.reports.filters.api import EmwfOptionsView
from corehq.apps.reports.filters.controllers import EmwfOptionsController
from corehq.apps.reports.filters.users import ExpandedMobileWorkerFilter
from corehq.apps.users.forms import MultipleSelectionForm
from corehq.util import reverse
from corehq.util.files import file_extention_from_filename
from corehq.util.workbook_json.excel import WorkbookJSONError, get_workbook

from .analytics import users_have_locations
from .const import ROOT_LOCATION_TYPE
from .dbaccessors import get_users_assigned_to_locations
from .exceptions import LocationConsistencyError
from .forms import LocationFormSet, RelatedLocationForm, UsersAtLocationForm, LocationFilterForm
from .models import LocationType, SQLLocation, filter_for_archived
from .permissions import (
    can_edit_location,
    can_edit_location_types,
    can_edit_or_view_location,
    locations_access_required,
    require_can_edit_locations,
    require_can_edit_or_view_locations,
    user_can_edit_location_types,
)
from .tree_utils import assert_no_cycles
from .util import load_locs_json, location_hierarchy_config

logger = logging.getLogger(__name__)


@location_safe
@locations_access_required
def default(request, domain):
    if request.couch_user.can_edit_locations():
        return HttpResponseRedirect(reverse(LocationsListView.urlname, args=[domain]))
    elif user_can_edit_location_types(request.couch_user, domain):
        return HttpResponseRedirect(reverse(LocationTypesView.urlname, args=[domain]))
    return no_permissions(request)


def lock_locations(func):
    # Decorate a post/delete method of a view to ensure concurrent locations edits don't happen.
    def func_wrapper(request, *args, **kwargs):
        key = "import_locations_async-{domain}".format(domain=request.domain)<commit_msg>isort<commit_after>import json
import logging

from django.contrib import messages
from django.core.cache import cache
from django.http import Http404, HttpResponseRedirect
from django.http.response import HttpResponseServerError
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy, ugettext_noop
from django.views.decorators.http import require_http_methods

from memoized import memoized

from dimagi.utils.couch import get_redis_lock, release_lock
from dimagi.utils.web import json_response
from soil import DownloadBase
from soil.exceptions import TaskFailedError
from soil.util import expose_cached_download, get_download_context

from corehq import toggles
from corehq.apps.commtrack.util import unicode_slug
from corehq.apps.consumption.shortcuts import get_default_monthly_consumption
from corehq.apps.custom_data_fields.edit_model import CustomDataModelMixin
from corehq.apps.domain.decorators import domain_admin_required
from corehq.apps.domain.views.base import BaseDomainView
from corehq.apps.hqwebapp.crispy import make_form_readonly
from corehq.apps.hqwebapp.decorators import use_jquery_ui, use_multiselect
from corehq.apps.hqwebapp.utils import get_bulk_upload_form
from corehq.apps.hqwebapp.views import no_permissions
from corehq.apps.locations.const import LOCK_LOCATIONS_TIMEOUT
from corehq.apps.locations.permissions import location_safe
from corehq.apps.locations.tasks import (
    download_locations_async,
    import_locations_async,
)
from corehq.apps.products.models import Product, SQLProduct
from corehq.apps.reports.filters.api import EmwfOptionsView
from corehq.apps.reports.filters.controllers import EmwfOptionsController
from corehq.apps.reports.filters.users import ExpandedMobileWorkerFilter
from corehq.apps.users.forms import MultipleSelectionForm
from corehq.util import reverse
from corehq.util.files import file_extention_from_filename
from corehq.util.workbook_json.excel import WorkbookJSONError, get_workbook

from .analytics import users_have_locations
from .const import ROOT_LOCATION_TYPE
from .dbaccessors import get_users_assigned_to_locations
from .exceptions import LocationConsistencyError
from .forms import (
    LocationFilterForm,
    LocationFormSet,
    RelatedLocationForm,
    UsersAtLocationForm,
)
from .models import LocationType, SQLLocation, filter_for_archived
from .permissions import (
    can_edit_location,
    can_edit_location_types,
    can_edit_or_view_location,
    locations_access_required,
    require_can_edit_locations,
    require_can_edit_or_view_locations,
    user_can_edit_location_types,
)
from .tree_utils import assert_no_cycles
from .util import load_locs_json, location_hierarchy_config

logger = logging.getLogger(__name__)


@location_safe
@locations_access_required
def default(request, domain):
    if request.couch_user.can_edit_locations():
        return HttpResponseRedirect(reverse(LocationsListView.urlname, args=[domain]))
    elif user_can_edit_location_types(request.couch_user, domain):
        return HttpResponseRedirect(reverse(LocationTypesView.urlname, args=[domain]))
    return no_permissions(request)


def lock_locations(func):
    # Decorate a post/delete method of a view to ensure concurrent locations edits don't happen.
    def func_wrapper(request, *args, **kwargs):
        key = "import_locations_async-{domain}".format(domain=request.domain)
{'fraction_non_alphanumeric': 0.05389892202155957, 'fraction_numerical': 0.0016799664006719867, 'mean_word_length': 6.689989235737352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243658', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2056, 'n_words': 505}