Skip to content

Commit

Permalink
fix: add support for static (md) cms pages
Browse files Browse the repository at this point in the history
used in changelog
  • Loading branch information
ohrstrom committed Mar 21, 2023
1 parent 73c3c61 commit 6530870
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
16 changes: 16 additions & 0 deletions core/cms/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PageSerializer(
read_only=True,
)
lead = serializers.CharField(
source="render_lead",
read_only=True,
)
sections = SectionSerializer(
Expand All @@ -54,3 +55,18 @@ class Meta:
"lead",
"sections",
]


class StaticPageSerializer(
serializers.Serializer,
):
title = serializers.CharField(
read_only=True,
)
lead = serializers.CharField(
read_only=True,
)
sections = serializers.JSONField(
# many=True,
read_only=True,
)
14 changes: 13 additions & 1 deletion core/cms/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.views import APIView

from ..models import Page
from ..static_page import StaticPage, StaticPageDoesNotExist
from . import serializers


Expand All @@ -25,4 +26,15 @@ def get(request, path, *args, **kwargs):
serializer.data,
)
except Page.DoesNotExist as e:
raise Http404(e) from e
pass

try:
page = StaticPage(path)
serializer = serializers.StaticPageSerializer(page)
return Response(
serializer.data,
)
except StaticPageDoesNotExist as e:
pass

raise Http404("Page does not exist")
38 changes: 38 additions & 0 deletions core/cms/static_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.conf import settings

import markdown

PAGES_DIR = settings.PROJECT_ROOT / "content" / "pages"


class StaticPageDoesNotExist(Exception):
pass


class StaticPage:
title = ""
lead = ""
body = ""

def __init__(self, path):
self.filename = f"{path}.md"
self.abs_path = PAGES_DIR / self.filename
if not self.abs_path.is_file():
raise StaticPageDoesNotExist(f"file does not exist: {self.filename}")

self.load_markdown()

def load_markdown(self):
with open(self.abs_path) as f:
text = f.read()
md = markdown.Markdown(extensions=["meta"])
self.body = md.convert(text)
self.title = "".join(md.Meta.get("title", []))
self.lead = "".join(md.Meta.get("lead", []))

def sections(self):
return [
{
"body": self.body,
}
]
2 changes: 1 addition & 1 deletion core/settings/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]

MIDDLEWARE += [
"querycount.middleware.QueryCountMiddleware",
# "querycount.middleware.QueryCountMiddleware",
]

LOGGING = {
Expand Down
4 changes: 4 additions & 0 deletions src/style/elements/cms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ https://python-markdown.github.io/extensions/footnotes/
list-style-position: outside;
margin-left: 1rem;
}

.footnote-backref {
text-decoration: none;
}
}

@mixin table {
Expand Down

0 comments on commit 6530870

Please sign in to comment.