Example #1
0
    def _send_mail(locale):
        # Avoid circular import issues
        from kitsune.users.helpers import display_name

        subject = _(u'[SUMO] You have a new private message from [{sender}]')
        subject = subject.format(
            sender=display_name(inbox_message.sender))

        msg_url = reverse('messages.read', kwargs={'msgid': inbox_message.id})
        settings_url = reverse('users.edit_settings')

        from kitsune.sumo.helpers import add_utm
        context = {
            'sender': inbox_message.sender,
            'message': inbox_message.message,
            'message_html': inbox_message.content_parsed,
            'message_url': add_utm(msg_url, 'messages-new'),
            'unsubscribe_url': add_utm(settings_url, 'messages-new'),
            'host': Site.objects.get_current().domain}

        mail = make_mail(subject=subject,
                         text_template='messages/email/private_message.ltxt',
                         html_template='messages/email/private_message.html',
                         context_vars=context,
                         from_email=settings.TIDINGS_FROM_ADDRESS,
                         to_email=inbox_message.to.email)

        send_messages([mail])
Example #2
0
def make_contributor(request):
    """Adds the logged in user to the contributor group"""
    group = Group.objects.get(name=CONTRIBUTOR_GROUP)
    request.user.groups.add(group)

    @email_utils.safe_translation
    def _make_mail(locale):
        mail = email_utils.make_mail(
            # L10n: Thank you so much for your translation work! You're
            # L10n: the best!
            subject=_('Welcome to SUMO!'),
            text_template='users/email/contributor.ltxt',
            html_template='users/email/contributor.html',
            context_vars={'contributor': request.user},
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=request.user.email)

        return mail

    email_utils.send_messages([_make_mail(request.LANGUAGE_CODE)])

    if 'return_to' in request.POST:
        return HttpResponseRedirect(request.POST['return_to'])
    else:
        return HttpResponseRedirect(reverse('landings.get_involved'))
Example #3
0
    def _send_mail(locale):
        # Avoid circular import issues
        from kitsune.users.templatetags.jinja_helpers import display_name

        subject = _("[SUMO] You have a new private message from [{sender}]")
        subject = subject.format(sender=display_name(inbox_message.sender))

        msg_url = reverse("messages.read", kwargs={"msgid": inbox_message.id})
        settings_url = reverse("users.edit_settings")

        from kitsune.sumo.templatetags.jinja_helpers import add_utm

        context = {
            "sender": inbox_message.sender,
            "message": inbox_message.message,
            "message_html": inbox_message.content_parsed,
            "message_url": add_utm(msg_url, "messages-new"),
            "unsubscribe_url": add_utm(settings_url, "messages-new"),
            "host": Site.objects.get_current().domain,
        }

        mail = make_mail(
            subject=subject,
            text_template="messages/email/private_message.ltxt",
            html_template="messages/email/private_message.html",
            context_vars=context,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=inbox_message.to.email,
        )

        send_messages([mail])
Example #4
0
def resend_confirmation(request, template):
    """Resend confirmation email."""
    if request.method == 'POST':
        form = EmailConfirmationForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                reg_prof = RegistrationProfile.objects.get(user__email=email)
                if not reg_prof.user.is_active:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email', reg_prof)
                else:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form,
                        'email',
                        reg_prof,
                        text_template='users/email/already_activated.ltxt',
                        html_template='users/email/already_activated.html',
                        subject=_('Account already activated'))
            except RegistrationProfile.DoesNotExist:
                # Send already active email if user exists
                try:
                    user = User.objects.get(email=email, is_active=True)

                    current_site = Site.objects.get_current()
                    email_kwargs = {
                        'domain': current_site.domain,
                        'login_url': reverse('users.login')
                    }

                    subject = _('Account already activated')

                    @email_utils.safe_translation
                    def _make_mail(locale):
                        mail = email_utils.make_mail(
                            subject=subject,
                            text_template='users/email/already_activated.ltxt',
                            html_template='users/email/already_activated.html',
                            context_vars=email_kwargs,
                            from_email=settings.DEFAULT_FROM_EMAIL,
                            to_email=user.email)

                        return mail

                    email_utils.send_messages(
                        [_make_mail(request.LANGUAGE_CODE)])
                except User.DoesNotExist:
                    # Don't leak existence of email addresses.
                    pass
            # Form may now be invalid if email failed to send.
            if form.is_valid():
                return render(request,
                              template + 'resend_confirmation_done.html',
                              {'email': email})
    else:
        form = EmailConfirmationForm()
    return render(request, template + 'resend_confirmation.html',
                  {'form': form})
Example #5
0
def make_contributor(request):
    """Adds the logged in user to the contributor group"""
    group = Group.objects.get(name=CONTRIBUTOR_GROUP)
    request.user.groups.add(group)

    @email_utils.safe_translation
    def _make_mail(locale):
        mail = email_utils.make_mail(
            # L10n: Thank you so much for your translation work! You're
            # L10n: the best!
            subject=_('Welcome to SUMO!'),
            text_template='users/email/contributor.ltxt',
            html_template='users/email/contributor.html',
            context_vars={'contributor': request.user},
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=request.user.email)

        return mail

    email_utils.send_messages([_make_mail(request.LANGUAGE_CODE)])

    if 'return_to' in request.POST:
        return HttpResponseRedirect(request.POST['return_to'])
    else:
        return HttpResponseRedirect(reverse('landings.get_involved'))
