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 #3825 from mozilla/spam_dashboard_fixes_1255609
Browse files Browse the repository at this point in the history
bug 1255609 - Fixes for spam dashboard
  • Loading branch information
jezdez committed Mar 30, 2016
2 parents 7f15829 + d40a53f commit 5167c6e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test:
coveragetest: clean
py.test --cov=$(target) $(target)

coveragetesthtml: coveragetest
coverage html

locust:
locust -f tests/performance/smoke.py --host=https://developer.allizom.org

Expand Down Expand Up @@ -37,6 +40,8 @@ intern:
clean:
rm -rf .coverage build/
find kuma -name '*.pyc' -exec rm {} \;
mkdir -p build/assets
mkdir -p build/locale

locale:
@mkdir -p locale/$(LOCALE)/LC_MESSAGES && \
Expand Down
4 changes: 2 additions & 2 deletions kuma/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,9 +1217,9 @@ def _get_languages_and_locales():
"Janet Swisher <no-reply@mozilla.org>",
'Email address from which welcome emails will be sent',
),
EMAIL_LIST_FOR_FIRST_EDITS=(
EMAIL_LIST_SPAM_WATCH=(
"mdn-spam-watch@mozilla.com",
"Email address to which emails will be sent for users' first edits",
"Email address to notify of possible spam (first edits, blocked edits)",
),
AKISMET_KEY=(
'',
Expand Down
34 changes: 31 additions & 3 deletions kuma/wiki/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from datetime import datetime
import json

Expand All @@ -10,6 +12,8 @@
from django.template.defaultfilters import truncatechars
from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import Truncator
from waffle import flag_is_active

from kuma.core.admin import DisabledDeletionMixin
Expand Down Expand Up @@ -342,7 +346,7 @@ class DocumentZoneAdmin(admin.ModelAdmin):
@admin.register(DocumentSpamAttempt)
class DocumentSpamAttemptAdmin(admin.ModelAdmin):
list_display = [
'id', 'user', 'title_short', 'slug_short', 'document', 'review']
'id', 'user', 'title_short', 'slug_short', 'doc_short', 'review']
list_display_links = ['id', 'title_short', 'slug_short']
list_filter = [
'created', 'review', 'document__deleted', 'document__locale']
Expand All @@ -355,14 +359,38 @@ class DocumentSpamAttemptAdmin(admin.ModelAdmin):
'review', 'reviewed', 'reviewer']
readonly_fields = ['created', 'submitted_data', 'reviewer', 'reviewed']

MAX_LENGTH = 25

def title_short(self, obj):
return truncatechars(obj.title, 25)
return truncatechars(obj.title, self.MAX_LENGTH)
title_short.short_description = 'Title'

def slug_short(self, obj):
return truncatechars(obj.slug, 25)
return truncatechars(obj.slug, self.MAX_LENGTH)
slug_short.short_description = 'Slug'

def doc_short(self, obj):
u"""
Shorten document 'path (name)' representation in list view.
The important part is having an HTML break character such as a space,
so truncating paths as well as long titles, to look like:
/en-US/docs/Start/Of/Slug… (The start of the title…)
"""
doc = obj.document
if doc:
full_path = u'/%s/docs/%s' % (doc.locale, doc.slug)
if len(full_path) <= self.MAX_LENGTH:
path = full_path
else:
path = Truncator(full_path).chars(self.MAX_LENGTH, u'…')
title = Truncator(doc.title).chars(self.MAX_LENGTH, u'…')
return u'%s (%s)' % (path, title)
else:
return mark_safe('<em>new document</em>')
doc_short.short_description = 'Document (if edit)'

class NotEnabled(Exception):
"""Akismet is not enabled"""

Expand Down
10 changes: 8 additions & 2 deletions kuma/wiki/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from constance import config

from django.apps import AppConfig
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
Expand Down Expand Up @@ -137,7 +139,11 @@ def on_revision_save(self, sender, instance, **kwargs):
from .tasks import tidy_revision_content
tidy_revision_content.delay(instance.pk)

