from articles.models import Article

from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from tasks.constants import IntroPopupType
from tasks.views import IntroPopupContextMixin
from users.decorators import apprentice_login_required


@apprentice_login_required
class ArticleListView(ListView, IntroPopupContextMixin):
    """Widok reprezentujący bazę wiedzy - listę artykułów"""

    template_name = 'articles/article_list.html'
    context_object_name = 'active_articles'
    intro_popup_type = IntroPopupType.ARTICLES

    def get_queryset(self):
        return Article.objects.active()


@apprentice_login_required
class ArticleDetailView(DetailView):
    """Widok reprezentujący bazę wiedzy - podedynczy artykuł"""

    template_name = 'articles/article.html'
    pk_url_kwarg = 'article_id'
    context_object_name = 'selected_article'

    def get_queryset(self):
        return Article.objects.active()

    def get_context_data(self, **kwargs):
        context = super(ArticleDetailView, self).get_context_data(**kwargs)
        context['random_articles_from_category'] = Article.objects.active().random_from_category(self.object, 3)
        return context