Example #6
0
    def _send_mail(locale):
        subject = _(u'[SUMO] You have a new private message from [{sender}]')
        subject = subject.format(sender=inbox_message.sender.username)

        context = {
            'sender':
            inbox_message.sender.username,
            'message':
            inbox_message.message,
            'message_html':
            inbox_message.content_parsed,
            'message_url':
            reverse('messages.read', kwargs={'msgid': inbox_message.id}),
            'unsubscribe_url':
            reverse('users.edit_settings'),
            'host':
            Site.objects.get_current().domain
        }

        mail = make_mail(subject=subject,
                         text_template='messages/email/private_message.ltxt',
                         html_template='messages/email/private_message.html',
                         context_vars=context,
                         from_email=settings.TIDINGS_FROM_ADDRESS,
                         to_email=inbox_message.to.email)

        send_messages([mail])
Example #7
0
def send_award_notification(award):
    """Sends the award notification email

    :arg award: the django-badger Award instance

    """
    @email_utils.safe_translation
    def _make_mail(locale, context, email):
        subject = _(u"You were awarded the '{title}' badge!").format(
            title=pgettext('DB: badger.Badge.title', award.badge.title))

        mail = email_utils.make_mail(
            subject=subject,
            text_template='kbadge/email/award_notification.ltxt',
            html_template='kbadge/email/award_notification.html',
            context_vars=context,
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=email)

        return mail

    msg = _make_mail(locale=award.user.profile.locale,
                     context={
                         'host': Site.objects.get_current().domain,
                         'award': award,
                         'badge': award.badge,
                     },
                     email=award.user.email)

    # FIXME: this sends emails to the person who was awarded the
    # badge. Should we also send an email to the person who awarded
    # the badge? Currently, I think we shouldn't.

    email_utils.send_messages([msg])
Example #8
0
def resend_confirmation(request, template):
    """Resend confirmation email."""
    if request.method == 'POST':
        form = EmailConfirmationForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            try:
                reg_prof = RegistrationProfile.objects.get(
                    user__email=email)
                if not reg_prof.user.is_active:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email',
                        reg_prof)
                else:
                    form = try_send_email_with_form(
                        RegistrationProfile.objects.send_confirmation_email,
                        form, 'email',
                        reg_prof,
                        text_template='users/email/already_activated.ltxt',
                        html_template='users/email/already_activated.html',
                        subject=_('Account already activated'))
            except RegistrationProfile.DoesNotExist:
                # Send already active email if user exists
                try:
                    user = User.objects.get(email=email, is_active=True)

                    current_site = Site.objects.get_current()
                    email_kwargs = {'domain': current_site.domain,
                                    'login_url': reverse('users.login')}

                    subject = _('Account already activated')

                    @email_utils.safe_translation
                    def _make_mail(locale):
                        mail = email_utils.make_mail(
                            subject=subject,
                            text_template='users/email/already_activated.ltxt',
                            html_template='users/email/already_activated.html',
                            context_vars=email_kwargs,
                            from_email=settings.DEFAULT_FROM_EMAIL,
                            to_email=user.email)

                        return mail

                    email_utils.send_messages(
                        [_make_mail(request.LANGUAGE_CODE)])
                except User.DoesNotExist:
                    # Don't leak existence of email addresses.
                    pass
            # Form may now be invalid if email failed to send.
            if form.is_valid():
                return render(
                    request, template + 'resend_confirmation_done.html',
                    {'email': email})
    else:
        form = EmailConfirmationForm()
    return render(request, template + 'resend_confirmation.html', {
        'form': form})
Example #9
0
def send_reviewed_notification(revision_id: int, document_id: int,
                               message: str):
    """Send notification of review to the revision creator."""

    try:
        revision = Revision.objects.get(id=revision_id)
        document = Document.objects.get(id=document_id)
    except (Revision.DoesNotExist, Document.DoesNotExist) as err:
        capture_exception(err)
        return

    if revision.reviewer == revision.creator:
        log.debug('Revision (id=%s) reviewed by creator, skipping email' %
                  revision.id)
        return

    log.debug('Sending reviewed email for revision (id=%s)' % revision.id)

    url = reverse('wiki.document_revisions',
                  locale=document.locale,
                  args=[document.slug])

    c = {
        'document_title': document.title,
        'approved': revision.is_approved,
        'reviewer': revision.reviewer,
        'message': message,
        'revisions_url': url,
        'host': Site.objects.get_current().domain
    }

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _('Your revision has been approved: {title}')
        else:
            subject = _('Your revision has been reviewed: {title}')
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(subject=subject,
                                     text_template='wiki/email/reviewed.ltxt',
                                     html_template='wiki/email/reviewed.html',
                                     context_vars=c,
                                     from_email=settings.TIDINGS_FROM_ADDRESS,
                                     to_email=user.email)

        msgs.append(mail)

    for user in [revision.creator, revision.reviewer]:
        if hasattr(user, 'profile'):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #10
