Move the 'utils' template tags to the shared app
This commit is contained in:
parent
f6458074b2
commit
0789da7bed
4 changed files with 33 additions and 30 deletions
|
@ -1,4 +1,4 @@
|
|||
{% load utils %}
|
||||
{% load search_utils %}
|
||||
|
||||
<ul>
|
||||
{% if members %}
|
||||
|
|
|
@ -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)
|
||||
|
|
32
shared/templatetags/search_utils.py
Normal file
32
shared/templatetags/search_utils.py
Normal 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)
|
Loading…
Reference in a new issue