# Path: apps/images/handlers.py
# def hexdigest_filename(instance, filename):
#     ext = filename.split(".")[-1]
#     data = instance.file.read()
#     hex = md5(data).hexdigest()
#     return smart_str(f"img/{hex}.{ext}")
# 
# Path: apps/ext/models.py
# class Category(MPTTModel):
#     name = models.CharField(max_length=50, unique=True)
#     parent = TreeForeignKey(
#         "self",
#         on_delete=models.CASCADE,
#         null=True,
#         blank=True,
#         related_name="children",
#     )
# 
#     class Meta:
#         abstract = True
# 
#     def __str__(self):
#         return self.name


import json
from django.conf import settings
from django.utils.functional import cached_property
from django.db.models import JSONField
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext as _
from apps.images.handlers import hexdigest_filename
from apps.ext.models import Category as BaseCategory

class Category(BaseCategory):
    image = models.ImageField(
        upload_to=hexdigest_filename, null=True, blank=True
    )
    width = models.PositiveIntegerField(default=0, editable=False)
    height = models.PositiveIntegerField(default=0, editable=False)
    description = models.TextField(blank=True, default="")

    class Meta:
        verbose_name = _("category")
        verbose_name_plural = _("category")

    class MPTTMeta:
        order_insertion_by = ["name"]


class Photo(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name="photos",
        on_delete=models.CASCADE,
    )
    # title = models.CharField(_("title"), max_length=255)
    description = models.TextField(blank=True, default="")

    category = models.ForeignKey(
        "Category", on_delete=models.SET_NULL, null=True, blank=True
    )


# Path: apps/images/handlers.py
# def hexdigest_filename(instance, filename):
#     ext = filename.split(".")[-1]
#     data = instance.file.read()
#     hex = md5(data).hexdigest()
#     return smart_str(f"img/{hex}.{ext}")
# 
# Path: apps/ext/models.py
# class Category(MPTTModel):
#     name = models.CharField(max_length=50, unique=True)
#     parent = TreeForeignKey(
#         "self",
#         on_delete=models.CASCADE,
#         null=True,
#         blank=True,
#         related_name="children",
#     )
# 
#     class Meta:
#         abstract = True
# 
#     def __str__(self):
#         return self.name


# Path: apps/photos/models/db.py
import json
from django.conf import settings
from django.utils.functional import cached_property
from django.db.models import JSONField
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext as _
from apps.images.handlers import hexdigest_filename
from apps.ext.models import Category as BaseCategory


class Category(BaseCategory):
    image = models.ImageField(
        upload_to=hexdigest_filename, null=True, blank=True
    )
    width = models.PositiveIntegerField(default=0, editable=False)
    height = models.PositiveIntegerField(default=0, editable=False)
    description = models.TextField(blank=True, default="")

    class Meta:
        verbose_name = _("category")
        verbose_name_plural = _("category")

    class MPTTMeta:
        order_insertion_by = ["name"]


class Photo(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name="photos",
        on_delete=models.CASCADE,
    )
    # title = models.CharField(_("title"), max_length=255)
    description = models.TextField(blank=True, default="")

    category = models.ForeignKey(
        "Category", on_delete=models.SET_NULL, null=True, blank=True
    )


    file = models.ImageField(upload_to=hexdigest_filename)