chores.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import time
  2. from django.conf import settings
  3. from django.core.management import BaseCommand
  4. from django.utils import timezone
  5. from desecapi import models, serializers
  6. from desecapi.pdns_change_tracker import PDNSChangeTracker
  7. class Command(BaseCommand):
  8. @staticmethod
  9. def delete_expired_captchas():
  10. models.Captcha.objects.filter(created__lt=timezone.now() - settings.CAPTCHA_VALIDITY_PERIOD).delete()
  11. @staticmethod
  12. def delete_never_activated_users():
  13. # delete inactive users whose activation link expired and who never logged in
  14. # (this will not delete users who have used their account and were later disabled)
  15. models.User.objects.filter(is_active=False, last_login__exact=None,
  16. created__lt=timezone.now() - settings.VALIDITY_PERIOD_VERIFICATION_SIGNATURE).delete()
  17. @staticmethod
  18. def update_healthcheck_timestamp():
  19. try:
  20. domain = models.Domain.objects.get(name='internal-timestamp.desec.test')
  21. except models.Domain.DoesNotExist:
  22. # Fail silently. If external alerting is configured, it will catch the problem; otherwise, we don't need it.
  23. return
  24. instances = domain.rrset_set.filter(subname='', type='TXT').all()
  25. timestamp = int(time.time())
  26. data = [{
  27. 'subname': '',
  28. 'type': 'TXT',
  29. 'ttl': '3600',
  30. 'records': [f'"{timestamp}"']
  31. }]
  32. serializer = serializers.RRsetSerializer(instances, domain=domain, data=data, many=True, partial=True)
  33. serializer.is_valid(raise_exception=True)
  34. with PDNSChangeTracker():
  35. serializer.save()
  36. def handle(self, *args, **kwargs):
  37. self.delete_expired_captchas()
  38. self.delete_never_activated_users()
  39. self.update_healthcheck_timestamp()