# HG changeset patch # User neko259 # Date 2016-12-16 14:32:47 # Node ID d0d5dc510429945b48465355f63495714c8fc2ce # Parent 64047c29b8f4ce275f8cea9dbcf0a0b4d2580add Simplify adding new domain images. Not there is no separate dict, just searching a file by name from a static finder diff --git a/boards/models/attachment/domains.py b/boards/models/attachment/domains.py deleted file mode 100644 --- a/boards/models/attachment/domains.py +++ /dev/null @@ -1,6 +0,0 @@ -URL_DOMAINS = { - 'meduza.io': 'meduza', - 'youtube.com': 'youtube', - 'youtu.be': 'youtube', - 'wikipedia.org': 'wikipedia', -} diff --git a/boards/models/attachment/viewers.py b/boards/models/attachment/viewers.py --- a/boards/models/attachment/viewers.py +++ b/boards/models/attachment/viewers.py @@ -5,7 +5,6 @@ from django.contrib.staticfiles.template from django.core.files.images import get_image_dimensions from django.template.defaultfilters import filesizeformat -from boards.models.attachment.domains import URL_DOMAINS REGEX_DOMAIN = re.compile(r'(\w+\.)*(\w+\.\w+)') @@ -60,6 +59,11 @@ def get_static_dimensions(filename): return get_image_dimensions(file_path) +# TODO Move this to utils +def file_exists(filename): + return finders.find(filename) is not None + + class AbstractViewer: def __init__(self, file, file_type, hash, url): self.file = file @@ -172,14 +176,16 @@ class UrlViewer(AbstractViewer): if protocol in URL_PROTOCOLS: url_image_name = URL_PROTOCOLS.get(protocol) - elif domain in URL_DOMAINS: - url_image_name = 'domains/' + URL_DOMAINS.get(domain) else: - url_image_name = FILE_STUB_URL + filename = 'images/domains/{}.png'.format(domain) + if file_exists(filename): + url_image_name = 'domains/' + domain + else: + url_image_name = FILE_STUB_URL - image = static('images/' + url_image_name + '.png') - - w, h = get_static_dimensions('images/' + url_image_name + '.png') + image_path = 'images/{}.png'.format(url_image_name) + image = static(image_path) + w, h = get_static_dimensions(image_path) return '' \ '' \ diff --git a/boards/static/images/domains/meduza.png b/boards/static/images/domains/meduza.io.png rename from boards/static/images/domains/meduza.png rename to boards/static/images/domains/meduza.io.png diff --git a/boards/static/images/domains/wikipedia.png b/boards/static/images/domains/wikipedia.org.png rename from boards/static/images/domains/wikipedia.png rename to boards/static/images/domains/wikipedia.org.png diff --git a/boards/static/images/domains/youtube.png b/boards/static/images/domains/youtu.be.png rename from boards/static/images/domains/youtube.png rename to boards/static/images/domains/youtu.be.png diff --git a/boards/static/images/domains/youtube.png b/boards/static/images/domains/youtube.com.png copy from boards/static/images/domains/youtube.png copy to boards/static/images/domains/youtube.com.png