from wagtail.admin.views.generic import IndexView, CreateView, EditView, DeleteView
from django.urls import reverse_lazy

from home.models import ArticleCategory
from home.threadlocals import get_current_site


class ArticleCategoryIndexView(IndexView):
    model = ArticleCategory
    page_title = "Article Categories admin_views.py"
    add_url_name = "articlecategory_add"
    context_object_name = "categories"

    def get_queryset(self):
        site = get_current_site()
        return ArticleCategory.objects.filter(site=site)


class ArticleCategoryCreateView(CreateView):
    model = ArticleCategory
    fields = ["title", "slug"]
    page_title = "Add Article Category"
    add_url_name = "articlecategory_add"

    success_url = reverse_lazy("articlecategory_index")

    def form_valid(self, form):
        form.instance.site = get_current_site()
        return super().form_valid(form)


class ArticleCategoryEditView(EditView):
    model = ArticleCategory
    fields = ["title", "slug"]
    page_title = "Edit Article Category"
    success_url = reverse_lazy("articlecategory_index")


class ArticleCategoryDeleteView(DeleteView):
    model = ArticleCategory
    success_url = reverse_lazy("articlecategory_index")