aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/views/article.py
blob: 344047198e5c2617e5172143c1896e933769f9e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from datetime import datetime
from pathlib import Path
from typing import Optional

from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View

from pydis_site.apps.content.utils import get_category, get_article


class ArticleView(View):
    """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)

        if category is not None:
            path = Path(
                settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content", category, f"{article}.md"
            )
        else:
            path = Path(
                settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content", f"{article}.md"
            )

        if category is not None:
            category_data = get_category(category)
            category_data["raw_name"] = category
        else:
            category_data = {"name": None, "raw_name": None}

        return render(
            request,
            "content/article.html",
            {
                "article": article_result,
                "last_modified": datetime.fromtimestamp(os.path.getmtime(path)).strftime("%dth %B %Y"),
                "category_data": category_data,
                "relevant_links": {
                    link: value for link, value in zip(
                        article_result["metadata"].get("relevantlinks", []),
                        article_result["metadata"].get("relevantlinkvalues", [])
                    )
                }
            }
        )