# Path: apps/images/models/db.py
# class Image(ImageAbstractModel):
#     description = models.CharField(max_length=255, blank=True)
#     is_cover = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.description
# 
#     class Meta(ImageAbstractModel.Meta):
#         db_table = "generic_image"
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     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


from django.contrib import admin
from django.contrib.contenttypes.admin import GenericStackedInline
from apps.images.models import Image
# from ajax_select import register, LookupChannel



class PostImageInlineAdmin(GenericStackedInline):
    model = Image
    fields = ["file", "description"]
    extra = 1


class PostAdmin(admin.ModelAdmin):
    list_display = (
        "title",
        "status",
        "tag_string",
        "created_at",
        "updated_at",
    )
    list_filter = ("status",)
    search_fields = ("title",)
    list_per_page = 30
    inlines = (PostImageInlineAdmin,)

    # form = PostForm

    def get_queryset(self, request):

# Path: apps/images/models/db.py
# class Image(ImageAbstractModel):
#     description = models.CharField(max_length=255, blank=True)
#     is_cover = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.description
# 
#     class Meta(ImageAbstractModel.Meta):
#         db_table = "generic_image"
# 
#     @cached_property
#     def thumb(self):
#         return self.resize_image()
# 
#     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


# Path: apps/blog/admin/post.py
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericStackedInline
from apps.images.models import Image

# from ajax_select import register, LookupChannel



class PostImageInlineAdmin(GenericStackedInline):
    model = Image
    fields = ["file", "description"]
    extra = 1


class PostAdmin(admin.ModelAdmin):
    list_display = (
        "title",
        "status",
        "tag_string",
        "created_at",
        "updated_at",
    )
    list_filter = ("status",)
    search_fields = ("title",)
    list_per_page = 30
    inlines = (PostImageInlineAdmin,)

    # form = PostForm

    def get_queryset(self, request):

        return super().get_queryset(request).prefetch_related("tags")