Skip to content

Commit

Permalink
Merge remote branch 'chenba/574276-nickname-blacklist'
Browse files Browse the repository at this point in the history
  • Loading branch information
clouserw committed Aug 8, 2010
2 parents 1107aaf + 2531ea6 commit 4d71353
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
8 changes: 8 additions & 0 deletions apps/users/admin.py
Expand Up @@ -48,17 +48,25 @@ def add_view(self, request, form_url='', extra_context=None):
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
msg = '%s new nicknames added to the blacklist.' % (inserted)
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(
Expand Down
2 changes: 1 addition & 1 deletion apps/users/fixtures/users/test_backends.json
Expand Up @@ -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"
}
Expand Down
3 changes: 2 additions & 1 deletion apps/users/forms.py
Expand Up @@ -176,7 +176,8 @@ def clean(self):

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])
Expand Down
11 changes: 7 additions & 4 deletions apps/users/models.py
Expand Up @@ -11,6 +11,7 @@
from django.db import models
from django.template import Context, loader

import caching.base as caching
import commonware.log
from tower import ugettext as _

Expand Down Expand Up @@ -202,10 +203,12 @@ def __unicode__(self):

@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):
Expand Down
5 changes: 3 additions & 2 deletions apps/users/tests/test_forms.py
Expand Up @@ -309,8 +309,9 @@ def test_no_nicknames(self):
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')

0 comments on commit 4d71353

Please sign in to comment.