| 1 | from django.core.management.base import BaseCommand |
| 2 | from django.db import transaction |
| 3 | |
| 4 | from captcha.models import CaptchaStore |
| 5 | |
| 6 | |
| 7 | class Command(BaseCommand): |
| 8 | help = "Create a pool of random captchas." |
| 9 | |
| 10 | def add_arguments(self, parser): |
| 11 | parser.add_argument( |
| 12 | "--pool-size", |
| 13 | type=int, |
| 14 | default=1000, |
| 15 | help="Number of new captchas to create, default=1000", |
| 16 | ) |
| 17 | parser.add_argument( |
| 18 | "--cleanup-expired", |
| 19 | action="store_true", |
| 20 | default=True, |
| 21 | help="Cleanup expired captchas after creating new ones", |
| 22 | ) |
| 23 | |
| 24 | @transaction.atomic() |
| 25 | def handle(self, **options): |
| 26 | verbose = int(options.get("verbosity")) |
| 27 | count = options.get("pool_size") |
| 28 | CaptchaStore.create_pool(count) |
| 29 | verbose and self.stdout.write("Created %d new captchas\n" % count) |
| 30 | options.get("cleanup_expired") and CaptchaStore.remove_expired() |
| 31 | options.get("cleanup_expired") and verbose and self.stdout.write( |
| 32 | "Expired captchas cleaned up\n" |
| 33 | ) |