view accounts/management/commands/rate_limit_clear.py @ 645:99f7917702ca

Fix 081a88b3bfc8, javascript resize of forum images. Commit 081a88b3bfc8 broke those pages that loaded forums.js but did not load the imagesLoaded jQuery extension. Now we have arranged it so that only the forums topic view loads imagesLoaded and put the resizing javascript inline.
author Brian Neal <bgneal@gmail.com>
date Mon, 11 Mar 2013 15:30:25 -0500
parents ee87ea74d46b
children
line wrap: on
line source
"""
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

from core.services import get_redis_connection


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 = get_redis_connection()

            # 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)