def test_creates_three_emails_for_three_languages(self): n = 5 for _ in list(range(n)): self.group.add_member(VerifiedUserFactory(language='en')) for _ in list(range(n)): self.group.add_member(VerifiedUserFactory(language='de')) for _ in list(range(n)): self.group.add_member(VerifiedUserFactory(language='fr')) from_date, to_date = group_emails.calculate_group_summary_dates( self.group) context = group_emails.prepare_group_summary_data( self.group, from_date, to_date) emails = group_emails.prepare_group_summary_emails(self.group, context) self.assertEqual(len(emails), 3) to = [] for email in emails: to.extend(email.to) expected_members = self.group.members.filter( groupmembership__in=GroupMembership.objects.active( ).with_notification_type(GroupNotificationType.WEEKLY_SUMMARY) ).exclude(groupmembership__user__in=get_user_model().objects. unverified_or_ignored()) self.assertEqual(sorted(to), sorted([member.email for member in expected_members])) self.assertNotIn(self.user_without_notifications.email, to)
def group_summary(self): group = random_group() from_date = timezone.now() - relativedelta(days=7) to_date = from_date + relativedelta(days=7) context = prepare_group_summary_data(group, from_date, to_date) summary_emails = prepare_group_summary_emails(group, context) if len(summary_emails) is 0: raise Exception( 'No emails were generated, you need at least one verified user in your db, and some activity data...') return summary_emails[0]
def send_summary_emails(): email_count = 0 recipient_count = 0 groups = Group.objects.annotate(member_count=Count('members')).filter( member_count__gt=0) for group in groups: from_date, to_date = emails.calculate_group_summary_dates(group) if not group.sent_summary_up_to or group.sent_summary_up_to < to_date: email_recipient_count = 0 context = prepare_group_summary_data(group, from_date, to_date) if context['has_activity']: for email in emails.prepare_group_summary_emails( group, context): try: email.send() email_count += 1 email_recipient_count += len(email.to) except AnymailAPIError: sentry_client.captureException() # we save this even if some of the email sending fails, no retries right now basically... # we also save if no emails were sent due to missing activity, to not try again over and over. group.sent_summary_up_to = to_date group.save() stats.group_summary_email( group, email_recipient_count=email_recipient_count, feedback_count=context['feedbacks'].count(), message_count=context['messages'].count(), new_user_count=context['new_users'].count(), pickups_done_count=context['pickups_done_count'], pickups_missed_count=context['pickups_missed_count'], has_activity=context['has_activity'], ) recipient_count += email_recipient_count stats_utils.periodic_task('group__send_summary_emails', { 'recipient_count': recipient_count, 'email_count': email_count, })
def send_summary_emails(): email_count = 0 recipient_count = 0 groups = Group.objects.annotate(member_count=Count('members')).filter( member_count__gt=0) for group in groups: from_date, to_date = emails.calculate_group_summary_dates(group) if not group.sent_summary_up_to or group.sent_summary_up_to < to_date: email_recipient_count = 0 context = prepare_group_summary_data(group, from_date, to_date) for email in emails.prepare_group_summary_emails(group, context): try: email.send() email_count += 1 email_recipient_count += len(email.to) except Exception: traceback.print_exc() # we save this even if some of the email sending, no retries right now basically... group.sent_summary_up_to = to_date group.save() stats.group_summary_email( group, email_recipient_count=email_recipient_count, feedback_count=context['feedbacks'].count(), message_count=context['messages'].count(), new_user_count=context['new_users'].count(), pickups_done_count=context['pickups_done_count'], pickups_missed_count=context['pickups_missed_count'], ) recipient_count += email_recipient_count stats_utils.periodic_task('group__send_summary_emails', { 'recipient_count': recipient_count, 'email_count': email_count, })