<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals
from functools import wraps
from datetime import datetime, timedelta
from django.contrib import messages
from django.db import transaction
from django.http import (
    Http404,
    HttpResponse,
    HttpResponseRedirect,
    HttpResponseBadRequest,
    JsonResponse,
)
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _, ugettext_lazy
from corehq import privileges
from corehq import toggles
from corehq.apps.accounting.decorators import requires_privilege_with_fallback
from corehq.apps.data_interfaces.models import AutomaticUpdateRule, CreateScheduleInstanceActionDefinition
from corehq.apps.domain.models import Domain
from corehq.apps.reminders.models import CaseReminderHandler
from corehq.apps.reminders.views import ScheduledRemindersCalendarView
from corehq.apps.sms.filters import EventTypeFilter, EventStatusFilter
from corehq.apps.sms.models import QueuedSMS, SMS, INCOMING, OUTGOING, MessagingEvent
from corehq.apps.sms.tasks import time_within_windows, OutboundDailyCounter
from corehq.apps.sms.views import BaseMessagingSectionView
from corehq.apps.hqwebapp.async_handler import AsyncHandlerMixin
from corehq.apps.hqwebapp.decorators import use_datatables, use_select2, use_jquery_ui, use_timepicker, use_nvd3
from corehq.apps.hqwebapp.views import DataTablesAJAXPaginationMixin
from corehq.apps.users.decorators import require_permission
from corehq.apps.users.models import Permissions
from corehq.messaging.decorators import reminders_framework_permission
from corehq.messaging.scheduling.async_handlers import MessagingRecipientHandler, ConditionalAlertAsyncHandler
from corehq.messaging.scheduling.forms import (
    BroadcastForm,
    ConditionalAlertForm,
    ConditionalAlertCriteriaForm,
    ConditionalAlertScheduleForm,
)
from corehq.messaging.scheduling.models import (
    AlertSchedule,
    TimedSchedule,
    ImmediateBroadcast,
    ScheduledBroadcast,
)
from corehq.messaging.scheduling.scheduling_partitioned.dbaccessors import (
    get_count_of_active_schedule_instances_due,
)
from corehq.messaging.scheduling.tasks import refresh_alert_schedule_instances, refresh_timed_schedule_instances
from corehq.messaging.tasks import initiate_messaging_rule_run
from corehq.messaging.util import MessagingRuleProgressHelper
from corehq.const import SERVER_DATETIME_FORMAT
from corehq.util.timezones.conversions import ServerTime
from corehq.util.timezones.utils import get_timezone_for_user
from dimagi.utils.couch import CriticalSection
import six
from six.moves import range
from six.moves.urllib.parse import quote_plus


def get_broadcast_edit_critical_section(broadcast_type, broadcast_id):
    return CriticalSection(['edit-broadcast-%s-%s' % (broadcast_type, broadcast_id)], timeout=5 * 60)


def get_conditional_alert_edit_critical_section(rule_id):
    return CriticalSection(['edit-conditional-alert-%s' % rule_id], timeout=5 * 60)


def _requires_new_reminder_framework():
    def decorate(fn):
        @wraps(fn)
        def wrapped(request, *args, **kwargs):
            if (
                hasattr(request, 'couch_user') and
                toggles.NEW_REMINDERS_MIGRATOR.enabled(request.couch_user.username)
            ):
                return fn(request, *args, **kwargs)
            if not hasattr(request, 'project'):
                request.project = Domain.get_by_name(request.domain)
            if request.project.uses_new_reminders:
                return fn(request, *args, **kwargs)
            raise Http404()
        return wrapped
    return decorate


class MessagingDashboardView(BaseMessagingSectionView):
    urlname = 'messaging_dashboard'
    page_title = ugettext_lazy("Dashboard")
    template_name = 'scheduling/dashboard.html'

    @use_nvd3
    def dispatch(self, *args, **kwargs):
        return super(MessagingDashboardView, self).dispatch(*args, **kwargs)

    def get_messaging_history_errors_url(self, messaging_history_url):
        url_param_tuples = [
            ('startdate', self.domain_now.date().strftime('%Y-%m-%d')),
            ('enddate', (self.domain_now.date() - timedelta(days=6)).strftime('%Y-%m-%d')),
            (EventStatusFilter.slug, MessagingEvent.STATUS_ERROR),
        ]

        for event_type, description in EventTypeFilter.options:
            url_param_tuples.append((EventTypeFilter.slug, event_type))

        url_param_list = ['%s=%s' % (quote_plus(name), quote_plus(value)) for name, value in url_param_tuples]
        url_param_str = '&'.join(url_param_list)

        return '%s?%s' % (messaging_history_url, url_param_str)

    @property
    def page_context(self):
        from corehq.apps.reports.standard.sms import (
            ScheduleInstanceReport,
            MessageLogReport,
            MessagingEventsReport,
        )