0
    def save(
        self,
        domain_override=None,
        subject_template_name="registration/password_reset_subject.txt",
        text_template=None,
        html_template=None,
        use_https=False,
        token_generator=default_token_generator,
        from_email=None,
        request=None,
    ):
        """
        Based off of django's but handles html and plain-text emails.
        """
        users = User.objects.filter(
            email__iexact=self.cleaned_data["email"], is_active=True
        )
        for user in users:
            if not domain_override:
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
            else:
                site_name = domain = domain_override

            c = {
                "email": user.email,
                "domain": domain,
                "site_name": site_name,
                "uid": int_to_base36(user.id),
                "user": user,
                "token": token_generator.make_token(user),
                "protocol": use_https and "https" or "http",
            }

            subject = email_utils.render_email(subject_template_name, c)
            # Email subject *must not* contain newlines
            subject = "".join(subject.splitlines())

            @email_utils.safe_translation
            def _make_mail(locale):
                mail = email_utils.make_mail(
                    subject=subject,
                    text_template=text_template,
                    html_template=html_template,
                    context_vars=c,
                    from_email=from_email,
                    to_email=user.email,
                )

                return mail

            if request:
                locale = request.LANGUAGE_CODE
            else:
                locale = settings.WIKI_DEFAULT_LANGUAGE

            email_utils.send_messages([_make_mail(locale)])
Example #11
0
    def save(
        self,
        domain_override=None,
        subject_template_name="registration/password_reset_subject.txt",
        text_template=None,
        html_template=None,
        use_https=False,
        token_generator=default_token_generator,
        from_email=None,
        request=None,
    ):
        """
        Based off of django's but uses jingo and handles html and plain-text
        emails
        """
        for user in self.users_cache:
            if not domain_override:
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
            else:
                site_name = domain = domain_override

            c = {
                "email": user.email,
                "domain": domain,
                "site_name": site_name,
                "uid": int_to_base36(user.id),
                "user": user,
                "token": token_generator.make_token(user),
                "protocol": use_https and "https" or "http",
            }

            subject = email_utils.render_email(subject_template_name, c)
            # Email subject *must not* contain newlines
            subject = "".join(subject.splitlines())

            @email_utils.safe_translation
            def _make_mail(locale):
                mail = email_utils.make_mail(
                    subject=subject,
                    text_template=text_template,
                    html_template=html_template,
                    context_vars=c,
                    from_email=from_email,
                    to_email=user.email,
                )

                return mail

            if request:
                locale = request.LANGUAGE_CODE
            else:
                locale = settings.WIKI_DEFAULT_LANGUAGE

            email_utils.send_messages([_make_mail(locale)])
Example #12
0
def send_contributor_notification(based_on, revision, document, message):
    """Send notification of review to the contributors of revisions."""

    text_template = 'wiki/email/reviewed_contributors.ltxt'
    html_template = 'wiki/email/reviewed_contributors.html'
    url = reverse('wiki.document_revisions',
                  locale=document.locale,
                  args=[document.slug])
    c = {
        'document_title': document.title,
        'approved': revision.is_approved,
        'reviewer': revision.reviewer,
        'message': message,
        'revisions_url': url,
        'host': Site.objects.get_current().domain
    }

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _(u'A revision you contributed to has '
                        'been approved: {title}')
        else:
            subject = _(u'A revision you contributed to has '
                        'been reviewed: {title}')
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(subject=subject,
                                     text_template=text_template,
                                     html_template=html_template,
                                     context_vars=c,
                                     from_email=settings.TIDINGS_FROM_ADDRESS,
                                     to_email=user.email)

        msgs.append(mail)

    for r in based_on:
        # Send email to all contributors except the reviewer and the creator
        # of the approved revision.
        if r.creator in [revision.creator, revision.reviewer]:
            continue

        user = r.creator

        if hasattr(user, 'profile'):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #13
0
        def _send_mail(locale, user, context):
            subject = _('Your username on %s') % site_name

            mail = email_utils.make_mail(
                subject=subject,
                text_template=text_template,
                html_template=html_template,
                context_vars=context,
                from_email=settings.TIDINGS_FROM_ADDRESS,
                to_email=user.email)

            email_utils.send_messages([mail])
Example #14
0
    def _send_mail(locale, user, context):
        subject = _('[Reviews Pending: %s] SUMO needs your help!' % locale)

        mail = email_utils.make_mail(
            subject=subject,
            text_template='wiki/email/ready_for_review_weekly_digest.ltxt',
            html_template='wiki/email/ready_for_review_weekly_digest.html',
            context_vars=context,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email)

        email_utils.send_messages([mail])
Example #15
0
    def _send_mail(locale, user, context):
        subject = _('[Reviews Pending: %s] SUMO needs your help!' % locale)

        mail = email_utils.make_mail(
            subject=subject,
            text_template='wiki/email/ready_for_review_weekly_digest.ltxt',
            html_template='wiki/email/ready_for_review_weekly_digest.html',
            context_vars=context,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email)

        email_utils.send_messages([mail])
