Example #1
0
def render_and_send_invite_email(invite_id):
    """Renders and sends an invite email."""
    from open_connect.accounts.models import Invite
    invite = Invite.objects.get(pk=invite_id)

    if invite.notified:
        return

    context = {
        'invite': invite,
        'email': invite.email,
        'origin': settings.ORIGIN
    }
    html = render_to_string(
        'accounts/email/new_user_invite.html', context)
    text = render_to_string(
        'accounts/email/new_user_invite.txt', context)

    send_email(
        email=invite.email,
        from_email=settings.DEFAULT_FROM_EMAIL,
        subject=u"You're invited to Connect",
        text=text,
        html=html
    )

    invite.notified = now()
    invite.save()
Example #2
0
def send_moderation_notification(group_owner_id, top_current_hour_iso):
    """Send an individual moderation notification"""
    from open_connect.accounts.models import User
    owner = User.objects.get(pk=group_owner_id)
    top_current_hour = parse_datetime(top_current_hour_iso)

    # Find the last time we would've used as a cut off as a message
    # moderation notification time
    notification_start = top_current_hour - timedelta(
        hours=owner.moderator_notification_period)

    # Grab all the messages that were modified before the current top hour and
    # after the last cut-off point for messages
    all_messages = owner.messages_to_moderate

    total_new_messages = all_messages.filter(
        modified_at__lt=top_current_hour,
        modified_at__gte=notification_start
    ).count()

    # We don't want to re-notify users of the same pending messages, so we need
    # to bail out here if there are no new pending messages
    if total_new_messages == 0:
        return

    # We'll include the total number of pending messages in the template
    total_messages = all_messages.count()

    context = {
        'total_messages': total_messages,
        'total_new_messages': total_new_messages,
        'recipient': owner,
        'email': owner.email
    }

    subject = u"You have {count} new {word} to moderate on Connect".format(
        count=total_new_messages,
        word=ngettext('message', 'messages', total_new_messages))
    text = render_to_string(
        'notifications/email/moderator_notification.txt', context)
    html = render_to_string(
        'notifications/email/moderator_notification.html', context)

    send_email(
        email=owner.email,
        from_email=settings.DEFAULT_FROM_EMAIL,
        subject=subject,
        text=text,
        html=html
    )
Example #3
0
def send_daily_digest_notification(user_id):
    """Send a daily digest notification for an individual user"""
    notifications = Notification.objects.filter(
        recipient_id=user_id,
        consumed=False,
        # Only send notifications for approved messages, otherwise leave the
        # messages pending
        message__status='approved',
        subscription__period='daily'
    ).select_related(
        'recipient',
        'message',
        'message__thread',
        'message__thread__group',
        'message__thread__group__group'
    ).order_by('-message__thread', '-message__pk')

    # To reduce the number of `count` and `exists` queries, do the count once
    # and base as much logic around that count as possible.
    total_notifications = notifications.count()

    if not total_notifications:
        return
    recipient = notifications[0].recipient

    context = {
        'notifications': notifications,
        'recipient': recipient,
        'email': recipient.email
    }
    text = render_to_string('notifications/email/email_digest.txt', context)
    html = render_to_string('notifications/email/email_digest.html', context)

    subject = u'Your {brand} {day} Digest - {num} New {word}'.format(
        brand=settings.BRAND_TITLE,
        day=now().strftime('%A'),
        num=total_notifications,
        word=ngettext('Message', 'Messages', total_notifications)
    )

    send_email(
        email=recipient.email,
        from_email=settings.DEFAULT_FROM_EMAIL,
        subject=subject,
        text=text,
        html=html
    )

    notifications.update(consumed=True)
Example #4
0
def send_immediate_notification(notification_id):
    """Send an email for a given notification."""
    notification = Notification.objects.select_related(
        'recipient', 'message', 'message__thread').get(pk=notification_id)
    recipient = notification.recipient
    message = notification.message
    context = {
        'notification': notification,
        'message': message,
        'recipient': recipient,
        'email': recipient.email
    }
    text = render_to_string(
        'notifications/email/email_immediate.txt', context)
    html = render_to_string(
        'notifications/email/email_immediate.html', context)

    # Determine the correct format of the subject line of the notification
    if message.thread.thread_type == 'direct':
        subject_format = u"{subject}"
    else:
        subject_format = u"[{group}] {subject}"
    subject = subject_format.format(
        group=message.thread.group, subject=message.thread.subject)

    if message.sender.is_staff:
        from_name = u'{name}, Staff Member, {brand}'.format(
            name=message.sender.get_full_name(),
            brand=settings.BRAND_TITLE)
    else:
        from_name = u'{name}, {brand}'.format(
            name=message.sender.get_full_name(),
            brand=settings.BRAND_TITLE)
    from_email = formataddr((from_name, settings.DEFAULT_FROM_ADDRESS))

    to_email_tup = (recipient.get_full_name(), notification.recipient.email)
    to_address = formataddr(to_email_tup)

    send_email(
        email=to_address,
        from_email=from_email,
        subject=subject,
        text=text,
        html=html
    )

    notification.consumed_at = now()
    notification.save()
