Skip to content

Commit 9bba714

Browse files
migrate to jinja2 templates
1 parent 522d678 commit 9bba714

13 files changed

Lines changed: 41 additions & 36 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Jinja2 template environment loader."""
2+
3+
from pathlib import Path
4+
5+
from jinja2 import Environment, FileSystemLoader
6+
7+
BASE_DIR = Path(__file__).resolve().parent.parent
8+
SLACK_TEMPLATES_DIR = BASE_DIR / "slack" / "templates"
9+
VIDEO_TEMPLATES_DIR = BASE_DIR / "owasp" / "templates" / "video"
10+
11+
slack_env = Environment(loader=FileSystemLoader(SLACK_TEMPLATES_DIR), autoescape=True)
12+
video_env = Environment(loader=FileSystemLoader(VIDEO_TEMPLATES_DIR), autoescape=True)
File renamed without changes.

backend/apps/owasp/templates/video/slides/chapters.html renamed to backend/apps/owasp/templates/video/slides/chapters.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col p-12 px-24">
44
<h1 class="text-6xl font-bold mb-12 text-center">{{ title }}</h1>
55
<div class="flex flex-wrap gap-4 justify-center w-full">
6-
{% for chapter in chapters|slice:":24" %}
6+
{% for chapter in chapters[:24] %}
77
<div class="rounded-lg border-2 bg-card p-4 shrink-0 w-96 h-32">
88
<div class="text-2xl font-bold text-nest-blue line-clamp-2">{{ chapter.name }}</div>
99
<div class="text-lg text-gray-300 mt-1">{{ chapter.location }}</div>
1010
</div>
1111
{% endfor %}
1212
</div>
1313
{% if chapters|length > 24 %}
14-
<p class="text-center text-gray-400 mt-8 text-xl">+ {{ chapters|length|add:"-24" }} more chapters not shown</p>
14+
<p class="text-center text-gray-400 mt-8 text-xl">+ {{ chapters|length - 24 }} more chapters not shown</p>
1515
{% endif %}
1616
</div>
1717
{% endblock %}

backend/apps/owasp/templates/video/slides/intro.html renamed to backend/apps/owasp/templates/video/slides/intro.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col items-center justify-center p-12 text-center">
44
<h1 class="text-6xl font-bold">{{ name }} OWASP Community Snapshot</h1>

backend/apps/owasp/templates/video/slides/projects.html renamed to backend/apps/owasp/templates/video/slides/projects.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col p-12 px-24">
44
<h1 class="text-6xl font-bold mb-12 text-center">{{ title }}</h1>
55
<div class="flex flex-wrap gap-4 justify-center w-full">
6-
{% for project in projects|slice:":16" %}
6+
{% for project in projects[:16] %}
77
<div class="rounded-lg border-2 bg-card p-4 shrink-0 w-96 h-48">
88
<div class="text-2xl font-bold text-nest-blue mb-2 line-clamp-2">{{ project.name }}</div>
99
<div class="text-lg text-gray-300">
@@ -14,7 +14,7 @@ <h1 class="text-6xl font-bold mb-12 text-center">{{ title }}</h1>
1414
{% endfor %}
1515
</div>
1616
{% if projects|length > 16 %}
17-
<p class="text-center text-gray-400 mt-8 text-xl">+ {{ projects|length|add:"-16" }} more projects not shown</p>
17+
<p class="text-center text-gray-400 mt-8 text-xl">+ {{ projects|length - 16 }} more projects not shown</p>
1818
{% endif %}
1919
</div>
2020
{% endblock %}

backend/apps/owasp/templates/video/slides/releases.html renamed to backend/apps/owasp/templates/video/slides/releases.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col p-12 px-24">
44
<h1 class="text-6xl font-bold mb-12 text-center">{{ title }}</h1>

backend/apps/owasp/templates/video/slides/sponsors.html renamed to backend/apps/owasp/templates/video/slides/sponsors.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col p-8">
44
<h1 class="text-6xl font-bold mb-8 text-center">{{ title }}</h1>

backend/apps/owasp/templates/video/slides/thank_you.html renamed to backend/apps/owasp/templates/video/slides/thank_you.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "video/base.html" %}
1+
{% extends "base.jinja" %}
22
{% block content %}
33
<div class="flex h-full flex-col items-center justify-center relative">
44
<h1 class="text-6xl font-bold text-center">Thank You!</h1>

backend/apps/owasp/video.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import ffmpeg
1010
import pypdfium2 as pdfium
1111
from django.db.models import QuerySet
12-
from django.template.loader import render_to_string
1312
from weasyprint import HTML
1413

1514
from apps.common.eleven_labs import ElevenLabs
15+
from apps.common.template_loader import video_env
1616
from apps.github.models.release import Release
1717
from apps.owasp.models.chapter import Chapter
1818
from apps.owasp.models.project import Project
@@ -63,7 +63,7 @@ def render_and_save_image(self) -> None:
6363
page = None
6464
pdf = None
6565
try:
66-
html = HTML(string=render_to_string(self.template_name, self.context))
66+
html = HTML(string=video_env.get_template(self.template_name).render(self.context))
6767

6868
pdf = pdfium.PdfDocument(html.write_pdf())
6969
page = pdf[0]
@@ -162,7 +162,7 @@ def add_intro_slide(self) -> Slide:
162162
},
163163
name="intro",
164164
output_dir=self.output_dir,
165-
template_name="video/slides/intro.html",
165+
template_name="slides/intro.jinja",
166166
transcript=f"Hey, everyone... Welcome to the Owasp Nest {name} community snapshot...",
167167
)
168168

@@ -190,7 +190,7 @@ def add_sponsors_slide(self) -> Slide:
190190
},
191191
name="sponsors",
192192
output_dir=self.output_dir,
193-
template_name="video/slides/sponsors.html",
193+
template_name="slides/sponsors.jinja",
194194
transcript="A HUGE thanks to the sponsors who make our work possible!",
195195
)
196196

@@ -225,7 +225,7 @@ def add_projects_slide(self) -> Slide | None:
225225
},
226226
name="projects",
227227
output_dir=self.output_dir,
228-
template_name="video/slides/projects.html",
228+
template_name="slides/projects.jinja",
229229
transcript=f"So... this time we've welcomed {project_count} new projects! "
230230
f"including Owasp {formatted_project_names}.",
231231
)
@@ -256,7 +256,7 @@ def add_chapters_slide(self) -> Slide | None:
256256
},
257257
output_dir=self.output_dir,
258258
name="chapters",
259-
template_name="video/slides/chapters.html",
259+
template_name="slides/chapters.jinja",
260260
transcript=f"We've also welcomed {chapter_count} new chapters! "
261261
f"including {formatted_names}.",
262262
)
@@ -299,7 +299,7 @@ def add_releases_slide(self) -> Slide | None:
299299
},
300300
name="releases",
301301
output_dir=self.output_dir,
302-
template_name="video/slides/releases.html",
302+
template_name="slides/releases.jinja",
303303
transcript=f"There were {release_count} new releases across our projects this time... "
304304
f"with {formatted_names} leading the way.",
305305
)
@@ -310,7 +310,7 @@ def add_thank_you_slide(self) -> Slide:
310310
context={},
311311
name="thank_you",
312312
output_dir=self.output_dir,
313-
template_name="video/slides/thank_you.html",
313+
template_name="slides/thank_you.jinja",
314314
transcript="Thanks for tuning in!... Visit Nest dot Owasp dot org "
315315
"for more community updates!... See you next time!!",
316316
)

backend/apps/slack/commands/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from jinja2 import Template
88

99
from apps.common.constants import NL
10+
from apps.common.template_loader import slack_env as env
1011
from apps.common.utils import convert_to_snake_case
1112
from apps.slack.apps import SlackConfig
1213
from apps.slack.blocks import DIVIDER, SECTION_BREAK, markdown
1314
from apps.slack.constants import FEEDBACK_SHARING_INVITE, NEST_BOT_NAME
14-
from apps.slack.template_loader import env
1515
from apps.slack.utils import get_text
1616

1717
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)