Move the 'utils' template tags to the shared app

This commit is contained in:
Martin Pépin 2020-05-14 22:30:28 +02:00
parent 87184eaadc
commit 001a832116
No known key found for this signature in database
GPG key ID: E7520278B1774448
4 changed files with 33 additions and 30 deletions

View file

@ -1,4 +1,4 @@
{% load utils %}
{% load search_utils %}
<ul>
{% if members %}

View file

@ -1,7 +1,4 @@
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@ -15,29 +12,3 @@ def key(d, key_name):
value = settings.TEMPLATE_STRING_IF_INVALID
return value
def highlight_text(text, q):
q2 = "|".join(re.escape(word) for word in q.split())
pattern = re.compile(r"(?P<filter>%s)" % q2, re.IGNORECASE)
return mark_safe(
re.sub(pattern, r"<span class='highlight'>\g<filter></span>", text)
)
@register.filter
def highlight_user(user, q):
if user.first_name and user.last_name:
text = "%s %s (<tt>%s</tt>)" % (user.first_name, user.last_name, user.username)
else:
text = user.username
return highlight_text(text, q)
@register.filter
def highlight_clipper(clipper, q):
if clipper.fullname:
text = "%s (<tt>%s</tt>)" % (clipper.fullname, clipper.clipper)
else:
text = clipper.clipper
return highlight_text(text, q)

View file

@ -0,0 +1,32 @@
import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
def highlight_text(text, q):
q2 = "|".join(re.escape(word) for word in q.split())
pattern = re.compile(r"(?P<filter>%s)" % q2, re.IGNORECASE)
return mark_safe(
re.sub(pattern, r"<span class='highlight'>\g<filter></span>", text)
)
@register.filter
def highlight_user(user, q):
if user.first_name and user.last_name:
text = "%s %s (<tt>%s</tt>)" % (user.first_name, user.last_name, user.username)
else:
text = user.username
return highlight_text(text, q)
@register.filter
def highlight_clipper(clipper, q):
if clipper.fullname:
text = "%s (<tt>%s</tt>)" % (clipper.fullname, clipper.clipper)
else:
text = clipper.clipper
return highlight_text(text, q)