# Path: user_management/api/models.py
# class AuthToken(models.Model):
#     """
#     Model for auth tokens with added functionality of controlling
#     expiration time of tokens.
# 
#     Similar to DRF's model but with extra `expires` field.
# 
#     It also has FK (not OneToOne relation) to user as the user can have
#     many tokens (multiple devices) in order to token expiration to work.
#     """
#     key = models.CharField(max_length=40, primary_key=True)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='authtoken',
#         on_delete=models.CASCADE,
#     )
#     created = models.DateTimeField(default=timezone.now, editable=False)
#     expires = models.DateTimeField(default=update_expiry, editable=False)
# 
#     def __str__(self):
#         return self.key
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(AuthToken, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
#     def update_expiry(self, commit=True):
#         """Update token's expiration datetime on every auth action."""
#         self.expires = update_expiry(self.created)
#         if commit:
#             self.save()


from django.core.management.base import BaseCommand
from django.utils import timezone
from user_management.api.models import AuthToken



class Command(BaseCommand):
    help = "Remove expired auth tokens from the database."

    def handle(self, *args, **options):
        now = timezone.now()

# Path: user_management/api/models.py
# class AuthToken(models.Model):
#     """
#     Model for auth tokens with added functionality of controlling
#     expiration time of tokens.
# 
#     Similar to DRF's model but with extra `expires` field.
# 
#     It also has FK (not OneToOne relation) to user as the user can have
#     many tokens (multiple devices) in order to token expiration to work.
#     """
#     key = models.CharField(max_length=40, primary_key=True)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='authtoken',
#         on_delete=models.CASCADE,
#     )
#     created = models.DateTimeField(default=timezone.now, editable=False)
#     expires = models.DateTimeField(default=update_expiry, editable=False)
# 
#     def __str__(self):
#         return self.key
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(AuthToken, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
#     def update_expiry(self, commit=True):
#         """Update token's expiration datetime on every auth action."""
#         self.expires = update_expiry(self.created)
#         if commit:
#             self.save()


# Path: user_management/api/management/commands/remove_expired_tokens.py
from django.core.management.base import BaseCommand
from django.utils import timezone
from user_management.api.models import AuthToken




class Command(BaseCommand):
    help = "Remove expired auth tokens from the database."

    def handle(self, *args, **options):
        now = timezone.now()

        AuthToken.objects.filter(expires__lte=now).delete()