Example #5
0
    def test_send_email(self, mock):
        """Test the functionality of the send_email helper"""
        email_mock = Mock()
        mock.return_value = email_mock

        utils.send_email(email='*****@*****.**',
                         from_email='*****@*****.**',
                         subject='Updates',
                         text='You have a new message. someurl',
                         html='this is my snippet someurl')

        mock.assert_called_once_with(body='You have a new message. someurl',
                                     to=(u'*****@*****.**', ),
                                     subject='Updates',
                                     from_email='*****@*****.**')
        email_mock.attach_alternative.assert_called_once_with(
            mimetype='text/html', content='this is my snippet someurl')
Example #6
0
def send_daily_digest_notification(user_id):
    """Send a daily digest notification for an individual user"""
    notifications = Notification.objects.filter(
        recipient_id=user_id,
        consumed=False,
        # Only send notifications for approved messages, otherwise leave the
        # messages pending
        message__status='approved',
        subscription__period='daily').select_related(
            'recipient', 'message', 'message__thread',
            'message__thread__group',
            'message__thread__group__group').order_by('-message__thread',
                                                      '-message__pk')

    # To reduce the number of `count` and `exists` queries, do the count once
    # and base as much logic around that count as possible.
    total_notifications = notifications.count()

    if not total_notifications:
        return
    recipient = notifications[0].recipient

    context = {
        'notifications': notifications,
        'recipient': recipient,
        'email': recipient.email
    }
    text = render_to_string('notifications/email/email_digest.txt', context)
    html = render_to_string('notifications/email/email_digest.html', context)

    subject = u'Your {brand} {day} Digest - {num} New {word}'.format(
        brand=settings.BRAND_TITLE,
        day=now().strftime('%A'),
        num=total_notifications,
        word=ngettext('Message', 'Messages', total_notifications))

    send_email(email=recipient.email,
               from_email=settings.DEFAULT_FROM_EMAIL,
               subject=subject,
               text=text,
               html=html)

    notifications.update(consumed=True)
Example #7
0
    def test_send_email(self, mock):
        """Test the functionality of the send_email helper"""
        email_mock = Mock()
        mock.return_value = email_mock

        utils.send_email(
            email='*****@*****.**',
            from_email='*****@*****.**',
            subject='Updates',
            text='You have a new message. someurl',
            html='this is my snippet someurl'
        )

        mock.assert_called_once_with(
            body='You have a new message. someurl',
            to=(u'*****@*****.**',),
            subject='Updates',
            from_email='*****@*****.**'
        )
        email_mock.attach_alternative.assert_called_once_with(
            mimetype='text/html', content='this is my snippet someurl'
        )
Example #8
0
def send_digest_notification(user_id):
    """Send a digest notification for an individual user"""
    notifications = Notification.objects.select_related('recipient').filter(
        recipient_id=user_id,
        consumed_at__isnull=True,
        # Only send notifications for approved messages, otherwise leave the
        # messages pending
        message__status='approved'
    ).exclude(subscription__period__in=['immediate', 'none']).order_by(
        '-message__thread', '-message__pk')
    if not notifications:
        return
    recipient = notifications[0].recipient

    context = {
        'notifications': notifications,
        'recipient': recipient,
        'email': recipient.email
    }
    text = render_to_string('notifications/email/email_digest.txt', context)
    html = render_to_string('notifications/email/email_digest.html', context)

    subject = u'Your {brand} {day} Digest - {num} New {word}'.format(
        brand=settings.BRAND_TITLE,
        day=now().strftime('%A'),
        num=notifications.count(),
        word=ngettext('Message', 'Messages', notifications.count())
    )

    send_email(
        email=recipient.email,
        from_email=settings.DEFAULT_FROM_EMAIL,
        subject=subject,
        text=text,
        html=html
    )

    notifications.update(consumed_at=now())
Example #9
0
def render_and_send_invite_email(invite_id):
    """Renders and sends an invite email."""
    from open_connect.accounts.models import Invite
    invite = Invite.objects.get(pk=invite_id)

    if invite.notified:
        return

    context = {
        'invite': invite,
        'email': invite.email,
        'origin': settings.ORIGIN
    }
    html = render_to_string('account/email/new_user_invite.html', context)
    text = render_to_string('account/email/new_user_invite.txt', context)

    send_email(email=invite.email,
               from_email=settings.DEFAULT_FROM_EMAIL,
               subject=u"You're invited to Connect",
               text=text,
               html=html)

    invite.notified = now()
    invite.save()