<commit_before>import logging

from django.conf import settings
from django.http import HttpResponseForbidden, HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django.utils.decorators import method_decorator
from firecares.tasks.cache import clear_cache as clear_cache_task
from firecares.tasks.slack import send_slack_message
from firecares.firecares_core.models import AccountRequest

logger = logging.getLogger(__name__)


class FireCARESSlack(View):
    """
    Class that routes and executes Slack commands.
    """
    http_method_names = ['post']
    commands = ['clear_cache', 'account_requests']

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(FireCARESSlack, self).dispatch(*args, **kwargs)

    @property
    def help_text(self):
        """
        Returns a list of commands.
        """

        msg = """clear_cache: Nuclear option, clears the entire cache.
        account_requests: Returns the number of active account requests.
        """

        return {'text': msg}

    def clear_cache(self, request, *args, **kwargs):
        clear_cache_task.apply_async(link=send_slack_message.s(self.response_url, {'text': 'Cache successfully cleared!'}),
                                     link_error=send_slack_message.s(self.response_url, {'text': 'Cache clearing cache.'}))
        return HttpResponse()

    def account_requests(self, request, *args, **kwargs):
        return JsonResponse({'text': 'There are currently {0} pending account requests.'.format(AccountRequest.objects.count())})

    def parse_message(self):
        """
        Parses the incoming message from slack.
        """

        try:
            text = self.request.POST.get('text').split()
            self.command = text[0]
            self.command_args = text[1:]<commit_msg>Add a task to update NFIRS counts.<commit_after>import logging
from django.conf import settings
from django.http import HttpResponseForbidden, HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django.utils.decorators import method_decorator
from firecares.tasks.cache import clear_cache as clear_cache_task
from firecares.tasks.slack import send_slack_message
from firecares.tasks.update import update_nfirs_counts
from firecares.firecares_core.models import AccountRequest

logger = logging.getLogger(__name__)


class FireCARESSlack(View):
    """
    Class that routes and executes Slack commands.
    """
    http_method_names = ['post']
    commands = ['clear_cache', 'account_requests', 'update_nfirs_counts']

    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(FireCARESSlack, self).dispatch(*args, **kwargs)

    @property
    def help_text(self):
        """
        Returns a list of commands.
        """

        msg = """
        *clear_cache*: Nuclear option, clears the entire cache.
        *account_requests*: Returns the number of active account requests.
        *update_nfirs_counts*: Updates the annual residential fire and casualty counts for a department. ```(Args: [<department_id>])```
        """

        return {'text': msg}

    def clear_cache(self, request, *args, **kwargs):
        clear_cache_task.apply_async(link=send_slack_message.s(self.response_url, {'text': 'Cache successfully cleared!'}),
                                     link_error=send_slack_message.s(self.response_url, {'text': 'Cache clearing cache.'}))
        return HttpResponse()

    def account_requests(self, request, *args, **kwargs):
        return JsonResponse({'text': 'There are currently {0} pending account requests.'.format(AccountRequest.objects.count())})

    def update_nfirs_counts(self, request, *args, **kwargs):
        for department in self.command_args:
            update_nfirs_counts.apply_async((department,),
                                        link=send_slack_message.s(self.response_url, {'text': 'NFIRS counts updated for department: {}'.format(department)}),
                                        link_error=send_slack_message.s(self.response_url, {'text': 'Error updating NFIRS counts for department: {}'.format(department)}))
        return HttpResponse()

    def parse_message(self):
        """
        Parses the incoming message from slack.
        """

        try:
            text = self.request.POST.get('text').split()
            self.command = text[0]
            self.command_args = text[1:]
{'fraction_non_alphanumeric': 0.10572493967975433, 'fraction_numerical': 0.0013160780872998464, 'mean_word_length': 3.2379182156133828, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7196033', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1237, 'n_words': 325}