def get_authentication_url(self, next=None): signer = TimestampSigner() return build_absolute_url( reverse('users-authenticate'), parse.urlencode({ 'sig': signer.sign(self.email), 'next': next or '', }))
def notify_users_for_comment(comment_id): """ Send an email notification about the comment to the relevant users. """ comment = Comment.objects.get(pk=comment_id) report = comment.report recipients = set() # Notify staff & managers who commented on the report q = Comment.objects.filter(report=report, created_by__is_active=True) q = q.prefetch_related('created_by') recipients.update(c.created_by for c in q) # Notify the user who claimed the report if report.claimed_by: recipients.add(report.claimed_by) # Notify invited experts q = Invite.objects.filter(report=report).prefetch_related('user') q = q.prefetch_related('user') recipients.update(invite.user for invite in q) # Notify the user that submitted the report if comment.visibility in (Comment.PROTECTED, Comment.PUBLIC): recipients.add(report.created_by) # Don't notify the user who made the comment recipients.discard(comment.created_by) emails = [] subject = get_setting('NOTIFICATIONS.notify_new_comment__subject') from_email = get_setting('NOTIFICATIONS.from_email') for user in recipients: next_url = comment.get_absolute_url() if user.is_active: url = build_absolute_url(next_url) else: url = user.get_authentication_url(next=next_url) body = render_to_string('reports/_new_comment.txt', { 'user': comment.created_by, 'body': comment.body, 'url': url, }) emails.append((subject, body, from_email, (user.email, ))) send_mass_mail(emails)
def notify_report_submission(report_id, user_id): report = Report.objects.get(pk=report_id) user = User.objects.get(pk=user_id) subject = get_setting('NOTIFICATIONS.new_report__subject') from_email = get_setting('NOTIFICATIONS.from_email') path = reverse('reports-detail', args=[report.pk]) if user.is_active: url = build_absolute_url(path) else: url = user.get_authentication_url(next=path) body = render_to_string('reports/_submission.txt', { 'user': user, 'url': url, }) send_mail(subject, body, from_email, [user.email])
def notify_report_subscribers(report_id): """ Notify users subscribed to a query that matches ``report``. """ from oregoninvasiveshotline.reports.forms import ReportSearchForm # avoid circular import report = Report.objects.get(pk=report_id) subject = get_setting('NOTIFICATIONS.notify_new_submission__subject') from_email = get_setting('NOTIFICATIONS.from_email') excluded_users = Notification.objects.filter(report=report).values_list( 'user_id', flat=True) queryset = UserNotificationQuery.objects.all().select_related('user') queryset = queryset.exclude(user__pk__in=excluded_users) notified_users = set() for user_notification_query in queryset.iterator(): user = user_notification_query.user if user.pk not in notified_users: query = user_notification_query.query form = ReportSearchForm(QueryDict(query), user=user) if form.is_valid() and \ form.search(Report.objects.all()).filter(pk=report.pk).count(): next_url = reverse('reports-detail', args=[report.pk]) if user.is_active: url = build_absolute_url(next_url) else: url = user.get_authentication_url(next=next_url) body = render_to_string( 'notifications/email.txt', { 'user': user, 'name': user_notification_query.name, 'url': url, 'report': report, }) send_mail(subject, body, from_email, [user.email]) Notification.objects.create(user=user, report=report) notified_users.add(user.pk)
def notify_new_subscription_owner(subscription_id, assigner_id): """ Notify the new owner of a subscription when ownership has changed. """ subscription = UserNotificationQuery.objects.get(pk=subscription_id) assigner = User.objects.get(pk=assigner_id) subject = get_setting('NOTIFICATIONS.notify_new_owner__subject') from_email = get_setting('NOTIFICATIONS.from_email') next_url = reverse('reports-list') + '?' + subscription.query if subscription.user.is_active: url = build_absolute_url(next_url) else: url = subscription.user.get_authentication_url(next=next_url) body = render_to_string('notifications/notify_new_owner.txt', { 'assigner': assigner, 'name': subscription.name, 'url': url, }) send_mail(subject, body, from_email, [subscription.user.email])