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

Commit

Permalink
bug 113260 - use django-cacheback for ip bans
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Mar 18, 2015
1 parent 49def1e commit 3bce0a2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -148,3 +148,6 @@
[submodule "vendor/src/django-ratelimit"]
path = vendor/src/django-ratelimit
url = git://github.com/jsocol/django-ratelimit.git
[submodule "vendor/src/django-cacheback"]
path = vendor/src/django-cachebackk
url = https://github.com/codeinthehole/django-cacheback.git
19 changes: 19 additions & 0 deletions kuma/core/jobs.py
Expand Up @@ -18,3 +18,22 @@ def hash(self, value):
if isinstance(value, tuple):
value = tuple(to_bytestring(v) for v in value)
return hashlib.md5(six.b(':').join(value)).hexdigest()


class IPBanJob(Job):
lifetime = 60 * 60 * 3
refresh_timeout = 60

def fetch(self, ip):
from .models import IPBan
if IPBan.objects.active(ip=ip).exists():
return "0/s"
return "60/m"

def key(self, ip):
# override the default way to make sure we handle unicode,
# bytestring and integer versions of the pk the same
return 'kuma:core:ipban:%s' % ip

def empty(self):
return []
4 changes: 2 additions & 2 deletions kuma/core/models.py
@@ -1,11 +1,10 @@
from datetime import datetime

from django.db import models
from django.utils import timezone

from south.modelsinspector import add_introspection_rules

from .managers import IPBanManager
from .jobs import IPBanJob


class ModelBase(models.Model):
Expand Down Expand Up @@ -49,6 +48,7 @@ class IPBan(models.Model):
def delete(self, *args, **kwargs):
self.deleted = timezone.now()
self.save()
IPBanJob.delete(self.ip)

def __unicode__(self):
return u'%s banned on %s' % (self.ip, self.created)
Expand Down
7 changes: 3 additions & 4 deletions kuma/core/utils.py
Expand Up @@ -21,7 +21,7 @@

from kuma.actioncounters.utils import get_ip
from .cache import memcache
from .models import IPBan
from .jobs import IPBanJob


log = commonware.log.getLogger('kuma.core.utils')
Expand Down Expand Up @@ -289,6 +289,5 @@ def chord_flow(pre_task, tasks, post_task):


def limit_banned_ip_to_0(group, request):
if IPBan.objects.active(ip=get_ip(request)).exists():
return "0/s"
return "60/m"
ip = get_ip(request)
return IPBanJob().get(ip)
3 changes: 3 additions & 0 deletions kuma/wiki/tests/test_views.py
Expand Up @@ -566,17 +566,20 @@ def test_banned_ip_cant_get_edit(self):
self.client.login(username='testuser', password='testpass')
response = self.client.get(self.edit_url, REMOTE_ADDR=self.ip)
eq_(403, response.status_code)
cache.clear()

def test_banned_ip_cant_post_edit(self):
self.client.login(username='testuser', password='testpass')
response = self.client.get(self.edit_url, REMOTE_ADDR=self.ip)
eq_(403, response.status_code)
cache.clear()

def test_banned_ip_can_still_get_articles(self):
response = self.client.get(self.doc.get_absolute_url(),
REMOTE_ADDR=self.ip
)
eq_(200, response.status_code)
cache.clear()

class KumascriptIntegrationTests(UserTestCase, WikiTestCase):
"""
Expand Down
2 changes: 1 addition & 1 deletion settings.py
Expand Up @@ -520,6 +520,7 @@ def lazy_language_deki_map():
'taggit',
'dbgettext',
'honeypot',
'cacheback',

'kuma.dashboards',
'statici18n',
Expand Down Expand Up @@ -1215,7 +1216,6 @@ def JINJA_CONFIG():
'handlers': ['console'],
'level': logging.ERROR,
}

},
}

Expand Down
1 change: 1 addition & 0 deletions vendor/kuma.pth
Expand Up @@ -98,3 +98,4 @@ src/django-honeypot
src/bitly-api-python
src/django-cacheback
src/django-ratelimit
src/django-cacheback

0 comments on commit 3bce0a2

Please sign in to comment.