Mercurial > public > sg101
comparison accounts/management/commands/rate_limit_clear.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/accounts/management/commands/rate_limit_clear.py@6f5fff924877 |
children |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 The rate_limit_clear command is used to clear IP addresses out from our rate | |
3 limit protection database. | |
4 | |
5 """ | |
6 from optparse import make_option | |
7 import re | |
8 | |
9 from django.core.management.base import BaseCommand | |
10 import redis | |
11 | |
12 from core.services import get_redis_connection | |
13 | |
14 | |
15 IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') | |
16 | |
17 | |
18 class Command(BaseCommand): | |
19 help = """Remove IP addresses from the rate limit protection datastore.""" | |
20 option_list = list(BaseCommand.option_list) + [ | |
21 make_option("--purge", action="store_true", | |
22 help="Purge all IP addresses"), | |
23 ] | |
24 | |
25 def handle(self, *args, **kwargs): | |
26 try: | |
27 con = get_redis_connection() | |
28 | |
29 # get all rate-limit keys | |
30 keys = con.keys('rate-limit-*') | |
31 | |
32 # if purging, delete them all... | |
33 if kwargs['purge']: | |
34 if keys: | |
35 con.delete(*keys) | |
36 return | |
37 | |
38 # otherwise delete the ones the user asked for | |
39 ips = [] | |
40 for ip in args: | |
41 if IP_RE.match(ip): | |
42 key = 'rate-limit-%s' % ip | |
43 if key in keys: | |
44 ips.append(key) | |
45 else: | |
46 self.stdout.write('%s not found\n' % ip) | |
47 else: | |
48 self.stderr.write('invalid IP address %s\n' % ip) | |
49 | |
50 if ips: | |
51 con.delete(*ips) | |
52 | |
53 except redis.RedisError, e: | |
54 self.stderr.write('%s\n' % e) |