Mercurial > sat_legacy_website
diff sat_website/templatetags/utils.py @ 136:c56b6ad62153
add section News with the official news feed
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 14 Jan 2016 23:39:43 +0100 |
parents | 9ae3d9c8b28a |
children |
line wrap: on
line diff
--- a/sat_website/templatetags/utils.py Wed Dec 09 03:01:48 2015 +0100 +++ b/sat_website/templatetags/utils.py Thu Jan 14 23:39:43 2016 +0100 @@ -1,6 +1,9 @@ from django import template from django.forms import RadioSelect, CheckboxInput from django.utils.translation import get_language, ugettext_lazy as _ +import urllib2 +import urlparse +import re register = template.Library() @register.filter @@ -106,3 +109,20 @@ except AttributeError: pass return "(%s)" % ', '.join(items) + +@register.simple_tag +def insert_blog_content(url): + # XXX: quick and dirty + url = url.encode("utf-8") + parse_result = urlparse.urlparse(url, "http") + base_url = urlparse.urljoin("%s://%s" % (parse_result.scheme, parse_result.netloc), "blog") + text = urllib2.urlopen(url).read() + subs = [] + for match in re.finditer(r"(href=|src=)\"([^\"]*)\"", text): + if re.match("^[^:]*://", match.group(2)): # keep absolute link + continue + # complete relative link + subs.append((match.group(0), match.group(1) + '"%s"' % urlparse.urljoin(base_url, match.group(2)))) + for url, new_url in subs: + text = text.replace(url, new_url) + return text