from django.contrib import admin
from django.urls import path
from django.utils.translation import gettext as _
from django.http.response import HttpResponseRedirect
from .models import *

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    """
    The admin model for Category
    """

    list_display = ["name", "slug"]

@admin.action(description=_("Regenerate traits"))
def regenerate(modeladmin, request, queryset):
    for obj in queryset:
        obj.regenerate()


@admin.register(BlogPost)
class BlogPostAdmin(admin.ModelAdmin):
    """
    The admin model for BlogPost
    """
    list_display = ["title_en", "subtitle_en", "title_de", "subtitle_de", "date", "category", "slug", "suburl", "public"]
    date_hierarchy = "date"
    ordering = ['title_de', 'title_en']
    actions = [regenerate]

    change_list_template = "admin/blogpost.html"

    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path('sync/', self.sync_with_fs),
        ]
        return my_urls + urls

    def sync_with_fs(self, request):
        BlogPost.sync_all()
        return HttpResponseRedirect("../")
