Merge remote branch 'chenba/574276-nickname-blacklist'

This commit is contained in:
Wil Clouser 2010-08-08 14:40:52 -07:00
Родитель 1107aafc1e 2531ea6a8b
Коммит 4d713534fc
5 изменённых файлов: 21 добавлений и 8 удалений

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

@ -48,10 +48,17 @@ class BlacklistedNicknameAdmin(admin.ModelAdmin):
inserted = 0
duplicates = 0
for n in form.cleaned_data['nicknames'].splitlines():
# check with teh cache
if BlacklistedNickname.blocked(n):
duplicates += 1
continue
n = n.decode().lower().encode('utf-8')
try:
BlacklistedNickname.objects.create(nickname=n)
inserted += 1
except IntegrityError:
# although unlikely, someone else could have added
# the nickname.
# note: unless we manage the transactions manually,
# we do lose a primary id here
duplicates += 1
@ -59,6 +66,7 @@ class BlacklistedNicknameAdmin(admin.ModelAdmin):
if duplicates:
msg += ' %s duplicates were ignored.' % (duplicates)
messages.success(request, msg)
form = forms.BlacklistedNicknameAddForm()
# Default django admin change list view does not print messages
# no redirect for now
# return http.HttpResponseRedirect(reverse(

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

@ -150,7 +150,7 @@
"pk": 1,
"model": "users.blacklistednickname",
"fields": {
"nickname": "IE6Fan",
"nickname": "ie6fan",
"modified": "2010-07-21 23:32:05",
"created": "2010-07-21 23:32:05"
}

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

@ -176,7 +176,8 @@ class BlacklistedNicknameAddForm(forms.Form):
if 'nicknames' in data:
data['nicknames'] = os.linesep.join(
[s for s in data['nicknames'].splitlines() if s])
[s.strip() for s in data['nicknames'].splitlines()
if s.strip()])
if 'nicknames' not in data or data['nicknames'] == '':
msg = 'Please enter at least one nickname to blacklist.'
self._errors['nicknames'] = ErrorList([msg])

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

@ -11,6 +11,7 @@ from django.core.mail import send_mail
from django.db import models
from django.template import Context, loader
import caching.base as caching
import commonware.log
from tower import ugettext as _
@ -202,10 +203,12 @@ class BlacklistedNickname(amo.models.ModelBase):
@classmethod
def blocked(cls, nick):
"""Check to see if a nickname is in the blacklist."""
# Could also cache the entire blacklist and simply check if the
# nickname is in the list here. @TODO?
return cls.uncached.only('nickname').filter(nickname=nick).exists()
"""Check to see if a nickname is in the (cached) blacklist."""
nick = nick.decode().lower().encode('utf-8')
qs = cls.objects.all()
f = lambda: dict(qs.values_list('nickname', 'id'))
blacklist = caching.cached_with(qs, f, 'blocked')
return nick in blacklist
class PersonaAuthor(unicode):

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

@ -309,8 +309,9 @@ class TestBlacklistedNicknameAdminAddForm(UserFormBase):
def test_add(self):
self.client.login(username='testo@example.com', password='foo')
url = reverse('admin:users_blacklistednickname_add')
data = {'nicknames': "IE6Fan\nfubar\n\n", }
data = {'nicknames': "IE6Fan\nFubar\n\n fubar \n", }
r = self.client.post(url, data)
msg = '1 new nicknames added to the blacklist. '
msg += '1 duplicates were ignored.'
msg += '2 duplicates were ignored.'
self.assertContains(r, msg)
self.assertNotContains(r, 'fubar')