# HG changeset patch # User Brian Neal # Date 1322945201 0 # Node ID 09a9402e4a71d5d22dfd88181007a3d34a91d0c5 # Parent a5d11471d03163c8fdbcbced53eca206a739cbdf Added the rate_limit_clear management command, to delete IP addresses from the rate limit datastore. diff -r a5d11471d031 -r 09a9402e4a71 gpp/accounts/management/commands/rate_limit_clear.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/accounts/management/commands/rate_limit_clear.py Sat Dec 03 20:46:41 2011 +0000 @@ -0,0 +1,51 @@ +""" +The rate_limit_clear command is used to clear IP addresses out from our rate +limit protection database. + +""" +from optparse import make_option +import re + +from django.core.management.base import BaseCommand +import redis + +IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') + + +class Command(BaseCommand): + help = """Remove IP addresses from the rate limit protection datastore.""" + option_list = list(BaseCommand.option_list) + [ + make_option("--purge", action="store_true", + help="Purge all IP addresses"), + ] + + def handle(self, *args, **kwargs): + try: + con = redis.Redis() + + # get all rate-limit keys + keys = con.keys('rate-limit-*') + + # if purging, delete them all... + if kwargs['purge']: + if keys: + con.delete(*keys) + return + + # otherwise delete the ones the user asked for + ips = [] + for ip in args: + if IP_RE.match(ip): + key = 'rate-limit-%s' % ip + if key in keys: + ips.append(key) + else: + self.stdout.write('%s not found\n' % ip) + else: + self.stderr.write('invalid IP address %s\n' % ip) + + if ips: + con.delete(*ips) + + except redis.RedisError, e: + self.stderr.write('%s\n' % e)