<commit_before>import datetime

from django.conf import settings
from django.core.management.base import BaseCommand
from django.template.loader import get_template
from django.template import Context

from periods import models as period_models, email_sender


class Command(BaseCommand):
    help = 'Notify users of upcoming periods'

    def _format_date(self, date_value):
        return date_value.strftime('%A %B %d, %Y')

    def handle(self, *args, **options):
        users = period_models.User.objects.filter(
            is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude(
            send_emails=False).distinct()
        for user in users:
            expected_in = (user.statistics.average_cycle_length
                           - user.statistics.current_cycle_length)
            expected_abs = abs(expected_in)
            # TODO This logic may be duplicated from statistics
            today = period_models.today()
            expected_date = today + datetime.timedelta(
                days=expected_in)
            if expected_abs == 1:
                day = 'day'
            else:
                day = 'days'

            context = Context({
                'full_name': user.get_full_name(),
                'today': self._format_date(today),
                'expected_in': expected_abs,
                'day': day,
                'expected_date': self._format_date(expected_date),
                'site_name': settings.ADMINS[0][0]
            })

            subject = ''
            if expected_in < 0:
                subject = "Period was expected %s %s ago" % (expected_abs, day)
                template_name = 'expected_ago'
            elif expected_in == 0:
                subject = "Period today!"
                template_name = 'expected_now'
            elif expected_in < 4:
                subject = "Period expected in %s %s" % (expected_in, day)<commit_msg>Make emailer use same prediction logic as ui.<commit_after>from django.conf import settings
from django.core.management.base import BaseCommand
from django.template.loader import get_template
from django.template import Context

from periods import models as period_models, email_sender


class Command(BaseCommand):
    help = 'Notify users of upcoming periods'

    def _format_date(self, date_value):
        return date_value.strftime('%A %B %d, %Y')

    def handle(self, *args, **options):
        users = period_models.User.objects.filter(
            is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude(
            send_emails=False).distinct()
        for user in users:
            today = period_models.today()
            upcoming_events = user.statistics.predicted_events
            # The upcoming events are in date order, ovulation/period/ovulation/...
            expected_date = upcoming_events[1]['timestamp']
            expected_in = (expected_date - today.date()).days
            expected_abs = abs(expected_in)
            if expected_abs == 1:
                day = 'day'
            else:
                day = 'days'

            context = Context({
                'full_name': user.get_full_name(),
                'today': self._format_date(today),
                'expected_in': expected_abs,
                'day': day,
                'expected_date': self._format_date(expected_date),
                'site_name': settings.ADMINS[0][0]
            })

            subject = ''
            if expected_in < 0:
                subject = "Period was expected %s %s ago" % (expected_abs, day)
                template_name = 'expected_ago'
            elif expected_in == 0:
                subject = "Period today!"
                template_name = 'expected_now'
            elif expected_in < 4:
                subject = "Period expected in %s %s" % (expected_in, day)
{'fraction_non_alphanumeric': 0.09130998702983138, 'fraction_numerical': 0.0033722438391699094, 'mean_word_length': 1.9937888198757765, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1786402', 'n_tokens_mistral': 1052, 'n_tokens_neox': 994, 'n_words': 264}