# Path: apps/images/handlers.py
# class UUIDFilename(object):
#     def __init__(self, sub_path):
#         self.sub_path = sub_path
# 
#     def __call__(self, instance, filename):
#         ext = filename.split(".")[-1]
#         uuid_filename = uuid.uuid5(uuid.NAMESPACE_DNS, filename)
#         filename = f"{self.sub_path}{uuid_filename.hex}.{ext}"
#         return filename


from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.utils import timezone
from django.conf import settings
from apps.images.handlers import UUIDFilename

# Create your models here.


upload_image_dir = getattr(settings, "IMAGE_UPLOAD_DIR", "images/")
upload_dir = UUIDFilename(upload_image_dir)


class ImageAbstractModel(models.Model):
    file = models.ImageField(upload_to=upload_dir)
    width = models.IntegerField(default=0, editable=False)
    height = models.IntegerField(default=0, editable=False)

    # Content-object field
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE, null=True
    )

# Path: apps/images/handlers.py
# class UUIDFilename(object):
#     def __init__(self, sub_path):
#         self.sub_path = sub_path
# 
#     def __call__(self, instance, filename):
#         ext = filename.split(".")[-1]
#         uuid_filename = uuid.uuid5(uuid.NAMESPACE_DNS, filename)
#         filename = f"{self.sub_path}{uuid_filename.hex}.{ext}"
#         return filename


# Path: apps/images/models/abstracts.py
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.utils import timezone
from django.conf import settings
from apps.images.handlers import UUIDFilename


# Create your models here.


upload_image_dir = getattr(settings, "IMAGE_UPLOAD_DIR", "images/")
upload_dir = UUIDFilename(upload_image_dir)


class ImageAbstractModel(models.Model):
    file = models.ImageField(upload_to=upload_dir)
    width = models.IntegerField(default=0, editable=False)
    height = models.IntegerField(default=0, editable=False)

    # Content-object field
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE, null=True
    )

    object_id = models.PositiveIntegerField(db_index=True, null=True)