# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid


from base64 import b64decode
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.http import JsonResponse
from django.views import View
from .models import ApiKey
        if kwargs:
            data = {'message': 'Bad Request.'}
            return JsonResponse(data, status=400)

        response = self.authenticate_user(request)

        if not isinstance(response, User):
            return response

        try:
            api_key, created = ApiKey.objects.get_or_create(owner=response, revoked=False)
        except ValidationError:
            data = {'message': 'Unauthorized.'}
            return JsonResponse(data, status=401)

        data = {'api_key': api_key.token.hashid}
        return JsonResponse(data, status=201 if created else 200)

    def delete(self, request, **kwargs):
        if not kwargs:
            data = {'message': 'Bad Request.'}
            return JsonResponse(data, status=400)

        response = self.authenticate_user(request)

        if not isinstance(response, User):
            return response

        try:
            api_key = ApiKey.objects.get(token=kwargs['token'], owner=response)

# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid


# Path: app/api/views.py
from base64 import b64decode
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.http import JsonResponse
from django.views import View
from .models import ApiKey

        if kwargs:
            data = {'message': 'Bad Request.'}
            return JsonResponse(data, status=400)

        response = self.authenticate_user(request)

        if not isinstance(response, User):
            return response

        try:
            api_key, created = ApiKey.objects.get_or_create(owner=response, revoked=False)
        except ValidationError:
            data = {'message': 'Unauthorized.'}
            return JsonResponse(data, status=401)

        data = {'api_key': api_key.token.hashid}
        return JsonResponse(data, status=201 if created else 200)

    def delete(self, request, **kwargs):
        if not kwargs:
            data = {'message': 'Bad Request.'}
            return JsonResponse(data, status=400)

        response = self.authenticate_user(request)

        if not isinstance(response, User):
            return response

        try:
            api_key = ApiKey.objects.get(token=kwargs['token'], owner=response)

        except (ApiKey.DoesNotExist, TypeError):