<commit_before>from __future__ import unicode_literals

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

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

from .models import datetime_to_date, DailyCount


@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

        # 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)


@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.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>deal scenario where partner for a case action is null<commit_after>from __future__ import unicode_literals

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

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

from .models import datetime_to_date, DailyCount


@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

        # 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)


@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.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)
        if partner:
            DailyCount.record_item(day, DailyCount.TYPE_CASE_OPENED, partner)

    elif instance.action == CaseAction.CLOSE:
        DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, org, user)
        if partner:
            DailyCount.record_item(day, DailyCount.TYPE_CASE_CLOSED, partner)
{'fraction_non_alphanumeric': 0.08886430678466077, 'fraction_numerical': 0.0007374631268436578, 'mean_word_length': 3.742132867132867, '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': '3924537', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1636, 'n_words': 382}