From ff90e8422b99df98af66549107c7f4d56d0b2451 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 24 Mar 2024 21:59:25 +0000 Subject: [PATCH] Render markdown during page import --- yamdl_playground/core/models.py | 7 +++++++ yamdl_playground/core/views.py | 13 +------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/yamdl_playground/core/models.py b/yamdl_playground/core/models.py index 1c951e4..b4ea30d 100644 --- a/yamdl_playground/core/models.py +++ b/yamdl_playground/core/models.py @@ -1,4 +1,5 @@ from django.db import models +import markdown class Tag(models.Model): __yamdl__ = True @@ -13,6 +14,7 @@ class Page(models.Model): title = models.CharField(max_length=255) + raw_content = models.TextField() content = models.TextField() slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True) @@ -21,6 +23,11 @@ class Page(models.Model): @classmethod def from_yaml(cls, **data): tags = data.pop("tags", None) + + content = data.pop("content") + data["raw_content"] = content + data["content"] = markdown.markdown(content) + instance = cls.objects.create(**data) if tags: diff --git a/yamdl_playground/core/views.py b/yamdl_playground/core/views.py index f74416f..5bbda87 100644 --- a/yamdl_playground/core/views.py +++ b/yamdl_playground/core/views.py @@ -2,12 +2,7 @@ from .models import Page from django.http import HttpResponse from django.db import connections from django.shortcuts import get_object_or_404 -import markdown -from django.core.cache import caches -from threading import Lock -md = markdown.Markdown() -md_lock = Lock() def search(request): with connections["default"].cursor() as cursor: @@ -20,10 +15,4 @@ def search(request): def content(request, slug): page = get_object_or_404(Page, slug=slug) - if (cached_content := caches["default"].get(f"page-content-{slug}")) is None: - with md_lock: - cached_content = md.convert(page.content) - md.reset() - caches["default"].set(f"page-content-{slug}", cached_content) - - return HttpResponse(cached_content, content_type="text/html") + return HttpResponse(page.content, content_type="text/html")