def on_document_spam_attempt_save(self, sender, instance, **kwargs):
def on_document_spam_attempt_save(
self, sender, instance, created, raw, **kwargs):
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)
Expand All @@ -146,4 +152,4 @@ def on_document_spam_attempt_save(self, sender, instance, **kwargs):
body = render_to_string('wiki/email/spam.ltxt',
{'spam_attempt': instance})
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL,
['mdn-spam-watch@mozilla.com'])
[config.EMAIL_LIST_SPAM_WATCH])
2 changes: 1 addition & 1 deletion kuma/wiki/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def send_first_edit_email(revision_pk):
context_dict(revision))
doc_url = absolutify(doc.get_absolute_url())
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_FOR_FIRST_EDITS],
to=[config.EMAIL_LIST_SPAM_WATCH],
headers={'X-Kuma-Document-Url': doc_url,
'X-Kuma-Editor-Username': user.username})
email.send()
Expand Down
37 changes: 37 additions & 0 deletions kuma/wiki/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import pytest
from pyquery import PyQuery as pq

Expand All @@ -15,6 +16,7 @@
from kuma.users.models import User
from kuma.wiki.admin import DocumentSpamAttemptAdmin
from kuma.wiki.models import DocumentSpamAttempt, RevisionAkismetSubmission
from kuma.wiki.tests import document, revision


@pytest.mark.spam
Expand Down Expand Up @@ -54,6 +56,41 @@ def test_slug_short(self):
dsa.slug = 'Web/A_long_slug_that_will_be_truncated'
assert self.admin.slug_short(dsa) == 'Web/A_long_slug_that_w...'''

def test_doc_short_without_document(self):
dsa = DocumentSpamAttempt(slug='Slug')
assert self.admin.doc_short(dsa) == '<em>new document</em>'

def test_doc_short_short_slug_and_title(self):
slug = 'NotSpam'
html = '<p>This page is not spam.</p>'
doc = document(title='blah', slug=slug, html=html, save=True)
revision(document=doc, content=html, is_approved=True, save=True)
dsa = DocumentSpamAttempt(slug=slug, document=doc)
assert self.admin.doc_short(dsa) == u'/en-US/docs/NotSpam (blah)'
assert self.admin.doc_short(dsa) == str(doc)

def test_doc_short_long_slug_and_title(self):
slug = 'Web/Guide/HTML/Sections_and_Outlines_of_an_HTML5_document'
title = 'Sections and Outlines of an HTML5 Document'
html = '<p>This German page is not spam.</p>'
doc = document(title=title, slug=slug, html=html, save=True,
locale='de')
revision(document=doc, content=html, is_approved=True, save=True)
dsa = DocumentSpamAttempt(slug=slug, document=doc)
expected = u'/de/docs/Web/Guide/HTML/… (Sections and Outlines of…)'
assert self.admin.doc_short(dsa) == expected

def test_doc_short_long_unicode(self):
slug = u'Web/Guide/HTML/HTML5_ডকুমেন্টের_সেকশন_এবং_আউটলাইন'
title = u'HTML5 ডকুমেন্টের সেকশন এবং আউটলাইন'
html = '<p>This Bengali page is not spam.</p>'
doc = document(title=title, slug=slug, html=html, save=True,
locale='bn-BD')
revision(document=doc, content=html, is_approved=True, save=True)
dsa = DocumentSpamAttempt(slug=slug, document=doc)
expected = u'/bn-BD/docs/Web/Guide/HT… (HTML5 ডকুমেন্টের সেকশন এব…)'
assert self.admin.doc_short(dsa) == expected

def test_submitted_data(self):
dsa = DocumentSpamAttempt(data=None)
expected = self.admin.SUBMISSION_NOT_AVAILABLE
Expand Down
2 changes: 1 addition & 1 deletion kuma/wiki/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def test_new_revision_POST_document_with_current(self, get_current):
time.sleep(1)
eq_(2, len(mail.outbox))
first_edit_email = mail.outbox[0]
expected_to = [config.EMAIL_LIST_FOR_FIRST_EDITS]
expected_to = [config.EMAIL_LIST_SPAM_WATCH]
expected_subject = u'[MDN] %(username)s made their first edit, to: %(title)s' % ({'username': new_rev.creator.username, 'title': self.d.title})
eq_(expected_subject, first_edit_email.subject)
eq_(expected_to, first_edit_email.to)
Expand Down

0 comments on commit 5167c6e

Please sign in to comment.