diff options
Diffstat (limited to '')
| -rw-r--r-- | pydis_site/apps/content/views/article.py | 43 | ||||
| -rw-r--r-- | pydis_site/apps/content/views/articles.py | 18 | ||||
| -rw-r--r-- | pydis_site/apps/content/views/category.py | 23 | 
3 files changed, 40 insertions, 44 deletions
| diff --git a/pydis_site/apps/content/views/article.py b/pydis_site/apps/content/views/article.py index 42b92cee..90688c6f 100644 --- a/pydis_site/apps/content/views/article.py +++ b/pydis_site/apps/content/views/article.py @@ -4,21 +4,21 @@ from django.core.handlers.wsgi import WSGIRequest  from django.http import HttpResponse  from django.shortcuts import render  from django.views import View +from django.views.generic import TemplateView  from pydis_site.apps.content.utils import get_article, get_category, get_github_information -class ArticleView(View): +class ArticleView(TemplateView):      """Shows specific guide page.""" -    def get( -            self, -            request: WSGIRequest, -            article: str, -            category: Optional[str] = None -    ) -> HttpResponse: -        """Collect guide content and display it. When guide don't exist, return 404.""" -        article_result = get_article(article, category) +    template_name = "content/article.html" + +    def get_context_data(self, **kwargs): +        """Add custom context info about article.""" +        context = super().get_context_data(**kwargs) +        category = self.kwargs.get("category") +        article_result = get_article(self.kwargs["article"], category)          if category is not None:              category_data = get_category(category) @@ -26,18 +26,13 @@ class ArticleView(View):          else:              category_data = {"name": None, "raw_name": None} -        return render( -            request, -            "content/article.html", -            { -                "article": article_result, -                "category_data": category_data, -                "relevant_links": { -                    link: value for link, value in zip( -                        article_result["metadata"].get("relevant_links", "").split(","), -                        article_result["metadata"].get("relevant_link_values", "").split(",") -                    ) if link != "" and value != "" -                }, -                "github_data": get_github_information(article, category), -            } -        ) +        context["article"] = article_result +        context["category_data"] = category_data +        context["relevant_links"] = { +            link: value for link, value in zip( +                article_result["metadata"].get("relevant_links", "").split(","), +                article_result["metadata"].get("relevant_link_values", "").split(",") +            ) if link != "" and value != "" +        } +        context["github_data"] = get_github_information(self.kwargs["article"], category) +        return context diff --git a/pydis_site/apps/content/views/articles.py b/pydis_site/apps/content/views/articles.py index cce601e1..d66425c3 100644 --- a/pydis_site/apps/content/views/articles.py +++ b/pydis_site/apps/content/views/articles.py @@ -2,17 +2,19 @@ from django.core.handlers.wsgi import WSGIRequest  from django.http import HttpResponse  from django.shortcuts import render  from django.views import View +from django.views.generic import TemplateView  from pydis_site.apps.content.utils import get_articles, get_categories -class ArticlesView(View): +class ArticlesView(TemplateView):      """Shows all content and categories.""" -    def get(self, request: WSGIRequest) -> HttpResponse: -        """Shows all content and categories.""" -        return render( -            request, -            "content/articles.html", -            {"content": get_articles(), "categories": get_categories()} -        ) +    template_name = "content/articles.html" + +    def get_context_data(self, **kwargs): +        """Add articles and categories data to template context.""" +        context = super().get_context_data(**kwargs) +        context["content"] = get_articles() +        context["categories"] = get_categories() +        return context diff --git a/pydis_site/apps/content/views/category.py b/pydis_site/apps/content/views/category.py index 9d2a978e..9c1da99c 100644 --- a/pydis_site/apps/content/views/category.py +++ b/pydis_site/apps/content/views/category.py @@ -2,21 +2,20 @@ from django.core.handlers.wsgi import WSGIRequest  from django.http import HttpResponse  from django.shortcuts import render  from django.views import View +from django.views.generic import TemplateView  from pydis_site.apps.content.utils import get_articles, get_category -class CategoryView(View): +class CategoryView(TemplateView):      """Handles content category page.""" -    def get(self, request: WSGIRequest, category: str) -> HttpResponse: -        """Handles page that displays category content.""" -        return render( -            request, -            "content/category.html", -            { -                "category_info": get_category(category), -                "content": get_articles(category), -                "category_name": category -            } -        ) +    template_name = "content/category.html" + +    def get_context_data(self, **kwargs): +        """Add category data to context.""" +        context = super().get_context_data(**kwargs) +        context["category_info"] = get_category(self.kwargs["category"]) +        context["content"] = get_articles(self.kwargs["category"]) +        context["category_name"] = self.kwargs["category"] +        return context | 
