limit.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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('kind',
  10. help='Identifies which limit should be updated. Possible values: domains, ttl')
  11. parser.add_argument('id',
  12. help='Identifies the entity to be updated. Users are identified by email address; '
  13. 'domains by their name.')
  14. parser.add_argument('new_limit', help='New value for the limit.')
  15. def handle(self, *args, **options):
  16. if options['kind'] == 'domains':
  17. try:
  18. user = User.objects.get(email=options['id'])
  19. except User.DoesNotExist:
  20. raise CommandError(f'User with email address "{options["id"]}" could not be found.')
  21. user.limit_domains = options['new_limit']
  22. user.save()
  23. print(f'Updated {user.email}: set max number of domains to {user.limit_domains}.')
  24. elif options['kind'] == 'ttl':
  25. try:
  26. domain = Domain.objects.get(name=options['id'])
  27. except Domain.DoesNotExist:
  28. raise CommandError(f'Domain with name "{options["id"]}" could not be found.')
  29. domain.minimum_ttl = options['new_limit']
  30. domain.save()
  31. print(f'Updated {domain.name}: set minimum TTL to {domain.minimum_ttl}.')
  32. else:
  33. raise CommandError(f'Unknown limit "{options["kind"]}" specified.')