<commit_before>from django.core.management.base import BaseCommand

from goals.models import UserAction, UserBehavior
from notifications.models import GCMDevice, GCMMessage


class Command(BaseCommand):
    help = 'Creates notification Messages for users Actions & Behaviors'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._messages_created = 0

    def create_message(self, user, obj, title, message, delivery_date):
        if delivery_date is None:
            self.stderr.write("{0} has no trigger date".format(obj))
        else:
            try:
                GCMMessage.objects.create(
                    user, obj, title, message, delivery_date
                )
                self._messages_created += 1
            except GCMDevice.DoesNotExist:
                msg = "User {0} has not registered a Device".format(user)
                self.stderr.write(msg)

    def handle(self, *args, **options):
        # Make sure everything is ok before we run this.
        self.check()

        # Schedule notifications for Behaviors
        for ub in UserBehavior.objects.all():
            self.create_message(
                ub.user,
                ub.behavior,
                ub.behavior.title,
                ub.behavior.notification_text,
                ub.behavior.get_trigger().next()
            )

        # Schedule notifications for Actions
        for ua in UserAction.objects.all():
            self.create_message(
                ua.user,
                ua.action,
                ua.action.title,
                ua.action.notification_text,
                ua.action.get_trigger().next()
            )
<commit_msg>update create_notifications so it can write to stderr on failure<commit_after>from django.core.management.base import BaseCommand

from goals.models import UserAction, UserBehavior
from notifications.models import GCMDevice, GCMMessage


class Command(BaseCommand):
    help = 'Creates notification Messages for users Actions & Behaviors'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._messages_created = 0

    def create_message(self, user, obj, title, message, delivery_date):
        if delivery_date is None:
            msg = "{0}-{1} has no trigger date".format(obj.__class__.__name__, obj.id)
            self.stderr.write(msg)
        else:
            try:
                m = GCMMessage.objects.create(
                    user, obj, title, message, delivery_date
                )
                if m is not None:
                    self._messages_created += 1
                else:
                    msg = "Failed to create message for {0}/{1}-{2}".format(
                        user, obj.__class__.__name__, obj.id
                    )
                    self.stderr.write(msg)
            except GCMDevice.DoesNotExist:
                msg = "User {0} has not registered a Device".format(user)
                self.stderr.write(msg)

    def handle(self, *args, **options):
        # Make sure everything is ok before we run this.
        self.check()

        # Schedule notifications for Behaviors
        for ub in UserBehavior.objects.all():
            self.create_message(
                ub.user,
                ub.behavior,
                ub.behavior.title,
                ub.behavior.notification_text,
                ub.behavior.get_trigger().next()
            )

        # Schedule notifications for Actions
        for ua in UserAction.objects.all():
            self.create_message(
                ua.user,
                ua.action,
                ua.action.title,
                ua.action.notification_text,
                ua.action.get_trigger().next()
            )

{'fraction_non_alphanumeric': 0.08774262164318, 'fraction_numerical': 0.0031906407870247273, 'mean_word_length': 1.6775800711743771, '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': '2276057', 'n_tokens_mistral': 1021, 'n_tokens_neox': 934, 'n_words': 252}