Example #16
0
    def save(self,
             domain_override=None,
             subject_template_name='registration/password_reset_subject.txt',
             text_template=None,
             html_template=None,
             use_https=False,
             token_generator=default_token_generator,
             from_email=None,
             request=None):
        """
        Based off of django's but uses jingo and handles html and plain-text
        emails
        """
        for user in self.users_cache:
            if not domain_override:
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
            else:
                site_name = domain = domain_override

            c = {
                'email': user.email,
                'domain': domain,
                'site_name': site_name,
                'uid': int_to_base36(user.id),
                'user': user,
                'token': token_generator.make_token(user),
                'protocol': use_https and 'https' or 'http',
            }

            subject = email_utils.render_email(subject_template_name, c)
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            @email_utils.safe_translation
            def _make_mail(locale):
                mail = email_utils.make_mail(subject=subject,
                                             text_template=text_template,
                                             html_template=html_template,
                                             context_vars=c,
                                             from_email=from_email,
                                             to_email=user.email)

                return mail

            if request:
                locale = request.LANGUAGE_CODE
            else:
                locale = settings.WIKI_DEFAULT_LANGUAGE

            email_utils.send_messages([_make_mail(locale)])
Example #17
0
    def save(self, domain_override=None,
             subject_template_name='registration/password_reset_subject.txt',
             text_template=None,
             html_template=None,
             use_https=False, token_generator=default_token_generator,
             from_email=None, request=None):
        """
        Based off of django's but uses jingo and handles html and plain-text
        emails
        """
        users = User.objects.filter(
            email__iexact=self.cleaned_data["email"], is_active=True)
        for user in users:
            if not domain_override:
                current_site = get_current_site(request)
                site_name = current_site.name
                domain = current_site.domain
            else:
                site_name = domain = domain_override

            c = {
                'email': user.email,
                'domain': domain,
                'site_name': site_name,
                'uid': int_to_base36(user.id),
                'user': user,
                'token': token_generator.make_token(user),
                'protocol': use_https and 'https' or 'http',
            }

            subject = email_utils.render_email(subject_template_name, c)
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            @email_utils.safe_translation
            def _make_mail(locale):
                mail = email_utils.make_mail(
                    subject=subject,
                    text_template=text_template,
                    html_template=html_template,
                    context_vars=c,
                    from_email=from_email,
                    to_email=user.email)

                return mail

            if request:
                locale = request.LANGUAGE_CODE
            else:
                locale = settings.WIKI_DEFAULT_LANGUAGE

            email_utils.send_messages([_make_mail(locale)])
Example #18
0
def send_contributor_notification(based_on, revision, document, message):
    """Send notification of review to the contributors of revisions."""

    text_template = 'wiki/email/reviewed_contributors.ltxt'
    html_template = 'wiki/email/reviewed_contributors.html'
    url = reverse('wiki.document_revisions', locale=document.locale,
                  args=[document.slug])
    c = {'document_title': document.title,
         'approved': revision.is_approved,
         'reviewer': revision.reviewer,
         'message': message,
         'revisions_url': url,
         'host': Site.objects.get_current().domain}

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _(u'A revision you contributed to has '
                        'been approved: {title}')
        else:
            subject = _(u'A revision you contributed to has '
                        'been reviewed: {title}')
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(
            subject=subject,
            text_template=text_template,
            html_template=html_template,
            context_vars=c,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email)

        msgs.append(mail)

    for r in based_on:
        # Send email to all contributors except the reviewer and the creator
        # of the approved revision.
        if r.creator in [revision.creator, revision.reviewer]:
            continue

        user = r.creator

        if hasattr(user, 'profile'):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #19
0
def send_reviewed_notification(revision, document, message):
    """Send notification of review to the revision creator."""
    if revision.reviewer == revision.creator:
        log.debug("Revision (id=%s) reviewed by creator, skipping email" % revision.id)
        return

    log.debug("Sending reviewed email for revision (id=%s)" % revision.id)

    url = reverse("wiki.document_revisions", locale=document.locale, args=[document.slug])

    c = {
        "document_title": document.title,
        "approved": revision.is_approved,
        "reviewer": revision.reviewer,
        "message": message,
        "revisions_url": url,
        "host": Site.objects.get_current().domain,
    }

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _(u"Your revision has been approved: {title}")
        else:
            subject = _(u"Your revision has been reviewed: {title}")
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(
            subject=subject,
            text_template="wiki/email/reviewed.ltxt",
            html_template="wiki/email/reviewed.html",
            context_vars=c,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email,
        )

        msgs.append(mail)

    for user in [revision.creator, revision.reviewer]:
        if hasattr(user, "profile"):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #20
0
def send_reviewed_notification(revision, document, message):
    """Send notification of review to the revision creator."""
    if revision.reviewer == revision.creator:
        log.debug('Revision (id=%s) reviewed by creator, skipping email' %
                  revision.id)
        return

    log.debug('Sending reviewed email for revision (id=%s)' % revision.id)

    url = reverse('wiki.document_revisions', locale=document.locale,
                  args=[document.slug])

    c = {'document_title': document.title,
         'approved': revision.is_approved,
         'reviewer': revision.reviewer,
         'message': message,
         'revisions_url': url,
         'host': Site.objects.get_current().domain}

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _(u'Your revision has been approved: {title}')
        else:
            subject = _(u'Your revision has been reviewed: {title}')
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(
            subject=subject,
            text_template='wiki/email/reviewed.ltxt',
            html_template='wiki/email/reviewed.html',
            context_vars=c,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email)

        msgs.append(mail)

    for user in [revision.creator, revision.reviewer]:
        if hasattr(user, 'profile'):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #21
