From b0fc20f6e5ae6c2632d49aef9c3ea20a8df1df22 Mon Sep 17 00:00:00 2001 From: Thibaud Colas Date: Mon, 2 Oct 2023 12:49:49 +0100 Subject: [PATCH] Convert all templates and views to Jinja --- bakerydemo/base/jinja2tags.py | 59 ++++++++++++++++++ bakerydemo/base/templatetags/gallery_tags.py | 7 +-- .../base/templatetags/navigation_tags.py | 21 ++----- bakerydemo/blog/models.py | 2 +- bakerydemo/jinja2/base.html | 18 +++--- bakerydemo/jinja2/base/form_page.html | 11 ++-- bakerydemo/jinja2/base/form_page_landing.html | 1 - bakerydemo/jinja2/base/gallery_page.html | 3 +- bakerydemo/jinja2/base/home_page.html | 27 ++++----- .../jinja2/base/include/footer_text.html | 2 +- .../jinja2/base/include/header-blog.html | 6 +- .../jinja2/base/include/header-hero.html | 4 +- .../jinja2/base/include/header-index.html | 2 - bakerydemo/jinja2/base/include/header.html | 4 +- bakerydemo/jinja2/base/preview/person.html | 3 +- bakerydemo/jinja2/base/standard_page.html | 1 - bakerydemo/jinja2/blocks/blockquote.html | 6 +- bakerydemo/jinja2/blocks/embed_block.html | 4 +- bakerydemo/jinja2/blocks/heading_block.html | 16 ++--- bakerydemo/jinja2/blocks/image_block.html | 6 +- bakerydemo/jinja2/blocks/paragraph_block.html | 2 +- .../jinja2/blocks/recipe_step_block.html | 2 +- bakerydemo/jinja2/blog/blog_index_page.html | 5 +- bakerydemo/jinja2/blog/blog_page.html | 3 +- bakerydemo/jinja2/breads/bread_page.html | 3 +- .../jinja2/breads/breads_index_page.html | 8 +-- .../includes/card/blog-listing-card.html | 14 ++--- .../jinja2/includes/card/listing-card.html | 4 +- .../jinja2/includes/card/location-card.html | 6 +- .../jinja2/includes/card/picture-card.html | 6 +- bakerydemo/jinja2/includes/footer.html | 60 +++++++++---------- bakerydemo/jinja2/includes/header.html | 8 +-- bakerydemo/jinja2/includes/pagination.html | 2 - .../jinja2/locations/location_page.html | 5 +- .../locations/locations_index_page.html | 6 +- .../jinja2/recipes/recipe_index_page.html | 6 +- bakerydemo/jinja2/recipes/recipe_page.html | 7 +-- bakerydemo/jinja2/search/search_results.html | 11 ++-- bakerydemo/jinja2/tags/breadcrumbs.html | 9 ++- bakerydemo/jinja2/tags/gallery.html | 5 +- bakerydemo/jinja2/tags/top_menu.html | 12 ++-- bakerydemo/jinja2/tags/top_menu_children.html | 5 +- bakerydemo/settings/base.py | 19 ++++++ requirements/base.txt | 1 + 44 files changed, 212 insertions(+), 200 deletions(-) create mode 100644 bakerydemo/base/jinja2tags.py diff --git a/bakerydemo/base/jinja2tags.py b/bakerydemo/base/jinja2tags.py new file mode 100644 index 000000000..a381f9e72 --- /dev/null +++ b/bakerydemo/base/jinja2tags.py @@ -0,0 +1,59 @@ +from django.template.context_processors import csrf +from django.template.defaultfilters import ( + cut, + date, + linebreaks, + pluralize, + slugify, + truncatewords, + urlencode, +) +from django.contrib.staticfiles.storage import staticfiles_storage +from jinja2 import pass_context +from jinja2.ext import Extension + +from wagtail.contrib.search_promotions.templatetags.wagtailsearchpromotions_tags import ( + get_search_promotions, +) + +from bakerydemo.base.templatetags.navigation_tags import ( + breadcrumbs, + get_footer_text, + get_site_root, + top_menu, + top_menu_children, +) +from bakerydemo.base.templatetags.gallery_tags import gallery + + +class BaseExtension(Extension): + def __init__(self, environment): + super().__init__(environment) + self.environment.globals.update( + { + "static": staticfiles_storage.url, + "csrf": csrf, + "get_search_promotions": get_search_promotions, + "breadcrumbs": pass_context(breadcrumbs), + "get_footer_text": pass_context(get_footer_text), + "get_site_root": pass_context(get_site_root), + "top_menu": top_menu, + "top_menu_children": top_menu_children, + "gallery": gallery, + } + ) + + self.environment.filters.update( + { + "cut": cut, + "date": date, + "linebreaks": linebreaks, + "pluralize": pluralize, + "slugify": slugify, + "truncatewords": truncatewords, + "urlencode": urlencode, + } + ) + + +base = BaseExtension diff --git a/bakerydemo/base/templatetags/gallery_tags.py b/bakerydemo/base/templatetags/gallery_tags.py index 974f6a25b..cadb14dd5 100644 --- a/bakerydemo/base/templatetags/gallery_tags.py +++ b/bakerydemo/base/templatetags/gallery_tags.py @@ -1,15 +1,10 @@ -from django import template from wagtail.images.models import Image -register = template.Library() - # Retrieves a single gallery item and returns a gallery of images -@register.inclusion_tag("tags/gallery.html", takes_context=True) -def gallery(context, gallery): +def gallery(gallery): images = Image.objects.filter(collection=gallery) return { "images": images, - "request": context["request"], } diff --git a/bakerydemo/base/templatetags/navigation_tags.py b/bakerydemo/base/templatetags/navigation_tags.py index 75699dd26..c758c6602 100644 --- a/bakerydemo/base/templatetags/navigation_tags.py +++ b/bakerydemo/base/templatetags/navigation_tags.py @@ -1,13 +1,8 @@ -from django import template from wagtail.models import Page, Site from bakerydemo.base.models import FooterText -register = template.Library() -# https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/ - -@register.simple_tag(takes_context=True) def get_site_root(context): # This returns a core.Page. The main menu needs to have the site.root_page # defined else will return an object attribute error ('str' object has no @@ -35,8 +30,7 @@ def is_active(page, current_page): # Retrieves the top menu items - the immediate children of the parent page # The has_menu_children method is necessary because the Foundation menu requires # a dropdown class to be applied to a parent -@register.inclusion_tag("tags/top_menu.html", takes_context=True) -def top_menu(context, parent, calling_page=None): +def top_menu(parent, calling_page=None): menuitems = parent.get_children().live().in_menu() for menuitem in menuitems: menuitem.show_dropdown = has_menu_children(menuitem) @@ -51,14 +45,11 @@ def top_menu(context, parent, calling_page=None): return { "calling_page": calling_page, "menuitems": menuitems, - # required by the pageurl tag that we want to use within this template - "request": context["request"], } # Retrieves the children of the top menu items for the drop downs -@register.inclusion_tag("tags/top_menu_children.html", takes_context=True) -def top_menu_children(context, parent, calling_page=None): +def top_menu_children(parent, calling_page=None): menuitems_children = parent.get_children() menuitems_children = menuitems_children.live().in_menu() for menuitem in menuitems_children: @@ -75,14 +66,11 @@ def top_menu_children(context, parent, calling_page=None): return { "parent": parent, "menuitems_children": menuitems_children, - # required by the pageurl tag that we want to use within this template - "request": context["request"], } -@register.inclusion_tag("tags/breadcrumbs.html", takes_context=True) def breadcrumbs(context): - self = context.get("self") + self = context.get("page") if self is None or self.depth <= 2: # When on the home page, displaying breadcrumbs is irrelevant. ancestors = () @@ -90,12 +78,11 @@ def breadcrumbs(context): ancestors = Page.objects.ancestor_of(self, inclusive=True).filter(depth__gt=1) return { "ancestors": ancestors, - "request": context["request"], } -@register.inclusion_tag("base/include/footer_text.html", takes_context=True) def get_footer_text(context): + # Use together with base/include/footer_text.html. # Get the footer text from the context if exists, # so that it's possible to pass a custom instance e.g. for previews # or page types that need a custom footer diff --git a/bakerydemo/blog/models.py b/bakerydemo/blog/models.py index 2f97ead6d..1d26747ab 100644 --- a/bakerydemo/blog/models.py +++ b/bakerydemo/blog/models.py @@ -188,7 +188,7 @@ def tag_archive(self, request, tag=None): return redirect(self.url) posts = self.get_posts(tag=tag) - context = {"self": self, "tag": tag, "posts": posts} + context = {"page": self, "tag": tag, "posts": posts} return render(request, "blog/blog_index_page.html", context) def serve_preview(self, request, mode_name): diff --git a/bakerydemo/jinja2/base.html b/bakerydemo/jinja2/base.html index 2bcd75dd4..240521116 100755 --- a/bakerydemo/jinja2/base.html +++ b/bakerydemo/jinja2/base.html @@ -1,4 +1,3 @@ -{% load navigation_tags static wagtailuserbar %} @@ -12,7 +11,7 @@ {% endif %} {% endblock %} {% block title_suffix %} - | {{ settings.base.SiteSettings.title_suffix }} + | {{ settings("base.SiteSettings").title_suffix }} {% endblock %} @@ -23,21 +22,20 @@ {% endif %} - - - + + + - - {% wagtailuserbar %} + + {{ wagtailuserbar() }} {% block header %} {% include "includes/header.html" %} {% endblock header %} {% block breadcrumbs %} - {# breadcrumbs is defined in base/templatetags/navigation_tags.py #} - {% breadcrumbs %} + {% include "tags/breadcrumbs.html" %} {% endblock breadcrumbs %} {% block messages %} @@ -53,6 +51,6 @@ {% include "includes/footer.html" %} - + diff --git a/bakerydemo/jinja2/base/form_page.html b/bakerydemo/jinja2/base/form_page.html index 022623508..ec97a5a00 100644 --- a/bakerydemo/jinja2/base/form_page.html +++ b/bakerydemo/jinja2/base/form_page.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load wagtailcore_tags navigation_tags wagtailimages_tags %} {% block content %} @@ -22,12 +21,12 @@

