From f3d8a357065389cf476fcacad2e4d97954df2e6e Mon Sep 17 00:00:00 2001 From: Samuel Huang Date: Sat, 18 Feb 2023 23:42:28 -0800 Subject: [PATCH] Refactor templatetag logic into multiple modules --- places/templates/places/restaurant_list.html | 2 +- .../{base_extras.py => navbar_extras.py} | 10 ---------- portal/templatetags/query_extras.py | 14 ++++++++++++++ templates/core/navigation.html | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) rename portal/templatetags/{base_extras.py => navbar_extras.py} (59%) create mode 100644 portal/templatetags/query_extras.py diff --git a/places/templates/places/restaurant_list.html b/places/templates/places/restaurant_list.html index 3988b44..ecdf3b7 100644 --- a/places/templates/places/restaurant_list.html +++ b/places/templates/places/restaurant_list.html @@ -1,5 +1,5 @@ {% extends 'simple.html' %} -{% load base_extras %} +{% load query_extras %} {% block meta %} Chowist | Restaurant List diff --git a/portal/templatetags/base_extras.py b/portal/templatetags/navbar_extras.py similarity index 59% rename from portal/templatetags/base_extras.py rename to portal/templatetags/navbar_extras.py index 58c68f5..41c8bc4 100644 --- a/portal/templatetags/base_extras.py +++ b/portal/templatetags/navbar_extras.py @@ -18,13 +18,3 @@ def nav_active(request, url): if reverse_url in current_url and reverse_url != "/": return _NavigationStatus.ACTIVE return _NavigationStatus.INACTIVE - - -@register.simple_tag(takes_context=True) -def updated_params(context, **kwargs): - # https://blog.ovalerio.net/archives/1512 - # https://docs.djangoproject.com/en/4.1/ref/request-response/ - query_dict = context["request"].GET.copy() - for k, v in kwargs.items(): - query_dict[k] = v - return query_dict.urlencode() diff --git a/portal/templatetags/query_extras.py b/portal/templatetags/query_extras.py new file mode 100644 index 0000000..3645d46 --- /dev/null +++ b/portal/templatetags/query_extras.py @@ -0,0 +1,14 @@ +from django import template +from django.urls import reverse + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def updated_params(context, **kwargs): + # https://blog.ovalerio.net/archives/1512 + # https://docs.djangoproject.com/en/4.1/ref/request-response/ + query_dict = context["request"].GET.copy() + for k, v in kwargs.items(): + query_dict[k] = v + return query_dict.urlencode() diff --git a/templates/core/navigation.html b/templates/core/navigation.html index 270fc15..f666239 100644 --- a/templates/core/navigation.html +++ b/templates/core/navigation.html @@ -1,4 +1,4 @@ -{% load base_extras %} +{% load navbar_extras %}