store abuse reports in the db (bug 675357)

This commit is contained in:
Jeff Balogh 2011-08-03 16:17:44 -07:00
Родитель c2a947b545
Коммит 2808df2dce
10 изменённых файлов: 131 добавлений и 50 удалений

0
apps/abuse/__init__.py Normal file
Просмотреть файл

53
apps/abuse/models.py Normal file
Просмотреть файл

@ -0,0 +1,53 @@
import logging
from django.conf import settings
from django.db import models
import amo.models
import amo.utils
from addons.models import Addon
from users.models import UserProfile
log = logging.getLogger('z.abuse')
class AbuseReport(amo.models.ModelBase):
# NULL if the reporter is anonymous.
reporter = models.ForeignKey(UserProfile, null=True,
related_name='abuse_reported')
ip_address = models.CharField(max_length=255, default='0.0.0.0')
# An abuse report can be for an addon or a user. Only one of these should
# be null.
addon = models.ForeignKey(Addon, null=True, related_name='abuse_reports')
user = models.ForeignKey(UserProfile, null=True,
related_name='abuse_reports')
message = models.TextField()
class Meta:
db_table = 'abuse_reports'
def send(self):
obj = self.addon or self.user
if self.reporter:
user_name = '%s (%s)' % (self.reporter.name, self.reporter.email)
else:
user_name = 'An anonymous user'
subject = 'Abuse Report for %s' % obj.name
msg = u'%s reported abuse for %s (%s%s).\n\n%s' % (
user_name, obj.name, settings.SITE_URL, obj.get_url_path(),
self.message)
amo.utils.send_mail(subject, msg, recipient_list=(settings.FLIGTAR,))
def send_abuse_report(request, obj, message):
report = AbuseReport(ip_address=request.META.get('REMOTE_ADDR'),
message=message)
if request.user.is_authenticated():
report.reporter = request.amo_user
if isinstance(obj, Addon):
report.addon = obj
elif isinstance(obj, UserProfile):
report.user = obj
report.save()
report.send()

Просмотреть файл