{{ page.title }}

- {% comment %} + {# You could render your form using a Django rendering shortcut such as `{{ form.as_p }}` but that will tend towards unsemantic code, and make it difficult to style. You can read more on Django form at: https://docs.djangoproject.com/en/3.2/topics/forms/#form-rendering-options - {% endcomment %} -
- {% csrf_token %} + #} + + {% if form.subject.errors %}
    {% for error in form.subject.errors %} @@ -39,7 +38,7 @@

    {{ page.title }}

    {% for field in form %}
    - {{ field.label_tag }}{% if field.field.required %}*{% endif %} + {{ field.label_tag() }}{% if field.field.required %}*{% endif %} {% if field.help_text %}

    {{ field.help_text }}

    diff --git a/bakerydemo/jinja2/base/form_page_landing.html b/bakerydemo/jinja2/base/form_page_landing.html index fb0b02974..24f4e8467 100644 --- a/bakerydemo/jinja2/base/form_page_landing.html +++ b/bakerydemo/jinja2/base/form_page_landing.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load wagtailcore_tags %} {% block content %} diff --git a/bakerydemo/jinja2/base/gallery_page.html b/bakerydemo/jinja2/base/gallery_page.html index 423f62e55..ae760b2bd 100644 --- a/bakerydemo/jinja2/base/gallery_page.html +++ b/bakerydemo/jinja2/base/gallery_page.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load wagtailimages_tags gallery_tags %} {% block content %} {% include "base/include/header-hero.html" %} @@ -13,7 +12,7 @@
{% endblock content %} diff --git a/bakerydemo/jinja2/base/home_page.html b/bakerydemo/jinja2/base/home_page.html index 46c6bca9b..2d8f74b3e 100644 --- a/bakerydemo/jinja2/base/home_page.html +++ b/bakerydemo/jinja2/base/home_page.html @@ -1,11 +1,10 @@ {% extends "base.html" %} -{% load wagtailimages_tags wagtailcore_tags %} {% block content %}
- {% image page.image fill-1920x600 class="hero-image" alt="" %} + {{ image(page.image, "fill-1920x600", class="hero-image", alt="") }}
@@ -13,7 +12,7 @@

