Allow for remote policy pages

This commit is contained in:
Andrew Godwin 2022-12-17 15:30:51 -07:00
parent d08324e159
commit 4d71da7ae1
2 changed files with 17 additions and 8 deletions

View file

@ -1,5 +1,6 @@
import markdown_it import markdown_it
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import redirect
from django.templatetags.static import static from django.templatetags.static import static
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
@ -79,13 +80,21 @@ class FlatPage(TemplateView):
config_option = None config_option = None
title = None title = None
def get_context_data(self): def get(self, request, *args, **kwargs):
if self.config_option is None: if self.config_option is None:
raise ValueError("No config option provided") raise ValueError("No config option provided")
# Get raw content self.content = getattr(Config.system, self.config_option)
content = getattr(Config.system, self.config_option) # If the content is a plain URL, then redirect to it instead
# Render it if (
html = markdown_it.MarkdownIt().render(content) "\n" not in self.content
and " " not in self.content
and "://" in self.content
):
return redirect(self.content)
return super().get(request, *args, **kwargs)
def get_context_data(self):
html = markdown_it.MarkdownIt().render(self.content)
return { return {
"title": self.title, "title": self.title,
"content": mark_safe(html), "content": mark_safe(html),

View file

@ -174,17 +174,17 @@ class PoliciesSettings(AdminSettingsPage):
options = { options = {
"policy_terms": { "policy_terms": {
"title": "Terms of Service Page", "title": "Terms of Service Page",
"help_text": "Will only be shown if it has content. Use Markdown for formatting.", "help_text": "Will only be shown if it has content. Use Markdown for formatting.\nIf you would like to redirect elsewhere, enter just a URL.",
"display": "textarea", "display": "textarea",
}, },
"policy_privacy": { "policy_privacy": {
"title": "Privacy Policy Page", "title": "Privacy Policy Page",
"help_text": "Will only be shown if it has content. Use Markdown for formatting.", "help_text": "Will only be shown if it has content. Use Markdown for formatting.\nIf you would like to redirect elsewhere, enter just a URL.",
"display": "textarea", "display": "textarea",
}, },
"policy_rules": { "policy_rules": {
"title": "Server Rules Page", "title": "Server Rules Page",
"help_text": "Will only be shown if it has content. Use Markdown for formatting.", "help_text": "Will only be shown if it has content. Use Markdown for formatting.\nIf you would like to redirect elsewhere, enter just a URL.",
"display": "textarea", "display": "textarea",
}, },
} }