2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
from django import template
|
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import mark_safe
|
2016-08-20 01:20:06 +02:00
|
|
|
from kfet.models import Settings
|
2016-11-23 04:39:31 +01:00
|
|
|
from math import floor
|
2016-08-02 10:40:46 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
2017-03-15 20:11:38 +01:00
|
|
|
|
2017-01-19 04:42:00 +01:00
|
|
|
@register.filter()
|
2016-08-02 10:40:46 +02:00
|
|
|
def highlight_text(text, q):
|
2017-03-19 15:48:04 +01:00
|
|
|
q2 = "|".join(re.escape(word) for word in q.split())
|
2016-08-02 10:40:46 +02:00
|
|
|
pattern = re.compile(r"(?P<filter>%s)" % q2, re.IGNORECASE)
|
2017-03-15 20:11:38 +01:00
|
|
|
regex = r"<span class='highlight_autocomplete'>\g<filter></span>"
|
2017-03-16 02:58:54 +01:00
|
|
|
return mark_safe(re.sub(pattern, regex, escape(text)))
|
2017-03-15 20:11:38 +01:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
@register.filter(is_safe=True)
|
|
|
|
def highlight_user(user, q):
|
|
|
|
if user.first_name and user.last_name:
|
|
|
|
text = "%s %s (%s)" % (user.first_name, user.last_name, user.username)
|
|
|
|
else:
|
|
|
|
text = user.username
|
2017-01-19 04:42:00 +01:00
|
|
|
return highlight_text(text, q)
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-03-15 20:11:38 +01:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
@register.filter(is_safe=True)
|
|
|
|
def highlight_clipper(clipper, q):
|
|
|
|
if clipper.fullname:
|
2017-03-15 20:11:38 +01:00
|
|
|
text = "%s (%s)" % (clipper.fullname, clipper.clipper)
|
2016-08-02 10:40:46 +02:00
|
|
|
else:
|
2017-03-15 20:11:38 +01:00
|
|
|
text = clipper.clipper
|
2017-01-19 04:42:00 +01:00
|
|
|
return highlight_text(text, q)
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-03-15 20:11:38 +01:00
|
|
|
|
2016-08-20 01:20:06 +02:00
|
|
|
@register.filter()
|
|
|
|
def ukf(balance, is_cof):
|
|
|
|
grant = is_cof and (1 + Settings.SUBVENTION_COF() / 100) or 1
|
2016-11-23 04:43:24 +01:00
|
|
|
return floor(balance * 10 * grant)
|