def handle(self, *args, **options): interactive = options.get('interactive') users = period_models.User.objects.filter( is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude( send_emails=False).distinct() active_users = [] for user in users: # Don't email users who haven't tracked a period in over 3 months if user.statistics.current_cycle_length < 90: active_users.append(user) if interactive: confirm = input("""You are about to email %s users about their accounts. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % len(active_users)) else: confirm = 'yes' if confirm == 'yes': subject = 'Important information about the data in your eggtimer account' template_name = 'notification' context = Context({}) plaintext = get_template('periods/email/%s.txt' % template_name) for user in active_users: email_sender.send(user, subject, plaintext.render(context), None) else: print("Would have emailed the following %s users:\n-------------------------" % len(active_users)) for user in active_users: print("%35s %5s periods" % (user.email, user.flow_events.count()))
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 if not upcoming_events: continue # The upcoming events are in date order, ovulation/period/ovulation/... expected_date = upcoming_events[1]['timestamp'] calendar_start_date = expected_date - datetime.timedelta(days=7) 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), 'calendar_start_date': self._format_date(calendar_start_date), 'admin_name': settings.ADMINS[0][0], 'full_domain': helpers.get_full_domain(), }) 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 starting" template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('periods/email/%s.txt' % template_name) html = get_template('periods/email/%s.html' % template_name) email_sender.send(user, subject, plaintext.render(context), html.render(context))
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 if not upcoming_events: continue # The upcoming events are in date order, ovulation/period/ovulation/... expected_date = upcoming_events[1]['timestamp'] calendar_start_date = expected_date - datetime.timedelta(days=7) expected_in = (expected_date - today.date()).days expected_abs = abs(expected_in) if expected_abs == 1: day = 'day' else: day = 'days' 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), 'calendar_start_date': self._format_date(calendar_start_date), 'admin_name': settings.ADMINS[0][0], 'full_domain': helpers.get_full_domain(), } 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 starting" template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('periods/email/%s.txt' % template_name) html = get_template('periods/email/%s.html' % template_name) email_sender.send(user, subject, plaintext.render(context), html.render(context))
def handle(self, *args, **options): users = period_models.User.objects.filter( 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) 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) template_name = 'expected_in' elif expected_in == user.luteal_phase_length: subject = "Ovulation today!" template_name = 'ovulating' if subject: plaintext = get_template('email/%s.txt' % template_name) email_sender.send(user, subject, plaintext.render(context), None)
def handle(self, *args, **options): interactive = options.get('interactive') users = period_models.User.objects.filter( is_active=True, flow_events__isnull=False, statistics__isnull=False).exclude(send_emails=False).distinct() active_users = [] for user in users: # Don't email users who haven't tracked a period in over 3 months if user.statistics.current_cycle_length < 90: active_users.append(user) if interactive: confirm = input( """You are about to email %s users about their accounts. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % len(active_users)) else: confirm = 'yes' if confirm == 'yes': subject = 'Important information about the data in your eggtimer account' template_name = 'notification' context = {} plaintext = get_template('periods/email/%s.txt' % template_name) for user in active_users: email_sender.send(user, subject, plaintext.render(context), None) else: print( "Would have emailed the following %s users:\n-------------------------" % len(active_users)) for user in active_users: print("%35s %5s periods" % (user.email, user.flow_events.count()))
def test_send_with_html(self, mock_send): result = email_sender.send(self.user, 'Hi!', 'good day', '<p>good day</p>') self.assertEqual(True, result) mock_send.assert_called_once_with()
def test_send_text_only(self, mock_send): result = email_sender.send(self.user, 'Hi!', 'good day', None) self.assertEqual(True, result) mock_send.assert_called_once_with()