<commit_before>from __future__ import unicode_literals

from django.db.models.signals import post_save, m2m_changed
from django.dispatch import receiver
from math import ceil

from casepro.cases.models import CaseAction
from casepro.msgs.models import Message, Label, Outgoing
from casepro.cases.models import CaseAction

from .models import datetime_to_date, DailyCount, DailyMinuteTotalCount


@receiver(post_save, sender=Message)
def record_new_incoming(sender, instance, created, **kwargs):
    """
    Records a new outgoing being sent
    """
    if created:
        org = instance.org

        # get day in org timezone
        day = datetime_to_date(instance.created_on, org)

        DailyCount.record_item(day, DailyCount.TYPE_INCOMING, org)


@receiver(post_save, sender=Outgoing)
def record_new_outgoing(sender, instance, created, **kwargs):
    if created and instance.is_reply():
        org = instance.org
        partner = instance.partner
        user = instance.created_by
        case = instance.case

        # get day in org timezone
        day = datetime_to_date(instance.created_on, org)

        DailyCount.record_item(day, DailyCount.TYPE_REPLIES, org)
        DailyCount.record_item(day, DailyCount.TYPE_REPLIES, org, user)

        if instance.partner:
            DailyCount.record_item(day, DailyCount.TYPE_REPLIES, partner)

        if case:
            # count the very first response on an org level
            if instance == case.outgoing_messages.earliest('created_on'):
                td = instance.created_on - case.opened_on
                minutes_since_open = ceil(td.total_seconds() / 60)
                DailyMinuteTotalCount.record_item(day, minutes_since_open,
                                                  DailyMinuteTotalCount.TYPE_TILL_REPLIED, org)

            if case.assignee == partner:
                # count the first response by this partner
                if instance == case.outgoing_messages.filter(partner=partner).earliest('created_on'):
                    # only count the time since this case was (re)assigned to this partner
                    try:
                        action = case.actions.filter(assignee=partner).latest('created_on')
                        start_date = action.created_on
                    except CaseAction.DoesNotExist:
                        start_date = case.opened_on

                    td = instance.created_on - start_date
                    minutes_since_open = ceil(td.total_seconds() / 60)
                    DailyMinuteTotalCount.record_item(day, minutes_since_open,
                                                      DailyMinuteTotalCount.TYPE_TILL_REPLIED, partner)


@receiver(m2m_changed, sender=Message.labels.through)
def record_incoming_labelling(sender, instance, action, reverse, model, pk_set, **kwargs):
    day = datetime_to_date(instance.created_on, instance.org)

    if action == 'post_add':
        for label_id in pk_set:
            DailyCount.record_item(day, DailyCount.TYPE_INCOMING, Label(pk=label_id))
    elif action == 'post_remove':
        for label_id in pk_set:
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING, Label(pk=label_id))
    elif action == 'pre_clear':
        for label in instance.labels.all():
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING, label)


@receiver(post_save, sender=CaseAction)
def record_new_case_action(sender, instance, created, **kwargs):
    """
    This is where we keep track of DailyCounts for users within organisations
    """
    org = instance.case.org
    user = instance.created_by
    partner = instance.case.assignee

    day = datetime_to_date(instance.created_on, instance.case.org)
    if instance.action == CaseAction.OPEN:
        DailyCount.record_item(day, DailyCount.TYPE_CASE_OPENED, org, user)
        DailyCount.record_item(day, DailyCount.TYPE_CASE_OPENED, partner)

    elif instance.action == CaseAction.CLOSE:
        DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, org, user)
        DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, partner)
<commit_msg>Fixed duplicate import from merge<commit_after>from __future__ import unicode_literals

from django.db.models.signals import post_save, m2m_changed
from django.dispatch import receiver
from math import ceil

from casepro.cases.models import CaseAction
from casepro.msgs.models import Message, Label, Outgoing

from .models import datetime_to_date, DailyCount, DailyMinuteTotalCount


@receiver(post_save, sender=Message)
def record_new_incoming(sender, instance, created, **kwargs):
    """
    Records a new outgoing being sent
    """
    if created:
        org = instance.org

        # get day in org timezone
        day = datetime_to_date(instance.created_on, org)

        DailyCount.record_item(day, DailyCount.TYPE_INCOMING, org)


@receiver(post_save, sender=Outgoing)
def record_new_outgoing(sender, instance, created, **kwargs):
    if created and instance.is_reply():
        org = instance.org
        partner = instance.partner
        user = instance.created_by
        case = instance.case

        # get day in org timezone
        day = datetime_to_date(instance.created_on, org)

        DailyCount.record_item(day, DailyCount.TYPE_REPLIES, org)
        DailyCount.record_item(day, DailyCount.TYPE_REPLIES, org, user)

        if instance.partner:
            DailyCount.record_item(day, DailyCount.TYPE_REPLIES, partner)

        if case:
            # count the very first response on an org level
            if instance == case.outgoing_messages.earliest('created_on'):
                td = instance.created_on - case.opened_on
                minutes_since_open = ceil(td.total_seconds() / 60)
                DailyMinuteTotalCount.record_item(day, minutes_since_open,
                                                  DailyMinuteTotalCount.TYPE_TILL_REPLIED, org)

            if case.assignee == partner:
                # count the first response by this partner
                if instance == case.outgoing_messages.filter(partner=partner).earliest('created_on'):
                    # only count the time since this case was (re)assigned to this partner
                    try:
                        action = case.actions.filter(assignee=partner).latest('created_on')
                        start_date = action.created_on
                    except CaseAction.DoesNotExist:
                        start_date = case.opened_on

                    td = instance.created_on - start_date
                    minutes_since_open = ceil(td.total_seconds() / 60)
                    DailyMinuteTotalCount.record_item(day, minutes_since_open,
                                                      DailyMinuteTotalCount.TYPE_TILL_REPLIED, partner)


@receiver(m2m_changed, sender=Message.labels.through)
def record_incoming_labelling(sender, instance, action, reverse, model, pk_set, **kwargs):
    day = datetime_to_date(instance.created_on, instance.org)

    if action == 'post_add':
        for label_id in pk_set:
            DailyCount.record_item(day, DailyCount.TYPE_INCOMING, Label(pk=label_id))
    elif action == 'post_remove':
        for label_id in pk_set:
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING, Label(pk=label_id))
    elif action == 'pre_clear':
        for label in instance.labels.all():
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING, label)


@receiver(post_save, sender=CaseAction)
def record_new_case_action(sender, instance, created, **kwargs):
    """
    This is where we keep track of DailyCounts for users within organisations
    """
    org = instance.case.org
    user = instance.created_by
    partner = instance.case.assignee

    day = datetime_to_date(instance.created_on, instance.case.org)
    if instance.action == CaseAction.OPEN:
        DailyCount.record_item(day, DailyCount.TYPE_CASE_OPENED, org, user)
        DailyCount.record_item(day, DailyCount.TYPE_CASE_OPENED, partner)

    elif instance.action == CaseAction.CLOSE:
        DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, org, user)
        DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, partner)

{'fraction_non_alphanumeric': 0.08014616321559075, 'fraction_numerical': 0.00146163215590743, 'mean_word_length': 2.7003154574132493, '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': '5498804', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2273, 'n_words': 526}