# Path: apps/photos/models/db.py
# 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)
#     width = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
#     height = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
#     size = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
# 
#     uploaded_at = models.DateTimeField(
#         default=timezone.now, db_index=True, editable=False
#     )
# 
#     class Meta:
#         ordering = ["-exif__shot_time"]
# 
#     def __str__(self):
#         return f"{self.file.name} - {self.uploaded_at}"
# 
#     @property
#     def shape(self):
#         return self.width, self.height
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     @property
#     def camera(self):
#         return self.exif.camera
# 
#     @property
#     def lens(self):
#         return self.exif.lens
# 
#     def resize_image(self, size=128):
#         _img_url = f"/upload/{size}/{self.file.name}"
#         if not debug:
#             _img_url = f"https://image.jiaxin.im{_img_url}"
#         return _img_url
# 
#     def gallery_image(self):
#         return self.resize_image(1280)
# 
#     def thumbnail_image(self):
#         return self.resize_image(600)


from django.contrib import admin
from django.db.models import Count
from django.utils.html import format_html
from django.utils.translation import ugettext as _
from mptt.admin import DraggableMPTTAdmin
from apps.photos.models import Category, Photo
class CategoryAdmin(DraggableMPTTAdmin):
    list_display = ["tree_actions", "name", "photo_count"]
    list_display_links = ["name"]
    MPTT_ADMIN_LEVEL_INDENT = 20

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        return qs.annotate(_photo_count=Count("photo"))

    def photo_count(self, obj):
        return obj._photo_count

    photo_count.admin_order_field = "_photo_count"


class PhotoAdmin(admin.ModelAdmin):
    list_display = [
        "photo",
        "user",
        "shape",
        "size",
        "camera",
        "lens",
        "shot_time",
        "uploaded_at",
    ]
    readonly_fields = ["user", "thumbnail"]

    fieldsets = (

# Path: apps/photos/models/db.py
# 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)
#     width = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
#     height = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
#     size = models.PositiveIntegerField(
#         default=0, editable=False, db_index=True
#     )
# 
#     uploaded_at = models.DateTimeField(
#         default=timezone.now, db_index=True, editable=False
#     )
# 
#     class Meta:
#         ordering = ["-exif__shot_time"]
# 
#     def __str__(self):
#         return f"{self.file.name} - {self.uploaded_at}"
# 
#     @property
#     def shape(self):
#         return self.width, self.height
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     @property
#     def camera(self):
#         return self.exif.camera
# 
#     @property
#     def lens(self):
#         return self.exif.lens
# 
#     def resize_image(self, size=128):
#         _img_url = f"/upload/{size}/{self.file.name}"
#         if not debug:
#             _img_url = f"https://image.jiaxin.im{_img_url}"
#         return _img_url
# 
#     def gallery_image(self):
#         return self.resize_image(1280)
# 
#     def thumbnail_image(self):
#         return self.resize_image(600)


# Path: apps/photos/admin.py
from django.contrib import admin
from django.db.models import Count
from django.utils.html import format_html
from django.utils.translation import ugettext as _
from mptt.admin import DraggableMPTTAdmin
from apps.photos.models import Category, Photo

class CategoryAdmin(DraggableMPTTAdmin):
    list_display = ["tree_actions", "name", "photo_count"]
    list_display_links = ["name"]
    MPTT_ADMIN_LEVEL_INDENT = 20

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        return qs.annotate(_photo_count=Count("photo"))

    def photo_count(self, obj):
        return obj._photo_count

    photo_count.admin_order_field = "_photo_count"


class PhotoAdmin(admin.ModelAdmin):
    list_display = [
        "photo",
        "user",
        "shape",
        "size",
        "camera",
        "lens",
        "shot_time",
        "uploaded_at",
    ]
    readonly_fields = ["user", "thumbnail"]

    fieldsets = (

        (_("photo-info"), {"fields": ("user", "thumbnail", "file")}),