it/itf/bestpractices/templatetags/guidelines.py
2011-04-26 00:43:19 +05:30

22 lines
629 B
Python

from django import template
from bestpractices.models import BestPractice
import re
register = template.Library()
def link_bps(value):
r = r'BP\:[0-9][0-9]?'
matches = re.findall(r, value)
for match in matches:
id = int(match.replace("BP:", "").strip())
try:
bp = BestPractice.objects.get(pk=id)
title = bp.title
html = "<a class='bpRelated' href='/itf/bestpractices/stories#%d' title='View related Best Practice story'>%s</a>" % (id, title,)
value = value.replace(match, html, 1)
except:
value = value.replace(match, '', 1)
return value
register.filter("link_bps", link_bps)