##// END OF EJS Templates
Search for image for every domain level starting from the lowest one. Cache this into memcached
neko259 -
r1772:1342675d default
parent child Browse files
Show More
@@ -5,7 +5,7 b' from django.contrib.staticfiles.template'
5 from django.core.files.images import get_image_dimensions
5 from django.core.files.images import get_image_dimensions
6 from django.template.defaultfilters import filesizeformat
6 from django.template.defaultfilters import filesizeformat
7
7
8 from boards.utils import get_domain
8 from boards.utils import get_domain, cached_result
9
9
10
10
11 FILE_STUB_IMAGE = 'images/file.png'
11 FILE_STUB_IMAGE = 'images/file.png'
@@ -179,11 +179,7 b' class UrlViewer(AbstractViewer):'
179 if protocol in URL_PROTOCOLS:
179 if protocol in URL_PROTOCOLS:
180 url_image_name = URL_PROTOCOLS.get(protocol)
180 url_image_name = URL_PROTOCOLS.get(protocol)
181 elif domain:
181 elif domain:
182 filename = 'images/domains/{}.png'.format(domain)
182 url_image_name = self._find_image_for_domains(domain) or FILE_STUB_URL
183 if file_exists(filename):
184 url_image_name = 'domains/' + domain
185 else:
186 url_image_name = FILE_STUB_URL
187 else:
183 else:
188 url_image_name = FILE_STUB_URL
184 url_image_name = FILE_STUB_URL
189
185
@@ -195,7 +191,20 b' class UrlViewer(AbstractViewer):'
195 '<img class="url-image" src="{}" width="{}" height="{}"/>' \
191 '<img class="url-image" src="{}" width="{}" height="{}"/>' \
196 '</a>'.format(self.url, image, w, h)
192 '</a>'.format(self.url, image, w, h)
197
193
194 @cached_result()
195 def _find_image_for_domains(self, domain):
196 """
197 Searches for the domain image for every domain level except top.
198 E.g. for l3.example.co.uk it will search for l3.example.co.uk, then
199 example.co.uk, then co.uk
200 """
201 levels = domain.split('.')
202 while len(levels) > 1:
203 domain = '.'.join(levels)
198
204
199 def _get_protocol(self):
205 filename = 'images/domains/{}.png'.format(domain)
200 pass
206 if file_exists(filename):
207 return 'domains/' + domain
208 else:
209 del levels[0]
201
210
@@ -34,10 +34,6 b" ANON_IP = '127.0.0.1'"
34
34
35 FILE_EXTENSION_DELIMITER = '.'
35 FILE_EXTENSION_DELIMITER = '.'
36
36
37 KNOWN_DOMAINS = (
38 'org.ru',
39 )
40
41
37
42 def is_anonymous_mode():
38 def is_anonymous_mode():
43 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
39 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
@@ -157,21 +153,5 b' def get_domain(url: str) -> str:'
157 else:
153 else:
158 full_domain = ''
154 full_domain = ''
159
155
160 result = full_domain
156 return full_domain
161 if full_domain:
162 levels = full_domain.split('.')
163 if len(levels) >= 2:
164 top = levels[-1]
165 second = levels[-2]
166
157
167 has_third_level = len(levels) > 2
168 if has_third_level:
169 third = levels[-3]
170
171 if has_third_level and ('{}.{}'.format(second, top) in KNOWN_DOMAINS):
172 result = '{}.{}.{}'.format(third, second, top)
173 else:
174 result = '{}.{}'.format(second, top)
175
176 return result
177
General Comments 0
You need to be logged in to leave comments. Login now