@ -19,7 +19,7 @@ import amo
import amo.tests
from amo.helpers import absolutify
from amo.urlresolvers import reverse
from amo.tests.test_helpers import AbuseBase
from abuse.models import AbuseReport
from addons.models import Addon, AddonUser, Charity
from files.models import File
from paypal.tests import other_error
@ -1029,7 +1029,7 @@ class TestAddonSharing(amo.tests.TestCase):
assert iri_to_uri(summary) in r['Location']
class TestReportAbuse(AbuseBase, amo.tests.TestCase):
class TestReportAbuse(amo.tests.TestCase):
fixtures = ['addons/persona',
'base/apps',
'base/addon_3615',
@ -1039,6 +1039,29 @@ class TestReportAbuse(AbuseBase, amo.tests.TestCase):
settings.RECAPTCHA_PRIVATE_KEY = 'something'
self.full_page = reverse('addons.abuse', args=['a3615'])
@patch('captcha.fields.ReCaptchaField.clean')
def test_abuse_anonymous(self, clean):
clean.return_value = ""
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
report = AbuseReport.objects.get(addon=3615)
eq_(report.message, 'spammy')
eq_(report.reporter, None)
def test_abuse_anonymous_fails(self):
r = self.client.post(self.full_page, {'text': 'spammy'})
assert 'recaptcha' in r.context['abuse_form'].errors
def test_abuse_logged_in(self):
self.client.login(username='regular@mozilla.com', password='password')
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
report = AbuseReport.objects.get(addon=3615)
eq_(report.message, 'spammy')
eq_(report.reporter.email, 'regular@mozilla.com')
def test_abuse_name(self):
addon = Addon.objects.get(pk=3615)
addon.name = 'Bmrk.ru Социальные закладки'
@ -1047,6 +1070,7 @@ class TestReportAbuse(AbuseBase, amo.tests.TestCase):
self.client.login(username='regular@mozilla.com', password='password')
self.client.post(self.full_page, {'text': 'spammy'})
assert 'spammy' in mail.outbox[0].body
assert AbuseReport.objects.get(addon=addon)
def test_abuse_persona(self):
addon_url = reverse('addons.detail', args=['a15663'])
@ -1061,7 +1085,7 @@ class TestReportAbuse(AbuseBase, amo.tests.TestCase):
self.assertRedirects(r, addon_url)
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
assert AbuseReport.objects.get(addon=15663)
class TestMobile(amo.tests.TestCase):
fixtures = ['addons/featured', 'base/apps', 'base/addon_3615',

Просмотреть файл

@ -25,11 +25,12 @@ from mobility.decorators import mobilized, mobile_template
import amo
from amo import messages
from amo.forms import AbuseForm
from amo.utils import sorted_groupby, randslice, send_abuse_report
from amo.utils import sorted_groupby, randslice
from amo.helpers import absolutify
from amo.models import manual_order
from amo import urlresolvers
from amo.urlresolvers import reverse
from abuse.models import send_abuse_report
from addons.utils import FeaturedManager
from bandwagon.models import Collection, CollectionFeature, CollectionPromo
import paypal
@ -520,6 +521,7 @@ def eula(request, addon, file_id=None):
return jingo.render(request, 'addons/eula.html',
{'addon': addon, 'version': version})
@addon_view
def impala_eula(request, addon, file_id=None):
if not addon.eula:
@ -796,8 +798,7 @@ def license_redirect(request, version):
def report_abuse(request, addon):
form = AbuseForm(request.POST or None, request=request)
if request.method == "POST" and form.is_valid():
url = reverse('addons.detail', args=[addon.slug])
send_abuse_report(request, addon, url, form.cleaned_data['text'])
send_abuse_report(request, addon, form.cleaned_data['text'])
messages.success(request, _('Abuse reported.'))
return redirect('addons.detail', addon.slug)
else:

Просмотреть файл

@ -304,25 +304,6 @@ class TestLicenseLink(amo.tests.TestCase):
eq_(s, ex)
class AbuseBase:
@patch('captcha.fields.ReCaptchaField.clean')
def test_abuse_anonymous(self, clean):
clean.return_value = ""
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
def test_abuse_anonymous_fails(self):
r = self.client.post(self.full_page, {'text': 'spammy'})
assert 'recaptcha' in r.context['abuse_form'].errors
def test_abuse_logged_in(self):
self.client.login(username='regular@mozilla.com', password='password')
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
def get_image_path(name):
return os.path.join(settings.ROOT, 'apps', 'amo', 'tests', 'images', name)

Просмотреть файл

@ -24,7 +24,6 @@ from django.utils.translation import trans_real
from django.utils.functional import Promise
from django.utils.encoding import smart_str, smart_unicode
import bleach
from easy_thumbnails import processors
import html5lib
from html5lib.serializer.htmlserializer import HTMLSerializer
@ -34,12 +33,13 @@ from PIL import Image, ImageFile, PngImagePlugin
import amo.search
from amo import ADDON_ICON_SIZES
from amo.urlresolvers import reverse
from . import logger_log as log
from translations.models import Translation
from users.models import UserNotification
import users.notifications as notifications
from users.utils import UnsubscribeCode
from . import logger_log as log
def urlparams(url_, hash=None, **query):
"""
@ -448,24 +448,6 @@ class MenuItem():
url, text, selected, children = ('', '', False, [])
def send_abuse_report(request, obj, url, message):
"""Send email about an abusive addon/user/relationship."""
if request.user.is_anonymous():
user_name = 'An anonymous user'
else:
user_name = '%s (%s)' % (request.amo_user.name,
request.amo_user.email)
subject = 'Abuse Report for %s' % obj.name
msg = u'%s reported abuse for %s (%s%s).\n\n%s'
msg = msg % (user_name, obj.name, settings.SITE_URL, url, message)
msg += '\n\nhttp://translate.google.com/#auto|en|%s' % message
log.debug('Abuse reported by %s for %s: %s.' %
(smart_str(user_name), obj.id, smart_str(obj.name)))
send_mail(subject, msg, recipient_list=(settings.FLIGTAR,))
def to_language(locale):
"""Like django's to_language, but en_US comes out as en-US."""
# A locale looks like en_US or fr.

Просмотреть файл

@ -14,12 +14,12 @@ from nose.tools import eq_
import amo
import amo.tests
from abuse.models import AbuseReport
from access.models import Group, GroupUser
from addons.models import Addon, AddonUser
from amo.helpers import urlparams
from amo.pyquery_wrapper import PyQuery as pq
from amo.urlresolvers import reverse
from amo.tests.test_helpers import AbuseBase
from devhub.models import ActivityLog
from users.models import BlacklistedPassword, UserProfile, UserNotification
import users.notifications as email
@ -597,9 +597,32 @@ class TestProfile(UserViewBase):
for i in xrange(len(addons) - 1))
class TestReportAbuse(AbuseBase, amo.tests.TestCase):
class TestReportAbuse(amo.tests.TestCase):
fixtures = ['base/users']
def setUp(self):
settings.RECAPTCHA_PRIVATE_KEY = 'something'
self.full_page = reverse('users.abuse', args=[10482])
@patch('captcha.fields.ReCaptchaField.clean')
def test_abuse_anonymous(self, clean):
clean.return_value = ""
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
report = AbuseReport.objects.get(user=10482)
eq_(report.message, 'spammy')
eq_(report.reporter, None)
def test_abuse_anonymous_fails(self):
r = self.client.post(self.full_page, {'text': 'spammy'})
assert 'recaptcha' in r.context['abuse_form'].errors
def test_abuse_logged_in(self):
self.client.login(username='regular@mozilla.com', password='password')
self.client.post(self.full_page, {'text': 'spammy'})
eq_(len(mail.outbox), 1)
assert 'spammy' in mail.outbox[0].body
report = AbuseReport.objects.get(user=10482)
eq_(report.message, 'spammy')
eq_(report.reporter.email, 'regular@mozilla.com')

Просмотреть файл

@ -23,7 +23,8 @@ from amo.decorators import (json_view, login_required, permission_required,
write)
from amo.forms import AbuseForm
from amo.urlresolvers import reverse
from amo.utils import send_mail, send_abuse_report
from amo.utils import send_mail
from abuse.models import send_abuse_report
from addons.models import Addon
from access import acl
from bandwagon.models import Collection
@ -564,8 +565,7 @@ def report_abuse(request, user_id):
user = get_object_or_404(UserProfile, pk=user_id)
form = AbuseForm(request.POST or None, request=request)
if request.method == "POST" and form.is_valid():
url = reverse('users.profile', args=[user.pk])
send_abuse_report(request, user, url, form.cleaned_data['text'])
send_abuse_report(request, user, form.cleaned_data['text'])
messages.success(request, _('User reported.'))
else:
return jingo.render(request, 'users/report_abuse_full.html',

Просмотреть файл

@ -0,0 +1,16 @@
CREATE TABLE `abuse_reports` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`reporter_id` int(11) unsigned,
`ip_address` varchar(255) NOT NULL,
`addon_id` int(11) unsigned,
`user_id` int(11) unsigned,
`message` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
ALTER TABLE `abuse_reports` ADD CONSTRAINT `reporter_id_refs_id_12d88e23`
FOREIGN KEY (`reporter_id`) REFERENCES `users` (`id`);
ALTER TABLE `abuse_reports` ADD CONSTRAINT `user_id_refs_id_12d88e23`
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `abuse_reports` ADD CONSTRAINT `addon_id_refs_id_2b6ff2a7`
FOREIGN KEY (`addon_id`) REFERENCES `addons` (`id`);

Просмотреть файл

@ -310,6 +310,7 @@ ROOT_URLCONF = '%s.urls' % ROOT_PACKAGE
INSTALLED_APPS = (
'amo', # amo comes first so it always takes precedence.
'abuse',
'access',
'addons',
'api',