limit.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.core.management import BaseCommand, CommandError
  2. from desecapi.models import Domain, User
  3. class Command(BaseCommand):
  4. help = "Sets/updates limits for users and domains."
  5. def add_arguments(self, parser):
  6. parser.add_argument(
  7. "kind",
  8. help="Identifies which limit should be updated. Possible values: domains, ttl",
  9. )
  10. parser.add_argument(
  11. "id",
  12. help="Identifies the entity to be updated. Users are identified by email address; "
  13. "domains by their name.",
  14. )
  15. parser.add_argument("new_limit", help="New value for the limit.")
  16. def handle(self, *args, **options):
  17. if options["kind"] == "domains":
  18. try:
  19. user = User.objects.get(email=options["id"])
  20. except User.DoesNotExist:
  21. raise CommandError(
  22. f'User with email address "{options["id"]}" could not be found.'
  23. )
  24. user.limit_domains = options["new_limit"]
  25. user.save()
  26. print(
  27. f"Updated {user.email}: set max number of domains to {user.limit_domains}."
  28. )
  29. elif options["kind"] == "ttl":
  30. try:
  31. domain = Domain.objects.get(name=options["id"])
  32. except Domain.DoesNotExist:
  33. raise CommandError(
  34. f'Domain with name "{options["id"]}" could not be found.'
  35. )
  36. domain.minimum_ttl = options["new_limit"]
  37. domain.save()
  38. print(f"Updated {domain.name}: set minimum TTL to {domain.minimum_ttl}.")
  39. else:
  40. raise CommandError(f'Unknown limit "{options["kind"]}" specified.')