<commit_before>import json
from corehq import toggles
from corehq.apps.accounting.models import BillingAccountAdmin
from django.contrib import messages
from django.http import Http404, HttpResponse
from django_prbac.decorators import requires_privilege
from django_prbac.exceptions import PermissionDenied
import toggle


def require_billing_admin():
    def decorate(fn):
        """
        Decorator to require the current logged in user to be a billing admin to access the decorated view.
        """
        def wrapped(request, *args, **kwargs):
            if not hasattr(request, 'couch_user') or not hasattr(request, 'domain'):
                raise Http404()
            is_billing_admin = BillingAccountAdmin.get_admin_status_and_account(request.couch_user, request.domain)[0]
            if not (is_billing_admin or request.couch_user.is_superuser):
                raise Http404()
            return fn(request, *args, **kwargs)

        return wrapped

    return decorate


def requires_privilege_alert(slug, **assignment):
    """
    A version of the requires_privilege decorator which inserts an info message into the request
    alerting the user that they will soon loose access to this privilege.
    """
    def decorate(fn):
        def wrapped(request, *args, **kwargs):
            try:
                return requires_privilege(slug, **assignment)(fn)(request, *args, **kwargs)
            except PermissionDenied:
                messaged_slugs = [m.extra_tags for m in messages.get_messages(request)]
                if slug not in messaged_slugs:
                    messages.info(request, "You will soon lose access to this feature", extra_tags=slug)
                return fn(request, *args, **kwargs)
        return wrapped
    return decorate


def requires_privilege_plaintext_response(slug, http_status_code=None, **assignment):
    """
    A version of the requires_privilege decorator which returns an HttpResponse object
    with HTTP Status Code of 412 by default and content_type of tex/plain if the privilege is not found.
    """
    def decorate(fn):
        def wrapped(request, *args, **kwargs):
            if (not hasattr(request, 'user') or
                    not toggle.shortcuts.toggle_enabled(toggles.ACCOUNTING_PREVIEW, request.user.username)):
                return fn(request, *args, **kwargs)
            try:
                return requires_privilege(slug, **assignment)(fn)(request, *args, **kwargs)
            except PermissionDenied:
                return HttpResponse("You have lost access to this feature.",
                                    status=http_status_code or 412, content_type="text/plain")
        return wrapped
    return decorate
<commit_msg>only show alerts for accounting previewers right now, to get christy up and going<commit_after>import json
from corehq import toggles
from corehq.apps.accounting.models import BillingAccountAdmin
from django.contrib import messages
from django.http import Http404, HttpResponse
from django_prbac.decorators import requires_privilege
from django_prbac.exceptions import PermissionDenied
import toggle


def require_billing_admin():
    def decorate(fn):
        """
        Decorator to require the current logged in user to be a billing admin to access the decorated view.
        """
        def wrapped(request, *args, **kwargs):
            if not hasattr(request, 'couch_user') or not hasattr(request, 'domain'):
                raise Http404()
            is_billing_admin = BillingAccountAdmin.get_admin_status_and_account(request.couch_user, request.domain)[0]
            if not (is_billing_admin or request.couch_user.is_superuser):
                raise Http404()
            return fn(request, *args, **kwargs)

        return wrapped

    return decorate


def requires_privilege_alert(slug, **assignment):
    """
    A version of the requires_privilege decorator which inserts an info message into the request
    alerting the user that they will soon loose access to this privilege.
    """
    def decorate(fn):
        def wrapped(request, *args, **kwargs):
            try:
                return requires_privilege(slug, **assignment)(fn)(request, *args, **kwargs)
            except PermissionDenied:
                if toggle.shortcuts.toggle_enabled(toggles.ACCOUNTING_PREVIEW, request.user.username):
                    messaged_slugs = [m.extra_tags for m in messages.get_messages(request)]
                    if slug not in messaged_slugs:
                        messages.info(request, "You will soon lose access to this feature %s" % slug, extra_tags=slug)
                return fn(request, *args, **kwargs)
        return wrapped
    return decorate


def requires_privilege_plaintext_response(slug, http_status_code=None, **assignment):
    """
    A version of the requires_privilege decorator which returns an HttpResponse object
    with HTTP Status Code of 412 by default and content_type of tex/plain if the privilege is not found.
    """
    def decorate(fn):
        def wrapped(request, *args, **kwargs):
            if (not hasattr(request, 'user') or
                    not toggle.shortcuts.toggle_enabled(toggles.ACCOUNTING_PREVIEW, request.user.username)):
                return fn(request, *args, **kwargs)
            try:
                return requires_privilege(slug, **assignment)(fn)(request, *args, **kwargs)
            except PermissionDenied:
                return HttpResponse("You have lost access to this feature.",
                                    status=http_status_code or 412, content_type="text/plain")
        return wrapped
    return decorate

{'fraction_non_alphanumeric': 0.07625465508068807, 'fraction_numerical': 0.005674765029260507, 'mean_word_length': 2.737574552683897, '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': '2474716', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1466, 'n_words': 478}