mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 13:03:29 +00:00
app_filters.py: Use Django to load template.
This commit allows us to load template using Django in render_markdown_path.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from typing import Dict, Optional, Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.template import Library
|
||||
from django.template import Library, loader
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.lru_cache import lru_cache
|
||||
|
||||
@@ -78,6 +80,7 @@ def render_markdown_path(markdown_file_path):
|
||||
md_engine = markdown.Markdown(extensions=md_extensions)
|
||||
md_engine.reset()
|
||||
|
||||
markdown_string = force_text(open(markdown_file_path).read())
|
||||
template = loader.get_template(markdown_file_path)
|
||||
markdown_string = template.render()
|
||||
html = md_engine.convert(markdown_string)
|
||||
return mark_safe(html)
|
||||
|
||||
@@ -158,7 +158,7 @@ class TemplateTestCase(ZulipTestCase):
|
||||
user_profile = get_user_profile_by_email(email)
|
||||
|
||||
context = dict(
|
||||
article="templates/zerver/help/index.md",
|
||||
article="zerver/help/index.md",
|
||||
shallow_tested=True,
|
||||
user_profile=user_profile,
|
||||
user=user_profile,
|
||||
|
||||
@@ -4,6 +4,7 @@ from collections import OrderedDict
|
||||
from django.views.generic import TemplateView
|
||||
from django.conf import settings
|
||||
from django.http import HttpRequest, HttpResponse, HttpResponseNotFound
|
||||
from django.template import loader
|
||||
from django.shortcuts import render
|
||||
|
||||
import os
|
||||
@@ -49,7 +50,7 @@ class APIView(ApiURLView):
|
||||
|
||||
class HelpView(ApiURLView):
|
||||
template_name = 'zerver/help/main.html'
|
||||
path_template = os.path.join(settings.DEPLOY_ROOT, 'templates/zerver/help/%s.md')
|
||||
path_template = 'zerver/help/%s.md'
|
||||
|
||||
def get_path(self, article):
|
||||
# type: (str) -> str
|
||||
@@ -62,10 +63,12 @@ class HelpView(ApiURLView):
|
||||
article = kwargs["article"]
|
||||
context = super(HelpView, self).get_context_data() # type: Dict[str, Any]
|
||||
path = self.get_path(article)
|
||||
if os.path.exists(path):
|
||||
try:
|
||||
loader.get_template(path)
|
||||
context["article"] = path
|
||||
else:
|
||||
except loader.TemplateDoesNotExist:
|
||||
context["article"] = self.get_path("missing")
|
||||
|
||||
# For disabling the "Back to home" on the homepage
|
||||
context["not_index_page"] = not path.endswith("/index.md")
|
||||
return context
|
||||
@@ -74,7 +77,9 @@ class HelpView(ApiURLView):
|
||||
# type: (HttpRequest, str) -> HttpResponse
|
||||
path = self.get_path(article)
|
||||
result = super(HelpView, self).get(self, article=article)
|
||||
if not os.path.exists(path):
|
||||
try:
|
||||
loader.get_template(path)
|
||||
except loader.TemplateDoesNotExist:
|
||||
# Ensure a 404 response code if no such document
|
||||
result.status_code = 404
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user