Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4181 from jwhitlock/diff_trans_1269104
Browse files Browse the repository at this point in the history
bug 1269104: Refactor notification emails, provide diff for first translations
  • Loading branch information
escattone committed Apr 19, 2017
2 parents 8426688 + 1d4b339 commit bff42c1
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 61 deletions.
15 changes: 2 additions & 13 deletions kuma/wiki/apps.py
@@ -1,11 +1,7 @@
from constance import config

from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.mail import send_mail
from django.db.models import signals
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _

from elasticsearch_dsl.connections import connections as es_connections
Expand Down Expand Up @@ -144,12 +140,5 @@ def on_document_spam_attempt_save(
if raw or not created:
# Only send for new instances, not fixtures or edits
return
subject = u'[MDN] Wiki spam attempt recorded'
if instance.document:
subject = u'%s for document %s' % (subject, instance.document)
elif instance.title:
subject = u'%s with title %s' % (subject, instance.title)
body = render_to_string('wiki/email/spam.ltxt',
{'spam_attempt': instance})
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL,
[config.EMAIL_LIST_SPAM_WATCH])
from .events import spam_attempt_email
spam_attempt_email(instance).send()
45 changes: 42 additions & 3 deletions kuma/wiki/events.py
@@ -1,5 +1,12 @@
# -*- coding: utf-8 -*-
"""Send notification emails for editing events."""
from __future__ import unicode_literals
import logging

from constance import config
from django.conf import settings
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils.translation import ugettext
from tidings.events import EventUnion, InstanceEvent

Expand All @@ -14,7 +21,7 @@
log = logging.getLogger('kuma.wiki.events')


def context_dict(revision):
def notification_context(revision):
"""
Return a dict that fills in the blanks in notification templates.
"""
Expand Down Expand Up @@ -70,8 +77,8 @@ def _mails(self, users_and_watches):
log.debug('Sending edited notification email for document (id=%s)' %
document.id)
subject = ugettext(
u'[MDN] Page "%(document_title)s" changed by %(creator)s')
context = context_dict(revision)
'[MDN] Page "%(document_title)s" changed by %(creator)s')
context = notification_context(revision)

return emails_with_users_and_watches(
subject=subject,
Expand All @@ -97,3 +104,35 @@ class EditDocumentInTreeEvent(InstanceEvent):
"""
event_type = 'wiki edit document in tree'
content_type = Document


def first_edit_email(revision):
"""Create a notification email for first-time editors."""
user, doc = revision.creator, revision.document
subject = ("[MDN] [%(loc)s] %(user)s made their first edit, to: %(doc)s" %
{'loc': doc.locale, 'user': user.username, 'doc': doc.title})
message = render_to_string('wiki/email/edited.ltxt',
notification_context(revision))
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_SPAM_WATCH],
headers={'X-Kuma-Document-Url': doc.get_full_url(),
'X-Kuma-Editor-Username': user.username})
return email


def spam_attempt_email(spam_attempt):
"""
Create a notification email for a spam attempt.
Because the spam may be on document creation, the document might be null.
"""
subject = '[MDN] Wiki spam attempt recorded'
if spam_attempt.document:
subject = '%s for document %s' % (subject, spam_attempt.document)
elif spam_attempt.title:
subject = '%s with title %s' % (subject, spam_attempt.title)
body = render_to_string('wiki/email/spam.ltxt',
{'spam_attempt': spam_attempt})
email = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_SPAM_WATCH])
return email
2 changes: 1 addition & 1 deletion kuma/wiki/models.py
Expand Up @@ -1806,7 +1806,7 @@ def get_previous(self):
created__lt=self.created,
).order_by('-created')[0]
except IndexError:
return None
return self.based_on

@cached_property
def needs_editorial_review(self):
Expand Down
17 changes: 3 additions & 14 deletions kuma/wiki/tasks.py
Expand Up @@ -9,13 +9,12 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.sitemaps import GenericSitemap
from django.core.mail import EmailMessage, mail_admins, send_mail
from django.core.mail import mail_admins, send_mail
from django.db import connection, transaction
from django.template.loader import render_to_string
from django.utils.encoding import smart_str

from celery import chord, task
from constance import config
from djcelery_transactions import task as transaction_task
from lxml import etree

Expand All @@ -24,7 +23,7 @@
from kuma.core.utils import MemcacheLock, chord_flow, chunked
from kuma.search.models import Index

from .events import context_dict
from .events import first_edit_email
from .exceptions import PageMoveError, StaleDocumentsRenderingInProgress
from .models import Document, DocumentSpamAttempt, Revision, RevisionIP
from .search import WikiDocumentType
Expand Down Expand Up @@ -312,17 +311,7 @@ def delete_old_revision_ips(days=30):
def send_first_edit_email(revision_pk):
""" Make an 'edited' notification email for first-time editors """
revision = Revision.objects.get(pk=revision_pk)
user, doc = revision.creator, revision.document
subject = (u"[MDN] [%(loc)s] %(user)s made their first edit, to: %(doc)s" %
{'loc': doc.locale, 'user': user.username, 'doc': doc.title})
message = render_to_string('wiki/email/edited.ltxt',
context_dict(revision))
doc_url = absolutify(doc.get_absolute_url())
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_SPAM_WATCH],
headers={'X-Kuma-Document-Url': doc_url,
'X-Kuma-Editor-Username': user.username})
email.send()
first_edit_email(revision).send()


class WikiSitemap(GenericSitemap):
Expand Down
72 changes: 72 additions & 0 deletions kuma/wiki/tests/conftest.py
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
"""py.test fixtures for kuma.wiki.tests."""

import pytest
from datetime import datetime

from ..models import Document, Revision


@pytest.fixture
def wiki_user(db, django_user_model):
"""A test user."""
return django_user_model.objects.create(
username='wiki_user',
email='wiki_user@example.com',
date_joined=datetime(2017, 4, 14, 12, 0))


@pytest.fixture
def root_doc(wiki_user):
"""A newly-created top-level English document."""
root_doc = Document.objects.create(
locale='en-US', slug='Root', title='Root Document')
Revision.objects.create(
document=root_doc,
creator=wiki_user,
content='<p>Getting started...</p>',
title='Root Document',
created=datetime(2017, 4, 14, 12, 15))
return root_doc


@pytest.fixture
def create_revision(root_doc):
"""A revision that created an English document."""
return root_doc.revisions.first()


@pytest.fixture
def edit_revision(root_doc, wiki_user):
"""A revision that edits an English document."""
root_doc.current_revision = Revision.objects.create(
document=root_doc,
creator=wiki_user,
content='<p>The root document.</p>',
comment='Done with initial version.',
created=datetime(2017, 4, 14, 12, 30))
root_doc.save()
return root_doc.current_revision


@pytest.fixture
def trans_doc(create_revision, wiki_user):
"""Translate the root document into French."""
trans_doc = Document.objects.create(
locale='fr',
parent=create_revision.document,
slug='Racine',
title='Racine du Document')
Revision.objects.create(
document=trans_doc,
creator=wiki_user,
based_on=create_revision,
content='<p>Mise en route...</p>',
title='Racine du Document',
created=datetime(2017, 4, 14, 12, 20))
return trans_doc


@pytest.fixture
def trans_revision(trans_doc):
return trans_doc.current_revision

0 comments on commit bff42c1

Please sign in to comment.