| 1 | import sys |
| 2 | |
| 3 | from django.core.management.base import BaseCommand |
| 4 | from django.utils import timezone |
| 5 | |
| 6 | |
| 7 | class Command(BaseCommand): |
| 8 | help = "Clean up expired captcha hashkeys." |
| 9 | |
| 10 | def handle(self, **options): |
| 11 | from captcha.models import CaptchaStore |
| 12 | |
| 13 | verbose = int(options.get("verbosity")) |
| 14 | expired_keys = CaptchaStore.objects.filter( |
| 15 | expiration__lte=timezone.now() |
| 16 | ).count() |
| 17 | if verbose >= 1: |
| 18 | print("Currently %d expired hashkeys" % expired_keys) |
| 19 | try: |
| 20 | CaptchaStore.remove_expired() |
| 21 | except Exception: |
| 22 | if verbose >= 1: |
| 23 | print("Unable to delete expired hashkeys.") |
| 24 | sys.exit(1) |
| 25 | if verbose >= 1: |
| 26 | if expired_keys > 0: |
| 27 | print("%d expired hashkeys removed." % expired_keys) |
| 28 | else: |
| 29 | print("No keys to remove.") |