{{ page.title }}

{{ page.hero_text }}

{% if page.hero_cta_link %} - + {{ page.hero_cta }} {% else %} @@ -30,15 +29,16 @@

{{ page.title }}

{% if page.featured_section_1 %} View more of our breads - {% include "includes/chevron-icon.html" with class="featured-cards__chevron-icon" %} + {% set class="featured-cards__chevron-icon" %} + {% include "includes/chevron-icon.html" %} {% endif %}
@@ -49,13 +49,11 @@ {% if page.promo_title %}

{{ page.promo_title }}

{% endif %} - {% if page.promo_text %} - {{ page.promo_text|richtext }} - {% endif %} + {{ page.promo_text|richtext if page.promo_text }}
{% endif %} {% if page.promo_image %} -
{% image page.promo_image fill-590x413-c100 %}
+
{{ image(page.promo_image, "fill-590x413-c100") }}
{% endif %}
@@ -76,8 +74,8 @@

{{ page.promo_title }}

{% if page.featured_section_2 %}

{{ page.featured_section_2_title }}

- {% for childpage in page.featured_section_2.specific.children|slice:"3" %} - {% include "includes/card/location-card.html" with page=childpage %} + {% for page in page.featured_section_2.specific.children()[:3] %} + {% include "includes/card/location-card.html" %} {% endfor %} {% endif %}
@@ -91,8 +89,9 @@

{{ page.featured_section_2_title }}

{{ page.featured_section_3_title }}

- {% for childpage in page.featured_section_3.specific.children|slice:"6" %} - {% include "includes/card/picture-card.html" with page=childpage portrait=True %} + {% for page in page.featured_section_3.specific.children()[:6] %} + {% set portrait=True %} + {% include "includes/card/picture-card.html" %} {% endfor %}
diff --git a/bakerydemo/jinja2/base/include/footer_text.html b/bakerydemo/jinja2/base/include/footer_text.html index 2ffb49ecc..a63304ea5 100644 --- a/bakerydemo/jinja2/base/include/footer_text.html +++ b/bakerydemo/jinja2/base/include/footer_text.html @@ -1,4 +1,4 @@ -{% load wagtailcore_tags %} +{% set footer_text=get_footer_text().footer_text %}