Mercurial > sat_legacy_website
comparison 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 |
comparison
equal
deleted
inserted
replaced
135:b61cb60a8eb2 | 136:c56b6ad62153 |
---|---|
1 from django import template | 1 from django import template |
2 from django.forms import RadioSelect, CheckboxInput | 2 from django.forms import RadioSelect, CheckboxInput |
3 from django.utils.translation import get_language, ugettext_lazy as _ | 3 from django.utils.translation import get_language, ugettext_lazy as _ |
4 import urllib2 | |
5 import urlparse | |
6 import re | |
4 register = template.Library() | 7 register = template.Library() |
5 | 8 |
6 @register.filter | 9 @register.filter |
7 def is_tuple(value): | 10 def is_tuple(value): |
8 """Tell if this value is a tuple or not. | 11 """Tell if this value is a tuple or not. |
104 lang_value = lang if lang in entry.paths else entry.paths.keys()[0] | 107 lang_value = lang if lang in entry.paths else entry.paths.keys()[0] |
105 items.append(get_item(lang_key, lang_value)) | 108 items.append(get_item(lang_key, lang_value)) |
106 except AttributeError: | 109 except AttributeError: |
107 pass | 110 pass |
108 return "(%s)" % ', '.join(items) | 111 return "(%s)" % ', '.join(items) |
112 | |
113 @register.simple_tag | |
114 def insert_blog_content(url): | |
115 # XXX: quick and dirty | |
116 url = url.encode("utf-8") | |
117 parse_result = urlparse.urlparse(url, "http") | |
118 base_url = urlparse.urljoin("%s://%s" % (parse_result.scheme, parse_result.netloc), "blog") | |
119 text = urllib2.urlopen(url).read() | |
120 subs = [] | |
121 for match in re.finditer(r"(href=|src=)\"([^\"]*)\"", text): | |
122 if re.match("^[^:]*://", match.group(2)): # keep absolute link | |
123 continue | |
124 # complete relative link | |
125 subs.append((match.group(0), match.group(1) + '"%s"' % urlparse.urljoin(base_url, match.group(2)))) | |
126 for url, new_url in subs: | |
127 text = text.replace(url, new_url) | |
128 return text |