0
def send_award_notification(award_id: int):
    """Sends the award notification email

    :arg award: the Award instance

    """
    try:
        award = Award.objects.get(id=award_id)
    except Award.DoesNotExist as err:
        capture_exception(err)
        return

    @email_utils.safe_translation
    def _make_mail(locale, context, email):

        subject = _("You were awarded the '{title}' badge!").format(
            title=pgettext("DB: badger.Badge.title", award.badge.title)
        )

        mail = email_utils.make_mail(
            subject=subject,
            text_template="kbadge/email/award_notification.ltxt",
            html_template="kbadge/email/award_notification.html",
            context_vars=context,
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=email,
        )

        return mail

    msg = _make_mail(
        locale=award.user.profile.locale,
        context={
            "host": Site.objects.get_current().domain,
            "award": award,
            "badge": award.badge,
        },
        email=award.user.email,
    )

    # FIXME: this sends emails to the person who was awarded the
    # badge. Should we also send an email to the person who awarded
    # the badge? Currently, I think we shouldn't.

    email_utils.send_messages([msg])
Example #22
0
def add_to_contributors(request, user):
    group = Group.objects.get(name=CONTRIBUTOR_GROUP)
    user.groups.add(group)
    user.save()

    @email_utils.safe_translation
    def _make_mail(locale):
        mail = email_utils.make_mail(
            subject=_('Welcome to SUMO!'),
            text_template='users/email/contributor.ltxt',
            html_template='users/email/contributor.html',
            context_vars={'contributor': user},
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=user.email)

        return mail

    email_utils.send_messages([_make_mail(request.LANGUAGE_CODE)])
Example #23
0
def add_to_contributors(user, language_code):
    group = Group.objects.get(name=CONTRIBUTOR_GROUP)
    user.groups.add(group)
    user.save()

    @email_utils.safe_translation
    def _make_mail(locale):
        mail = email_utils.make_mail(
            subject=_('Welcome to SUMO!'),
            text_template='users/email/contributor.ltxt',
            html_template='users/email/contributor.html',
            context_vars={'contributor': user},
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=user.email)

        return mail

    email_utils.send_messages([_make_mail(language_code)])
Example #24
0
def send_group_email(announcement_id):
    """Build and send the announcement emails to a group."""
    try:
        announcement = Announcement.objects.get(pk=announcement_id)
    except Announcement.DoesNotExist:
        return

    group = announcement.group
    users = User.objects.filter(groups__in=[group])
    plain_content = bleach.clean(announcement.content_parsed,
                                 tags=[],
                                 strip=True).strip()
    email_kwargs = {
        "content": plain_content,
        "content_html": announcement.content_parsed,
        "domain": Site.objects.get_current().domain,
    }
    text_template = "announcements/email/announcement.ltxt"
    html_template = "announcements/email/announcement.html"

    @safe_translation
    def _make_mail(locale, user):
        subject = _("New announcement for {group}").format(group=group.name)

        mail = make_mail(
            subject=subject,
            text_template=text_template,
            html_template=html_template,
            context_vars=email_kwargs,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email,
        )

        return mail

    messages = []
    for u in users:
        # Localize email each time.
        locale = u.profile.locale or settings.LANGUAGE_CODE
        messages.append(_make_mail(locale, u))

    send_messages(messages)
Example #25
0
    def _send_mail(locale):
        subject = _(u'[SUMO] You have a new private message from [{sender}]')
        subject = subject.format(sender=inbox_message.sender.username)

        context = {
            'sender': inbox_message.sender.username,
            'message': inbox_message.message,
            'message_html': inbox_message.content_parsed,
            'message_url': reverse('messages.read',
                                   kwargs={'msgid': inbox_message.id}),
            'unsubscribe_url': reverse('users.edit_settings'),
            'host': Site.objects.get_current().domain}

        mail = make_mail(subject=subject,
                         text_template='messages/email/private_message.ltxt',
                         html_template='messages/email/private_message.html',
                         context_vars=context,
                         from_email=settings.TIDINGS_FROM_ADDRESS,
                         to_email=inbox_message.to.email)

        send_messages([mail])
Example #26
0
def send_group_email(announcement_id):
    """Build and send the announcement emails to a group."""
    try:
        announcement = Announcement.objects.get(pk=announcement_id)
    except Announcement.DoesNotExist:
        return

    group = announcement.group
    users = User.objects.filter(groups__in=[group])
    plain_content = bleach.clean(announcement.content_parsed, tags=[], strip=True).strip()
    email_kwargs = {
        "content": plain_content,
        "content_html": announcement.content_parsed,
        "domain": Site.objects.get_current().domain,
    }
    text_template = "announcements/email/announcement.ltxt"
    html_template = "announcements/email/announcement.html"

    @safe_translation
    def _make_mail(locale, user):
        subject = _("New announcement for {group}").format(group=group.name)

        mail = make_mail(
            subject=subject,
            text_template=text_template,
            html_template=html_template,
            context_vars=email_kwargs,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email,
        )

        return mail

    messages = []
    for u in users:
        # Localize email each time.
        locale = u.profile.locale or settings.LANGUAGE_CODE
        messages.append(_make_mail(locale, u))

    send_messages(messages)
