Add filter to list all templates in a directory

This commit is contained in:
Magnus Hagander
2020-10-27 12:09:55 +01:00
parent f5671f2578
commit bc1c7fa1ea

View File

@ -2,7 +2,10 @@ from django.template.defaultfilters import stringfilter
from django import template
from django.utils.safestring import mark_safe
from django.template.loader import get_template
from django.conf import settings
import os
from pathlib import Path
import json
import pynliner
@ -103,6 +106,13 @@ def joinandor(value, andor):
return ", ".join([str(x) for x in value[:-1]]) + ' ' + andor + ' ' + str(value[-1])
@register.filter()
def list_templates(value):
for f in Path(os.path.join(settings.PROJECT_ROOT, '../templates/', value)).iterdir():
if f.is_file() and f.suffix == '.html':
yield f.stem
@register.simple_tag(takes_context=True)
def git_changes_link(context):
return mark_safe('<a href="https://git.postgresql.org/gitweb/?p=pgweb.git;a=history;f=templates/{}">View</a> change history.'.format(context.template_name))