def email_commenters_on_add_poll_comment(sender, instance, **kwargs): """Email a user when a comment is added to a poll.""" poll = instance.poll if poll.comments_allowed: subject = '[Voting] User {0} commented on {1}' email_template = 'emails/user_notification_on_add_poll_comment.txt' # Send an email to all users commented so far on the poll except from # the user who made the comment. Dedup the list with unique IDs. commenters = set(PollComment.objects.filter(poll=poll) .exclude(user=instance.user) .values_list('user', flat=True)) # Add the creator of the poll in the list if poll.created_by.id not in commenters: commenters.add(poll.created_by.id) for user_id in commenters: user = User.objects.get(pk=user_id) if (user.userprofile.receive_email_on_add_voting_comment and user != instance.user): ctx_data = {'poll': poll, 'user': user, 'commenter': instance.user, 'comment': instance.comment, 'created_on': instance.created_on} subject = subject.format(instance.user.get_full_name(), poll) send_remo_mail.delay(subject=subject, recipients_list=[user_id], email_template=email_template, data=ctx_data)
def extend_voting_period(): """Extend voting period by EXTEND_VOTING_PERIOD if there is no majority decision. """ # avoid circular dependencies from remo.voting.models import Poll tomorrow = get_date(days=1) review_count = User.objects.filter(groups__name='Review').count() query_start = make_aware(datetime.combine(tomorrow, datetime.min.time()), pytz.UTC) query_end = make_aware(datetime.combine(tomorrow, datetime.max.time()), pytz.UTC) polls = Poll.objects.filter(end__range=[query_start, query_end]) for poll in polls: if not poll.is_extended: budget_poll = poll.radio_polls.get(question='Budget Approval') majority = reduce(or_, map(lambda x: x.votes > review_count / 2, budget_poll.answers.all())) if not majority: poll.end += timedelta(seconds=EXTEND_VOTING_PERIOD) poll.save() subject = '[Urgent] Voting extended for {0}'.format(poll.name) recipients = (User.objects.filter(groups=poll.valid_groups) .exclude(pk__in=poll.users_voted.all()) .values_list('id', flat=True)) ctx_data = {'poll': poll} template = 'emails/voting_vote_reminder.jinja' send_remo_mail.delay(subject=subject, recipients_list=recipients, email_template=template, data=ctx_data)
def email_commenters_on_add_ng_report_comment(sender, instance, **kwargs): """Email a user when a comment is added to a continuous report instance.""" subject = '[Report] User {0} commented on {1}' email_template = 'emails/user_notification_on_add_ng_report_comment.jinja' report = instance.report # Send an email to all users commented so far on the report except fom # the user who made the comment. Dedup the list with unique IDs. commenters = set(NGReportComment.objects.filter(report=report) .exclude(user=instance.user) .values_list('user', flat=True)) # Add the owner of the report in the list if report.user.id not in commenters: commenters.add(report.user.id) for user_id in commenters: user = User.objects.get(pk=user_id) if user.userprofile.receive_email_on_add_comment and user != instance.user: ctx_data = {'report': report, 'user': user, 'commenter': instance.user, 'comment': instance.comment, 'created_on': instance.created_on} subject = subject.format(instance.user.get_full_name(), report) send_remo_mail.delay(subject=subject, recipients_list=[user_id], email_template=email_template, data=ctx_data)
def email_mentor_notification(sender, instance, raw, **kwargs): """Notify mentor when his/her mentee changes mentor on his/her profile.""" if not instance.mentor: return user_profile = get_object_or_none(UserProfile, user=instance.user) if not user_profile or not user_profile.mentor or raw: return if user_profile.mentor != instance.mentor: subject = '[Reps] Mentor reassignment.' email_template = 'emails/mentor_change_notification.txt' mentors_recipients = [user_profile.mentor.id, instance.mentor.id] rep_recipient = [instance.user.id] ctx_data = {'rep_user': instance.user, 'new_mentor': instance.mentor} send_remo_mail.delay(recipients_list=mentors_recipients, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.user.email}) send_remo_mail.delay(recipients_list=rep_recipient, subject=subject, email_template=email_template, data=ctx_data, headers={'Reply-To': instance.mentor.email}) statsd.incr('profiles.change_mentor')
def notify_event_owners_to_input_metrics(): """Send an email to event creators. After an event has finished event creators are notified that they should input the actual metrics for the event. """ start = datetime.combine(get_date(days=-1), datetime.min.time()) end = datetime.combine(get_date(days=-1), datetime.max.time()) events = Event.objects.filter(end__range=[start, end], has_new_metrics=True, eventmetricoutcome__outcome__isnull=True) events = events.distinct() event_model = ContentType.objects.get_for_model(Event) for event in events: # Before sending an email check that an action item already exists. # If it does, then we have already sent this email. action_item = ActionItem.objects.filter(content_type=event_model, object_id=event.id) if not action_item.exists(): subject = '[Reminder] Please add the actual metrics for event {0}'.format(event.name) template = 'email/event_creator_notification_to_input_metrics.jinja' data = {'event': event} send_remo_mail.delay(subject=subject, email_template=template, recipients_list=[event.owner.id], data=data) ActionItem.create(instance=event)
def extend_voting_period(): """Extend voting period by EXTEND_VOTING_PERIOD if there is no majority decision. """ # avoid circular dependencies from remo.voting.models import Poll tomorrow = get_date(days=1) council_count = User.objects.filter(groups__name='Council').count() polls = Poll.objects.filter(end__year=tomorrow.year, end__month=tomorrow.month, end__day=tomorrow.day, automated_poll=True) for poll in polls: if not poll.is_extended: budget_poll = poll.radio_polls.get(question='Budget Approval') majority = reduce(or_, map(lambda x: x.votes > council_count/2, budget_poll.answers.all())) if not majority: poll.end += timedelta(seconds=EXTEND_VOTING_PERIOD) poll.save() subject = '[Urgent] Voting extended for {0}'.format(poll.name) recipients = (User.objects.filter(groups=poll.valid_groups) .exclude(pk__in=poll.users_voted.all()) .values_list('id', flat=True)) ctx_data = {'poll': poll} template = 'emails/voting_vote_reminder.txt' send_remo_mail.delay(subject=subject, recipients_list=recipients, email_template=template, data=ctx_data)
def poll_vote_reminder(): """Send an email reminder every 8 hours to remind valid users to cast their vote. """ polls = Poll.objects.filter(start__lte=now(), end__gt=now()) for poll in polls: last_notification = (poll.last_nofication if poll.last_notification else poll.created_on) time_diff = (time.mktime(now().timetuple()) - time.mktime(last_notification.timetuple())) if time_diff > NOTIFICATION_INTERVAL: valid_users = User.objects.filter(groups=poll.valid_groups) recipients = (valid_users.exclude(pk__in=poll.users_voted.all()) .values_list('id', flat=True)) subject = ('[Reminder][Voting] Please cast your vote ' 'for "%s" now!' % poll.name) template_reminder = 'emails/voting_vote_reminder.txt' ctx_data = {'poll': poll} send_remo_mail.delay(subject=subject, recipients_list=recipients, email_template=template_reminder, data=ctx_data) Poll.objects.filter(pk=poll.pk).update(last_notification=now())
def send_email(self, request, subject=''): """Send an email to user's mentor""" mentor = request.user.userprofile.mentor from_email = '%s <%s>' % (request.user.get_full_name(), request.user.email) send_remo_mail.delay(sender=from_email, recipients_list=[mentor.id], subject=subject, message=self.cleaned_data['body'])
def send_email(self, request, subject='', message=None, template=None, data=None): """Send an email to user's mentor""" mentor = request.user.userprofile.mentor from_email = '%s <%s>' % (request.user.get_full_name(), request.user.email) send_remo_mail.delay(sender=from_email, recipients_list=[mentor.id], subject=subject, message=message, email_template=template, data=data)
def automated_poll_discussion_email(sender, instance, created, raw, **kwargs): """Send email reminders when a vote starts/ends.""" if instance.automated_poll and created: template = 'emails/review_budget_notify_council.txt' subject = ('[Bug %d] Budget request discussion' % instance.bug.bug_id) data = {'bug': instance.bug, 'BUGZILLA_URL': BUGZILLA_URL} send_remo_mail.delay( subject=subject, email_template=template, recipients_list=[settings.REPS_COUNCIL_ALIAS], data=data, headers={'Reply-To': settings.REPS_COUNCIL_ALIAS})
def automated_poll_discussion_email(sender, instance, created, raw, **kwargs): """Send email reminders when a vote starts/ends.""" if instance.automated_poll and created: template = 'emails/review_budget_notify_review_team.jinja' subject = u'Discuss [Bug {id}] - {summary}'.format(id=instance.bug.bug_id, summary=unicode(instance.bug.summary)) data = {'bug': instance.bug, 'BUGZILLA_URL': get_bugzilla_url(instance.bug), 'poll': instance} send_remo_mail.delay( subject=subject, email_template=template, recipients_list=[settings.REPS_REVIEW_ALIAS], data=data)
def email_event_owner_on_add_comment(sender, instance, **kwargs): """Email event owner when a comment is added to event.""" subject = '[Event] User %s commented on event "%s"' email_template = 'email/owner_notification_on_add_comment.jinja' event = instance.event owner = instance.event.owner event_url = reverse('events_view_event', kwargs={'slug': event.slug}) ctx_data = {'event': event, 'owner': owner, 'commenter': instance.user, 'comment': instance.comment, 'event_url': event_url} if owner.userprofile.receive_email_on_add_event_comment: subject = subject % (instance.user.get_full_name(), instance.event.name) send_remo_mail.delay(subject=subject, recipients_list=[owner.id], email_template=email_template, data=ctx_data)
def email_mentor_notification(sender, instance, raw, **kwargs): """Notify mentor when his/her mentee changes mentor on his/her profile.""" if UserProfile.objects.filter(user=instance.user).exists() and not raw: user_profile = UserProfile.objects.get(user=instance.user) if user_profile.mentor and user_profile.mentor != instance.mentor: subject = '[Reps] Change of mentor.' email_template = 'emails/mentor_change_notification.txt' recipients = [user_profile.mentor.id, user_profile.user.id, instance.mentor.id] ctx_data = {'rep_user': instance.user, 'new_mentor': instance.mentor} send_remo_mail.delay(recipients_list=recipients, subject=subject, email_template=email_template, data=ctx_data) statsd.incr('profiles.change_mentor')
def send_email(self, request, users): """Send mail to recipients list.""" recipients = users.values_list('id', flat=True) if recipients: from_email = '%s <%s>' % (request.user.get_full_name(), request.user.email) send_remo_mail.delay(sender=from_email, recipients_list=recipients, subject=self.cleaned_data['subject'], message=self.cleaned_data['body']) messages.success(request, 'Email sent successfully.') else: messages.error(request, 'Email not sent. An error occured.')
def email_event_owner_on_add_comment(sender, instance, **kwargs): """Email event owner when a comment is added to event.""" subject = '[Event] User %s commented on event "%s"' email_template = 'email/owner_notification_on_add_comment.txt' event = instance.event owner = instance.event.owner event_url = reverse('events_view_event', kwargs={'slug': event.slug}) ctx_data = {'event': event, 'owner': owner, 'user': instance.user, 'comment': instance.comment, 'event_url': event_url} if owner.userprofile.receive_email_on_add_event_comment: subject = subject % (instance.user.get_full_name(), instance.event.name) send_remo_mail.delay(subject=subject, recipients_list=[owner.id], email_template=email_template, data=ctx_data)
def send_report_notification(reps, weeks): """Send notification to inactive reps.""" rep_subject = '[Reminder] Please share your recent activities' rep_mail_body = 'emails/reps_ng_report_notification.txt' mentor_subject = ('[Report] Mentee without report for the last %d weeks' % weeks) mentor_mail_body = 'emails/mentor_ng_report_notification.txt' for rep in reps: ctx_data = {'mentor': rep.userprofile.mentor, 'user': rep, 'SITE_URL': settings.SITE_URL, 'weeks': weeks} rep_message = render_to_string(rep_mail_body, ctx_data) mentor_message = render_to_string(mentor_mail_body, ctx_data) send_remo_mail.delay(rep_subject, [rep.email], message=rep_message) send_remo_mail.delay(mentor_subject, [rep.userprofile.mentor.email], message=mentor_message)
def send_mail(self, request): """Send mail to recipients list.""" recipients_list = [] for field in self.fields: if (isinstance(self.fields[field], forms.BooleanField) and self.cleaned_data[field]): recipients_list.append(long(field)) if recipients_list: from_email = '%s <%s>' % (request.user.get_full_name(), request.user.email) send_remo_mail.delay(sender=from_email, recipients_list=recipients_list, subject=self.cleaned_data['subject'], message=self.cleaned_data['body']) messages.success(request, 'Email sent successfully.') else: messages.error(request, ('Email not sent. Please select at ' 'least one recipient.'))
def extend_voting_period(): """Extend voting period by EXTEND_VOTING_PERIOD if there is no majority decision. """ # avoid circular dependencies from remo.voting.models import Poll tomorrow = get_date(days=1) review_count = User.objects.filter(groups__name='Review').count() query_start = make_aware(datetime.combine(tomorrow, datetime.min.time()), pytz.UTC) query_end = make_aware(datetime.combine(tomorrow, datetime.max.time()), pytz.UTC) polls = Poll.objects.filter(end__range=[query_start, query_end]) for poll in polls: if not poll.is_extended: budget_poll = poll.radio_polls.get(question='Budget Approval') majority = reduce( or_, map(lambda x: x.votes > review_count / 2, budget_poll.answers.all())) if not majority: poll.end += timedelta(seconds=EXTEND_VOTING_PERIOD) poll.save() subject = '[Urgent] Voting extended for {0}'.format(poll.name) recipients = (User.objects.filter( groups=poll.valid_groups).exclude( pk__in=poll.users_voted.all()).values_list('id', flat=True)) ctx_data = {'poll': poll} template = 'emails/voting_vote_reminder.jinja' send_remo_mail.delay(subject=subject, recipients_list=recipients, email_template=template, data=ctx_data)
def extend_voting_period(): """Extend voting period by EXTEND_VOTING_PERIOD if there is no majority decision. """ # avoid circular dependencies from remo.voting.models import Poll tomorrow = get_date(days=1) council_count = User.objects.filter(groups__name='Council').count() polls = Poll.objects.filter(end__year=tomorrow.year, end__month=tomorrow.month, end__day=tomorrow.day, automated_poll=True) for poll in polls: if not poll.is_extended: budget_poll = poll.radio_polls.get(question='Budget Approval') majority = reduce( or_, map(lambda x: x.votes > council_count / 2, budget_poll.answers.all())) if not majority: poll.end += timedelta(seconds=EXTEND_VOTING_PERIOD) poll.save() subject = '[Urgent] Voting extended for {0}'.format(poll.name) recipients = (User.objects.filter( groups=poll.valid_groups).exclude( pk__in=poll.users_voted.all()).values_list('id', flat=True)) ctx_data = {'poll': poll} template = 'emails/voting_vote_reminder.txt' send_remo_mail.delay(subject=subject, recipients_list=recipients, email_template=template, data=ctx_data)