diff --git a/emencia/django/newsletter/mailer.py b/emencia/django/newsletter/mailer.py index 20a78a56..c02b9d00 100644 --- a/emencia/django/newsletter/mailer.py +++ b/emencia/django/newsletter/mailer.py @@ -264,7 +264,7 @@ class NewsLetterSender(object): resize = context_attachments_size.get(section) for event in objects: prefix = '{}_{}_'.format(section, event.object.event_type) - msg_attachment = self.gen_attachment_logo(event.object, prefix=prefix, resize=resize) + msg_attachment = self.gen_attachment_logo(event.object, prefix=prefix, resize=resize, dailymail=section) if msg_attachment: attachments.append(msg_attachment) return attachments @@ -328,7 +328,7 @@ class NewsLetterSender(object): message_attachment.add_header('Content-ID', '<{}>'.format(cid)) return message_attachment - def gen_attachment_logo(self, obj=None, prefix='logo_', resize=None, logo=None): + def gen_attachment_logo(self, obj=None, prefix='logo_', resize=None, logo=None, **kwargs): if obj is not None: logo = getattr(obj, 'logo') if not logo: @@ -343,7 +343,7 @@ class NewsLetterSender(object): maintype, subtype = ctype.split('/', 1) try: if resize is not None: - fd = get_thumbnail(logo.path, resize) + fd = get_thumbnail(logo.path, resize, **kwargs) # import pdb; pdb.set_trace() # fd = open(thumb.path, 'rb') else: diff --git a/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html index 20a03dae..346da734 100644 --- a/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html +++ b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html @@ -202,7 +202,7 @@ - {% thumbnail obj.get_logo '90x90' as im %} + {% thumbnail obj.get_logo '109x114' dailymail='russia' hit=obj.expohit as im %} {% endthumbnail %} @@ -281,7 +281,7 @@ - {% thumbnail obj.get_logo '100x100' as im %} + {% thumbnail obj.get_logo '109x114' dailymail='russia' hit=obj.expohit as im %} {% endthumbnail %} @@ -379,7 +379,7 @@ - {% thumbnail obj.get_logo '120x120' as im %} + {% thumbnail obj.get_logo '130x130' dailymail='foreign' hit=obj.expohit as im %} {% endthumbnail %} diff --git a/proj/sorlengine.py b/proj/sorlengine.py new file mode 100644 index 00000000..fa8ec7c6 --- /dev/null +++ b/proj/sorlengine.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +Sorl Thumbnail Engine that create special mailing thumbnails +""" +from PIL import Image # , ImageColor +from sorl.thumbnail.engines.pil_engine import Engine + +from django.contrib.staticfiles import finders + +russia_size = (89, 94) +foreign_size = (110, 110) +russia_background = finders.find('newsletter/images/dailymail_russia_background.png') +foreign_background = finders.find('newsletter/images/dailymail_foreign_background.png') +hit_mark = finders.find('newsletter/images/hitmark.png') + + +class SorlEngine(Engine): + def create_dailymail_thumb(self, image, size, background, hit=False): + # берем фон + thumb = Image.open(background) + # берем ресайз картинки + image.thumbnail(size, resample=4) + # считаем положение + box = ( + (thumb.width - image.width) / 2, # left + (thumb.height - image.height) / 2, # top + (thumb.width - image.width) / 2 + image.width, # right + (thumb.height - image.height) / 2 + image.height # bottom + ) + # вставляем + if image.mode == "RGBA": + # с прозрачностью + thumb.paste(image, box, image) + else: + # без + thumb.paste(image, box) + + # вставляем отметку "ХИТ" + if hit: + hit_mark_image = Image.open(hit_mark) + thumb.paste(hit_mark_image, (0, 0), hit_mark_image) + return thumb + + def create(self, image, geometry, options): + if options.get('dailymail') in ['russia', 'moscow']: + return self.create_dailymail_thumb(image, russia_size, russia_background, options.get('hit', False)) + if options.get('dailymail') == 'foreign': + return self.create_dailymail_thumb(image, foreign_size, foreign_background, options.get('hit', False)) + return super(SorlEngine, self).create(image, geometry, options) diff --git a/static/newsletter/images/dailymail_foreign_background.png b/static/newsletter/images/dailymail_foreign_background.png new file mode 100644 index 00000000..88e087c7 Binary files /dev/null and b/static/newsletter/images/dailymail_foreign_background.png differ diff --git a/static/newsletter/images/dailymail_russia_background.png b/static/newsletter/images/dailymail_russia_background.png new file mode 100644 index 00000000..c3679836 Binary files /dev/null and b/static/newsletter/images/dailymail_russia_background.png differ diff --git a/static/newsletter/images/hitmark.png b/static/newsletter/images/hitmark.png new file mode 100644 index 00000000..92399d35 Binary files /dev/null and b/static/newsletter/images/hitmark.png differ diff --git a/support/d_dev/settings.py b/support/d_dev/settings.py index 14bb4a03..12bb23ba 100644 --- a/support/d_dev/settings.py +++ b/support/d_dev/settings.py @@ -475,6 +475,8 @@ LOGGING = { """ THUMBNAIL_DEBUG = DEBUG +THUMBNAIL_ENGINE = "proj.sorlengine.SorlEngine" +THUMBNAIL_FORMAT = "PNG" CALLBACK_EMAIL = 'kotzilla@ukr.net' diff --git a/support/dev/settings.py b/support/dev/settings.py index 9772547c..4f1294cf 100644 --- a/support/dev/settings.py +++ b/support/dev/settings.py @@ -474,6 +474,8 @@ LOGGING = { """ THUMBNAIL_DEBUG = DEBUG +THUMBNAIL_ENGINE = "proj.sorlengine.SorlEngine" +THUMBNAIL_FORMAT = "PNG" CALLBACK_EMAIL = 'kotzilla@ukr.net' diff --git a/support/prod/settings.py b/support/prod/settings.py index 8125fa63..17928d28 100644 --- a/support/prod/settings.py +++ b/support/prod/settings.py @@ -473,6 +473,8 @@ LOGGING = { """ THUMBNAIL_DEBUG = DEBUG +THUMBNAIL_ENGINE = "proj.sorlengine.SorlEngine" +THUMBNAIL_FORMAT = "PNG" CALLBACK_EMAIL = 'kotzilla@ukr.net'