Example #27
0
def send_award_notification(award):
    """Sends the award notification email

    :arg award: the django-badger Award instance

    """
    @email_utils.safe_translation
    def _make_mail(locale, context, email):
        mail = email_utils.make_mail(
            subject=_(
                'You have been awarded a badge!'),  # TODO: make this suck less
            text_template='kbadge/email/award_notification.ltxt',
            html_template='kbadge/email/award_notification.html',
            context_vars=context,
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=email)

        return mail

    msg = _make_mail(
        locale=award.user.profile.locale,
        context={
            'badge_title': award.badge.title,  # TODO: l10nize this!
            'badge_description':
            award.badge.description,  # TODO: l10nize this!
            'badge_image':
            award.badge.image,  # TODO: this is an unbaked image!
            'award_description': award.description,
            'award_awardee': award.user,
            'award_awarder': award.creator,
            'award_url': award.get_absolute_url(),
        },
        email=award.user.email)

    # FIXME: this sends emails to the person who was awarded the
    # badge. Should we also send an email to the person who awarded
    # the badge? Currently, I think we shouldn't.

    email_utils.send_messages([msg])
Example #28
0
    def _send_email(self, confirmation_profile, url, subject, text_template,
                    html_template, send_to, **kwargs):
        """
        Send an email using a passed in confirmation profile.

        Use specified url, subject, text_template, html_template and
        email to send_to.
        """
        current_site = Site.objects.get_current()
        email_kwargs = {
            'activation_key': confirmation_profile.activation_key,
            'domain': current_site.domain,
            'activate_url': url,
            'login_url': reverse('users.login'),
            'reg': 'main'
        }
        email_kwargs.update(kwargs)

        # RegistrationProfile doesn't have a locale attribute. So if
        # we get one of those, then we have to get the real profile
        # from the user.
        if hasattr(confirmation_profile, 'locale'):
            locale = confirmation_profile.locale
        else:
            locale = confirmation_profile.user.profile.locale

        @email_utils.safe_translation
        def _make_mail(locale):
            mail = email_utils.make_mail(
                subject=subject,
                text_template=text_template,
                html_template=html_template,
                context_vars=email_kwargs,
                from_email=settings.DEFAULT_FROM_EMAIL,
                to_email=send_to)

            return mail

        email_utils.send_messages([_make_mail(locale)])
Example #29
0
    def _send_email(self, confirmation_profile, url,
                    subject, text_template, html_template,
                    send_to, **kwargs):
        """
        Send an email using a passed in confirmation profile.

        Use specified url, subject, text_template, html_template and
        email to send_to.
        """
        current_site = Site.objects.get_current()
        email_kwargs = {'activation_key': confirmation_profile.activation_key,
                        'domain': current_site.domain,
                        'activate_url': url,
                        'login_url': reverse('users.login'),
                        'reg': 'main'}
        email_kwargs.update(kwargs)

        # RegistrationProfile doesn't have a locale attribute. So if
        # we get one of those, then we have to get the real profile
        # from the user.
        if hasattr(confirmation_profile, 'locale'):
            locale = confirmation_profile.locale
        else:
            locale = confirmation_profile.user.profile.locale

        @email_utils.safe_translation
        def _make_mail(locale):
            mail = email_utils.make_mail(
                subject=subject,
                text_template=text_template,
                html_template=html_template,
                context_vars=email_kwargs,
                from_email=settings.DEFAULT_FROM_EMAIL,
                to_email=send_to)

            return mail

        email_utils.send_messages([_make_mail(locale)])
Example #30
0
    def request_password_reset(self, request, user__username=None):
        profile = self.get_object()

        current_site = get_current_site(request)
        site_name = current_site.name
        domain = current_site.domain

        c = {
            "email": profile.user.email,
            "domain": domain,
            "site_name": site_name,
            "uid": int_to_base36(profile.user.id),
            "user": profile.user,
            "token": default_token_generator.make_token(profile.user),
            "protocol": "https" if request.is_secure() else "http",
        }

        subject = email_utils.render_email("users/email/pw_reset_subject.ltxt",
                                           c)
        # Email subject *must not* contain newlines
        subject = "".join(subject.splitlines())

        @email_utils.safe_translation
        def _make_mail(locale):
            mail = email_utils.make_mail(
                subject=subject,
                text_template="users/email/pw_reset.ltxt",
                html_template="users/email/pw_reset.html",
                context_vars=c,
                from_email=None,
                to_email=profile.user.email,
            )

            return mail

        email_utils.send_messages([_make_mail(profile.locale)])

        return Response("", status=204)
Example #31
0
def send_award_notification(award):
    """Sends the award notification email

    :arg award: the django-badger Award instance

    """
    @email_utils.safe_translation
    def _make_mail(locale, context, email):
        mail = email_utils.make_mail(
            subject=_('You have been awarded a badge!'),  # TODO: make this suck less
            text_template='kbadge/email/award_notification.ltxt',
            html_template='kbadge/email/award_notification.html',
            context_vars=context,
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=email)

        return mail

    msg = _make_mail(
        locale=award.user.profile.locale,
        context={
            'badge_title': award.badge.title,  # TODO: l10nize this!
            'badge_description': award.badge.description,  # TODO: l10nize this!
            'badge_image': award.badge.image,  # TODO: this is an unbaked image!
            'award_description': award.description,
            'award_awardee': award.user,
            'award_awarder': award.creator,
            'award_url': award.get_absolute_url(),
        },
        email=award.user.email
    )

    # FIXME: this sends emails to the person who was awarded the
    # badge. Should we also send an email to the person who awarded
    # the badge? Currently, I think we shouldn't.

    email_utils.send_messages([msg])
