aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/guides/views/guide.py
blob: cb30a5250cd9a9fd33794ecfdf1bbd0e91b2d2d8 (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
52
53
54
55
56
import os
from datetime import datetime
from typing import Optional

import yaml
from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.views import View
from markdown import Markdown


class GuideView(View):
    """Shows specific guide page."""

    def get(self, request: WSGIRequest, guide: str, category: Optional[str] = None) -> HttpResponse:
        """Collect guide content and display it. When guide don't exist, return 404."""
        if category is None:
            path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", f"{guide}.md")
            category_name = None
        else:
            dir_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
            path = os.path.join(dir_path, f"{guide}.md")
            with open(os.path.join(dir_path, "_info.yml")) as f:
                category_name = yaml.load(f.read())["name"]

        if not os.path.exists(path) or not os.path.isfile(path):
            raise Http404(f"Guide not found")

        md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
        with open(path) as f:
            html = md.convert(f.read())
            f.close()

        category_data = {
            "title": category_name,
            "name": category,
        }

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