<commit_msg>Fix reversed start/end dates in msg error report<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals
from functools import wraps
from datetime import datetime, timedelta
from django.contrib import messages
from django.db import transaction
from django.http import (
    Http404,
    HttpResponse,
    HttpResponseRedirect,
    HttpResponseBadRequest,
    JsonResponse,
)
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.translation import ugettext as _, ugettext_lazy
from corehq import privileges
from corehq import toggles
from corehq.apps.accounting.decorators import requires_privilege_with_fallback
from corehq.apps.data_interfaces.models import AutomaticUpdateRule, CreateScheduleInstanceActionDefinition
from corehq.apps.domain.models import Domain
from corehq.apps.reminders.models import CaseReminderHandler
from corehq.apps.reminders.views import ScheduledRemindersCalendarView
from corehq.apps.sms.filters import EventTypeFilter, EventStatusFilter
from corehq.apps.sms.models import QueuedSMS, SMS, INCOMING, OUTGOING, MessagingEvent
from corehq.apps.sms.tasks import time_within_windows, OutboundDailyCounter
from corehq.apps.sms.views import BaseMessagingSectionView
from corehq.apps.hqwebapp.async_handler import AsyncHandlerMixin
from corehq.apps.hqwebapp.decorators import use_datatables, use_select2, use_jquery_ui, use_timepicker, use_nvd3
from corehq.apps.hqwebapp.views import DataTablesAJAXPaginationMixin
from corehq.apps.users.decorators import require_permission
from corehq.apps.users.models import Permissions
from corehq.messaging.decorators import reminders_framework_permission
from corehq.messaging.scheduling.async_handlers import MessagingRecipientHandler, ConditionalAlertAsyncHandler
from corehq.messaging.scheduling.forms import (
    BroadcastForm,
    ConditionalAlertForm,
    ConditionalAlertCriteriaForm,
    ConditionalAlertScheduleForm,
)
from corehq.messaging.scheduling.models import (
    AlertSchedule,
    TimedSchedule,
    ImmediateBroadcast,
    ScheduledBroadcast,
)
from corehq.messaging.scheduling.scheduling_partitioned.dbaccessors import (
    get_count_of_active_schedule_instances_due,
)
from corehq.messaging.scheduling.tasks import refresh_alert_schedule_instances, refresh_timed_schedule_instances
from corehq.messaging.tasks import initiate_messaging_rule_run
from corehq.messaging.util import MessagingRuleProgressHelper
from corehq.const import SERVER_DATETIME_FORMAT
from corehq.util.timezones.conversions import ServerTime
from corehq.util.timezones.utils import get_timezone_for_user
from dimagi.utils.couch import CriticalSection
import six
from six.moves import range
from six.moves.urllib.parse import quote_plus


def get_broadcast_edit_critical_section(broadcast_type, broadcast_id):
    return CriticalSection(['edit-broadcast-%s-%s' % (broadcast_type, broadcast_id)], timeout=5 * 60)


def get_conditional_alert_edit_critical_section(rule_id):
    return CriticalSection(['edit-conditional-alert-%s' % rule_id], timeout=5 * 60)


def _requires_new_reminder_framework():
    def decorate(fn):
        @wraps(fn)
        def wrapped(request, *args, **kwargs):
            if (
                hasattr(request, 'couch_user') and
                toggles.NEW_REMINDERS_MIGRATOR.enabled(request.couch_user.username)
            ):
                return fn(request, *args, **kwargs)
            if not hasattr(request, 'project'):
                request.project = Domain.get_by_name(request.domain)
            if request.project.uses_new_reminders:
                return fn(request, *args, **kwargs)
            raise Http404()
        return wrapped
    return decorate


class MessagingDashboardView(BaseMessagingSectionView):
    urlname = 'messaging_dashboard'
    page_title = ugettext_lazy("Dashboard")
    template_name = 'scheduling/dashboard.html'

    @use_nvd3
    def dispatch(self, *args, **kwargs):
        return super(MessagingDashboardView, self).dispatch(*args, **kwargs)

    def get_messaging_history_errors_url(self, messaging_history_url):
        url_param_tuples = [
            ('startdate', (self.domain_now.date() - timedelta(days=6)).strftime('%Y-%m-%d')),
            ('enddate', self.domain_now.date().strftime('%Y-%m-%d')),
            (EventStatusFilter.slug, MessagingEvent.STATUS_ERROR),
        ]

        for event_type, description in EventTypeFilter.options:
            url_param_tuples.append((EventTypeFilter.slug, event_type))

        url_param_list = ['%s=%s' % (quote_plus(name), quote_plus(value)) for name, value in url_param_tuples]
        url_param_str = '&'.join(url_param_list)

        return '%s?%s' % (messaging_history_url, url_param_str)

    @property
    def page_context(self):
        from corehq.apps.reports.standard.sms import (
            ScheduleInstanceReport,
            MessageLogReport,
            MessagingEventsReport,
        )

{'fraction_non_alphanumeric': 0.07566216891554223, 'fraction_numerical': 0.0031984007996002, 'mean_word_length': 4.555802332037757, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1541130', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2951, 'n_words': 613}