<commit_before>from django.conf import settings
from django.core.cache import cache
from django.urls import reverse, resolve, Resolver404
from django.utils.translation import get_language

from corehq.apps.domain.models import Domain
from corehq.tabs.exceptions import UrlPrefixFormatError, UrlPrefixFormatsSuggestion
from corehq.tabs.utils import sidebar_to_dropdown
from dimagi.utils.decorators.memoized import memoized
from dimagi.utils.django.cache import make_template_fragment_key
from dimagi.utils.web import get_url_base


def url_is_location_safe(url):
    from corehq.apps.locations.permissions import is_location_safe
    url = url.split(get_url_base())[-1] if url else None
    try:
        match = resolve(url)
    except Resolver404:
        return False
    return is_location_safe(match.func, match.args, match.kwargs)


class UITab(object):
    title = None
    view = None

    dispatcher = None

    # Tuple of prefixes that this UITab claims e.g.
    #   ('/a/{domain}/reports/', '/a/{domain}/otherthing/')
    # This is a required field.
    url_prefix_formats = ()
    show_by_default = True

    # must be instance of GaTracker
    ga_tracker = None

    def __init__(self, request, domain=None, couch_user=None, project=None):

        self.domain = domain
        self.couch_user = couch_user
        self._project = project

        # This should not be considered as part of the subclass API unless it
        # is necessary. Try to add new explicit parameters instead.
        self._request = request

        # must be set manually (i.e. `tab.is_active_tab = True`)
        self.is_active_tab = False

        # Do some preemptive checks on the subclass's configuration (if DEBUG)
        if settings.DEBUG:
            if not self.url_prefix_formats:
                raise UrlPrefixFormatsSuggestion(
                    'Class {} must define url_prefix_formats. Try\n'
                    'url_prefix_formats = {}'
                    .format(self.__class__.__name__,
                            self.get_url_prefix_formats_suggestion()))
            for url_prefix_formats in self.url_prefix_formats:
                try:
                    url_prefix_formats.format(domain='')
                except (IndexError, KeyError):
                    raise UrlPrefixFormatError(
                        'Class {} has url_prefix_format has an issue: {}'
                        .format(self.__class__.__name__, url_prefix_formats))

    @property
    def project(self):
        if not self._project:
            self._project = Domain.get_by_name(self.domain)
        return self._project

    @property
    def request_path(self):
        return self._request.get_full_path()

    @property
    def can_access_all_locations(self):
        """Is this a web user who can access project-wide data?"""
        return getattr(self._request, 'can_access_all_locations', True)

    @property
    def dropdown_items(self):
        return sidebar_to_dropdown(sidebar_items=self.sidebar_items,
                                   domain=self.domain, current_url=self.url)<commit_msg>check that domain is supplied<commit_after>from django.conf import settings
from django.core.cache import cache
from django.urls import reverse, resolve, Resolver404
from django.utils.translation import get_language

from corehq.apps.domain.models import Domain
from corehq.tabs.exceptions import UrlPrefixFormatError, UrlPrefixFormatsSuggestion
from corehq.tabs.utils import sidebar_to_dropdown
from dimagi.utils.decorators.memoized import memoized
from dimagi.utils.django.cache import make_template_fragment_key
from dimagi.utils.web import get_url_base


def url_is_location_safe(url):
    from corehq.apps.locations.permissions import is_location_safe
    url = url.split(get_url_base())[-1] if url else None
    try:
        match = resolve(url)
    except Resolver404:
        return False
    return is_location_safe(match.func, match.args, match.kwargs)


class UITab(object):
    title = None
    view = None

    dispatcher = None

    # Tuple of prefixes that this UITab claims e.g.
    #   ('/a/{domain}/reports/', '/a/{domain}/otherthing/')
    # This is a required field.
    url_prefix_formats = ()
    show_by_default = True

    # must be instance of GaTracker
    ga_tracker = None

    def __init__(self, request, domain=None, couch_user=None, project=None):

        self.domain = domain
        self.couch_user = couch_user
        self._project = project

        # This should not be considered as part of the subclass API unless it
        # is necessary. Try to add new explicit parameters instead.
        self._request = request

        # must be set manually (i.e. `tab.is_active_tab = True`)
        self.is_active_tab = False

        # Do some preemptive checks on the subclass's configuration (if DEBUG)
        if settings.DEBUG:
            if not self.url_prefix_formats:
                raise UrlPrefixFormatsSuggestion(
                    'Class {} must define url_prefix_formats. Try\n'
                    'url_prefix_formats = {}'
                    .format(self.__class__.__name__,
                            self.get_url_prefix_formats_suggestion()))
            for url_prefix_formats in self.url_prefix_formats:
                try:
                    url_prefix_formats.format(domain='')
                except (IndexError, KeyError):
                    raise UrlPrefixFormatError(
                        'Class {} has url_prefix_format has an issue: {}'
                        .format(self.__class__.__name__, url_prefix_formats))

    @property
    def project(self):
        if not self._project and self.domain:
            self._project = Domain.get_by_name(self.domain)
        return self._project

    @property
    def request_path(self):
        return self._request.get_full_path()

    @property
    def can_access_all_locations(self):
        """Is this a web user who can access project-wide data?"""
        return getattr(self._request, 'can_access_all_locations', True)

    @property
    def dropdown_items(self):
        return sidebar_to_dropdown(sidebar_items=self.sidebar_items,
                                   domain=self.domain, current_url=self.url)
{'fraction_non_alphanumeric': 0.07592295345104334, 'fraction_numerical': 0.0022471910112359553, 'mean_word_length': 2.508445945945946, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2716428', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1699, 'n_words': 459}