Example #32
0
    def request_password_reset(self, request, user__username=None):
        profile = self.get_object()

        current_site = get_current_site(request)
        site_name = current_site.name
        domain = current_site.domain

        c = {
            'email': profile.user.email,
            'domain': domain,
            'site_name': site_name,
            'uid': int_to_base36(profile.user.id),
            'user': profile.user,
            'token': default_token_generator.make_token(profile.user),
            'protocol': 'https' if request.is_secure() else 'http',
        }

        subject = email_utils.render_email('users/email/pw_reset_subject.ltxt',
                                           c)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        @email_utils.safe_translation
        def _make_mail(locale):
            mail = email_utils.make_mail(
                subject=subject,
                text_template='users/email/pw_reset.ltxt',
                html_template='users/email/pw_reset.html',
                context_vars=c,
                from_email=None,
                to_email=profile.user.email)

            return mail

        email_utils.send_messages([_make_mail(profile.locale)])

        return Response('', status=204)
Example #33
0
def send_award_notification(award):
    """Sends the award notification email

    :arg award: the Award instance

    """
    @email_utils.safe_translation
    def _make_mail(locale, context, email):
        subject = _(u"You were awarded the '{title}' badge!").format(
            title=pgettext('DB: badger.Badge.title', award.badge.title))

        mail = email_utils.make_mail(
            subject=subject,
            text_template='kbadge/email/award_notification.ltxt',
            html_template='kbadge/email/award_notification.html',
            context_vars=context,
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=email)

        return mail

    msg = _make_mail(
        locale=award.user.profile.locale,
        context={
            'host': Site.objects.get_current().domain,
            'award': award,
            'badge': award.badge,
        },
        email=award.user.email
    )

    # FIXME: this sends emails to the person who was awarded the
    # badge. Should we also send an email to the person who awarded
    # the badge? Currently, I think we shouldn't.

    email_utils.send_messages([msg])
Example #34
0
    def request_password_reset(self, request, user__username=None):
        profile = self.get_object()

        current_site = get_current_site(request)
        site_name = current_site.name
        domain = current_site.domain

        c = {
            'email': profile.user.email,
            'domain': domain,
            'site_name': site_name,
            'uid': int_to_base36(profile.user.id),
            'user': profile.user,
            'token': default_token_generator.make_token(profile.user),
            'protocol': 'https' if request.is_secure() else 'http',
        }

        subject = email_utils.render_email('users/email/pw_reset_subject.ltxt', c)
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        @email_utils.safe_translation
        def _make_mail(locale):
            mail = email_utils.make_mail(
                subject=subject,
                text_template='users/email/pw_reset.ltxt',
                html_template='users/email/pw_reset.html',
                context_vars=c,
                from_email=None,
                to_email=profile.user.email)

            return mail

        email_utils.send_messages([_make_mail(profile.locale)])

        return Response('', status=204)
Example #35
0
def make_contributor(request):
    """Adds the logged in user to the contributor group"""
    group = Group.objects.get(name=CONTRIBUTOR_GROUP)
    request.user.groups.add(group)

    @email_utils.safe_translation
    def _make_mail(locale):
        mail = email_utils.make_mail(
            subject=_("Welcome to SUMO!"),
            text_template="users/email/contributor.ltxt",
            html_template="users/email/contributor.html",
            context_vars={"username": request.user.username},
            from_email=settings.DEFAULT_FROM_EMAIL,
            to_email=request.user.email,
        )

        return mail

    email_utils.send_messages([_make_mail(request.LANGUAGE_CODE)])

    if "return_to" in request.POST:
        return HttpResponseRedirect(request.POST["return_to"])
    else:
        return HttpResponseRedirect(reverse("landings.get_involved"))
