limit.py 1.8 KB

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