annotate 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 |
|
rev |
line source |
bgneal@506
|
1 """
|
bgneal@506
|
2 The rate_limit_clear command is used to clear IP addresses out from our rate
|
bgneal@506
|
3 limit protection database.
|
bgneal@506
|
4
|
bgneal@506
|
5 """
|
bgneal@506
|
6 from optparse import make_option
|
bgneal@506
|
7 import re
|
bgneal@506
|
8
|
bgneal@506
|
9 from django.core.management.base import BaseCommand
|
bgneal@506
|
10 import redis
|
bgneal@506
|
11
|
bgneal@508
|
12 from core.services import get_redis_connection
|
bgneal@508
|
13
|
bgneal@508
|
14
|
bgneal@506
|
15 IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
|
bgneal@506
|
16
|
bgneal@506
|
17
|
bgneal@506
|
18 class Command(BaseCommand):
|
bgneal@506
|
19 help = """Remove IP addresses from the rate limit protection datastore."""
|
bgneal@506
|
20 option_list = list(BaseCommand.option_list) + [
|
bgneal@506
|
21 make_option("--purge", action="store_true",
|
bgneal@506
|
22 help="Purge all IP addresses"),
|
bgneal@506
|
23 ]
|
bgneal@506
|
24
|
bgneal@506
|
25 def handle(self, *args, **kwargs):
|
bgneal@506
|
26 try:
|
bgneal@508
|
27 con = get_redis_connection()
|
bgneal@506
|
28
|
bgneal@506
|
29 # get all rate-limit keys
|
bgneal@506
|
30 keys = con.keys('rate-limit-*')
|
bgneal@506
|
31
|
bgneal@506
|
32 # if purging, delete them all...
|
bgneal@506
|
33 if kwargs['purge']:
|
bgneal@506
|
34 if keys:
|
bgneal@506
|
35 con.delete(*keys)
|
bgneal@506
|
36 return
|
bgneal@506
|
37
|
bgneal@506
|
38 # otherwise delete the ones the user asked for
|
bgneal@506
|
39 ips = []
|
bgneal@506
|
40 for ip in args:
|
bgneal@506
|
41 if IP_RE.match(ip):
|
bgneal@506
|
42 key = 'rate-limit-%s' % ip
|
bgneal@506
|
43 if key in keys:
|
bgneal@506
|
44 ips.append(key)
|
bgneal@506
|
45 else:
|
bgneal@506
|
46 self.stdout.write('%s not found\n' % ip)
|
bgneal@506
|
47 else:
|
bgneal@506
|
48 self.stderr.write('invalid IP address %s\n' % ip)
|
bgneal@506
|
49
|
bgneal@506
|
50 if ips:
|
bgneal@506
|
51 con.delete(*ips)
|
bgneal@506
|
52
|
bgneal@506
|
53 except redis.RedisError, e:
|
bgneal@506
|
54 self.stderr.write('%s\n' % e)
|