Example #36
0
def send_welcome_emails():
    """Send a welcome email to first time contributors.

    Anyone who has made a contribution more than 24 hours ago and has not
    already gotten a welcome email should get a welcome email.
    """

    wait_period = datetime.now() - timedelta(hours=24)
    messages = []
    context = {
        'host': Site.objects.get_current().domain,
    }

    # Answers

    answer_filter = Q(created__lte=wait_period)
    answer_filter &= ~Q(question__creator=F('creator'))
    answer_filter &= Q(creator__profile__first_answer_email_sent=False)

    answer_recipient_ids = set(
        Answer.objects
        .filter(answer_filter)
        .values_list('creator', flat=True))

    @safe_translation
    def _make_answer_email(locale, to):
        return make_mail(subject=_('Thank you for your contribution to Mozilla Support!'),
                         text_template='community/email/first_answer.ltxt',
                         html_template='community/email/first_answer.html',
                         context_vars=context,
                         from_email=settings.TIDINGS_FROM_ADDRESS,
                         to_email=to.email)

    for user in User.objects.filter(id__in=answer_recipient_ids):
        messages.append(_make_answer_email(user.profile.locale, user))

    # Localization

    l10n_filter = Q(created__lte=wait_period)
    l10n_filter &= ~Q(document__locale=settings.WIKI_DEFAULT_LANGUAGE)
    l10n_filter &= Q(creator__profile__first_l10n_email_sent=False)

    l10n_recipient_ids = set(
        Revision.objects
        .filter(l10n_filter)
        .values_list('creator', flat=True))

    # This doesn't need localized, and so don't need the `safe_translation` helper.
    for user in User.objects.filter(id__in=l10n_recipient_ids):
        messages.append(make_mail(
            subject='Thank you for your contribution to Mozilla Support!',
            text_template='community/email/first_l10n.ltxt',
            html_template='community/email/first_l10n.html',
            context_vars=context,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email))

    # Release the Kraken!
    send_messages(messages)

    Profile.objects.filter(user__id__in=answer_recipient_ids).update(first_answer_email_sent=True)
    Profile.objects.filter(user__id__in=l10n_recipient_ids).update(first_l10n_email_sent=True)
Example #37
0
def send_contributor_notification(based_on_ids: List[int], revision_id: int,
                                  document_id: int, message: str):
    """Send notification of review to the contributors of revisions."""

    try:
        revision = Revision.objects.get(id=revision_id)
        document = Document.objects.get(id=document_id)
    except (Revision.DoesNotExist, Document.DoesNotExist) as err:
        capture_exception(err)
        return

    based_on = Revision.objects.filter(id__in=based_on_ids)

    text_template = "wiki/email/reviewed_contributors.ltxt"
    html_template = "wiki/email/reviewed_contributors.html"
    url = reverse("wiki.document_revisions",
                  locale=document.locale,
                  args=[document.slug])
    c = {
        "document_title": document.title,
        "approved": revision.is_approved,
        "reviewer": revision.reviewer,
        "message": message,
        "revisions_url": url,
        "host": Site.objects.get_current().domain,
    }

    msgs = []

    @email_utils.safe_translation
    def _make_mail(locale, user):
        if revision.is_approved:
            subject = _(
                "A revision you contributed to has been approved: {title}")
        else:
            subject = _(
                "A revision you contributed to has been reviewed: {title}")
        subject = subject.format(title=document.title)

        mail = email_utils.make_mail(
            subject=subject,
            text_template=text_template,
            html_template=html_template,
            context_vars=c,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email,
        )

        msgs.append(mail)

    for r in based_on:
        # Send email to all contributors except the reviewer and the creator
        # of the approved revision.
        if r.creator in [revision.creator, revision.reviewer]:
            continue

        user = r.creator

        if hasattr(user, "profile"):
            locale = user.profile.locale
        else:
            locale = settings.WIKI_DEFAULT_LANGUAGE

        _make_mail(locale, user)

    email_utils.send_messages(msgs)
Example #38
0
def send_welcome_emails():
    """Send a welcome email to first time contributors.

    Anyone who has made a contribution more than 24 hours ago and has not
    already gotten a welcome email should get a welcome email.
    """

    wait_period = datetime.now() - timedelta(hours=24)
    messages = []
    context = {
        'host': Site.objects.get_current().domain,
    }

    # Answers

    answer_filter = Q(created__lte=wait_period)
    answer_filter &= ~Q(question__creator=F('creator'))
    answer_filter &= Q(creator__profile__first_answer_email_sent=False)

    answer_recipient_ids = set(
        Answer.objects
        .filter(answer_filter)
        .values_list('creator', flat=True))

    @safe_translation
    def _make_answer_email(locale, to):
        return make_mail(subject=_('Thank you for your contribution to Mozilla Support!'),
                         text_template='community/email/first_answer.ltxt',
                         html_template='community/email/first_answer.html',
                         context_vars=context,
                         from_email=settings.TIDINGS_FROM_ADDRESS,
                         to_email=to.email)

    for user in User.objects.filter(id__in=answer_recipient_ids):
        messages.append(_make_answer_email(user.profile.locale, user))

    # Localization

    l10n_filter = Q(created__lte=wait_period)
    l10n_filter &= ~Q(document__locale=settings.WIKI_DEFAULT_LANGUAGE)
    l10n_filter &= Q(creator__profile__first_l10n_email_sent=False)

    l10n_recipient_ids = set(
        Revision.objects
        .filter(l10n_filter)
        .values_list('creator', flat=True))

    # This doesn't need localized, and so don't need the `safe_translation` helper.
    for user in User.objects.filter(id__in=l10n_recipient_ids):
        messages.append(make_mail(
            subject='Thank you for your contribution to Mozilla Support!',
            text_template='community/email/first_l10n.ltxt',
            html_template='community/email/first_l10n.html',
            context_vars=context,
            from_email=settings.TIDINGS_FROM_ADDRESS,
            to_email=user.email))

    # Release the Kraken!
    send_messages(messages)

    Profile.objects.filter(user__id__in=answer_recipient_ids).update(first_answer_email_sent=True)
    Profile.objects.filter(user__id__in=l10n_recipient_ids).update(first_l10n_email_sent=True)