Optionally render pages at runtime
This commit is contained in:
parent
babec7cc2d
commit
5044f6d822
3 changed files with 26 additions and 4 deletions
|
@ -2,6 +2,7 @@
|
||||||
title: Test
|
title: Test
|
||||||
tags:
|
tags:
|
||||||
- tag-1
|
- tag-1
|
||||||
|
runtime_render: false
|
||||||
---
|
---
|
||||||
|
|
||||||
# Heading
|
# Heading
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
import markdown
|
import markdown
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
from django.template import Template
|
from django.template import Template, Context
|
||||||
|
|
||||||
class Tag(models.Model):
|
class Tag(models.Model):
|
||||||
__yamdl__ = True
|
__yamdl__ = True
|
||||||
|
@ -19,6 +19,7 @@ class Page(models.Model):
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
|
|
||||||
raw_content = models.TextField()
|
raw_content = models.TextField()
|
||||||
|
runtime_render = models.BooleanField(default=False)
|
||||||
content = models.TextField()
|
content = models.TextField()
|
||||||
toc = models.JSONField()
|
toc = models.JSONField()
|
||||||
slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True)
|
slug = models.CharField(max_length=128, unique=True, db_index=True, default=None, null=True)
|
||||||
|
@ -35,12 +36,18 @@ class Page(models.Model):
|
||||||
data["raw_content"] = content
|
data["raw_content"] = content
|
||||||
|
|
||||||
data["content"] = md.convert(content)
|
data["content"] = md.convert(content)
|
||||||
|
|
||||||
data["toc"] = {
|
data["toc"] = {
|
||||||
"html": md.toc,
|
"html": md.toc,
|
||||||
"tokens": md.toc_tokens
|
"tokens": md.toc_tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = cls.objects.create(**data)
|
instance = cls(**data)
|
||||||
|
|
||||||
|
if not instance.runtime_render:
|
||||||
|
instance.content = instance.render_content()
|
||||||
|
|
||||||
|
instance.save()
|
||||||
|
|
||||||
if tags:
|
if tags:
|
||||||
instance.tags.set(tags)
|
instance.tags.set(tags)
|
||||||
|
@ -49,6 +56,20 @@ class Page(models.Model):
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def content_template(self):
|
def content_template(self):
|
||||||
|
if self.runtime_render:
|
||||||
|
return Template(self.content, name=self.slug)
|
||||||
|
|
||||||
if (cached_template := self._template_cache.get(self.slug)) is None:
|
if (cached_template := self._template_cache.get(self.slug)) is None:
|
||||||
cached_template = self._template_cache[self.slug] = Template(self.content, name=self.slug)
|
cached_template = self._template_cache[self.slug] = Template(self.content, name=self.slug)
|
||||||
return cached_template
|
return cached_template
|
||||||
|
|
||||||
|
def get_context(self):
|
||||||
|
return {
|
||||||
|
"page": self
|
||||||
|
}
|
||||||
|
|
||||||
|
def render_content(self, extra_context=None):
|
||||||
|
if extra_context is None:
|
||||||
|
extra_context = {}
|
||||||
|
|
||||||
|
return self.content_template.render(Context({**self.get_context(), **extra_context}))
|
||||||
|
|
|
@ -2,8 +2,8 @@ from .models import Page
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.db import connections
|
from django.db import connections
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from django.template import Context
|
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
|
|
||||||
def search(request):
|
def search(request):
|
||||||
|
@ -19,7 +19,7 @@ def content(request, slug):
|
||||||
|
|
||||||
return render(request, "content.html", {
|
return render(request, "content.html", {
|
||||||
"page": page,
|
"page": page,
|
||||||
"content": page.content_template.render(Context({"request": request, "page": page}))
|
"content": page.render_content({"request": request}) if page.runtime_render else mark_safe(page.content)
|
||||||
})
|
})
|
||||||
|
|
||||||
cached_content = cache_page(600, cache="mem")(content)
|
cached_content = cache_page(600, cache="mem")(content)
|
||||||
|
|
Loading…
Reference in a new issue