|
|
|
|
@ -123,6 +123,14 @@ dailymail_attahcments = { |
|
|
|
|
'vk': 'newsletter/images/vk.png', |
|
|
|
|
'twitter': 'newsletter/images/twitter.png', |
|
|
|
|
} |
|
|
|
|
context_attachments_size = { |
|
|
|
|
'recommended': '281x225', |
|
|
|
|
'news': '272x195', |
|
|
|
|
'blog': '272x195', |
|
|
|
|
'moscow': '109x114', |
|
|
|
|
'russia': '109x114', |
|
|
|
|
'foreign': '109x114', |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewsLetterSender(object): |
|
|
|
|
@ -234,18 +242,20 @@ class NewsLetterSender(object): |
|
|
|
|
|
|
|
|
|
def build_daily_ctx_attachments(self, context): |
|
|
|
|
attachments = [] |
|
|
|
|
for obj in ['recommended', 'news', 'blog']: |
|
|
|
|
_obj = context.get(obj) |
|
|
|
|
for section in ['recommended', 'news', 'blog']: |
|
|
|
|
resize = context_attachments_size.get(section) |
|
|
|
|
_obj = context.get(section) |
|
|
|
|
if _obj: |
|
|
|
|
msg_attachment = self.gen_attachment_logo(_obj, prefix=obj) |
|
|
|
|
msg_attachment = self.gen_attachment_logo(_obj, prefix=section, resize=resize) |
|
|
|
|
if msg_attachment: |
|
|
|
|
attachments.append(msg_attachment) |
|
|
|
|
for section in ['moscow', 'russia', 'foreign']: |
|
|
|
|
_section = context.get(section) |
|
|
|
|
if _section: |
|
|
|
|
for event in _section: |
|
|
|
|
objects = context.get(section) |
|
|
|
|
if objects: |
|
|
|
|
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) |
|
|
|
|
msg_attachment = self.gen_attachment_logo(event.object, prefix=prefix, resize=resize) |
|
|
|
|
if msg_attachment: |
|
|
|
|
attachments.append(msg_attachment) |
|
|
|
|
return attachments |
|
|
|
|
@ -305,7 +315,7 @@ class NewsLetterSender(object): |
|
|
|
|
message_attachment.add_header('Content-ID', '<{}>'.format(cid)) |
|
|
|
|
return message_attachment |
|
|
|
|
|
|
|
|
|
def gen_attachment_logo(self, obj, prefix='logo_'): |
|
|
|
|
def gen_attachment_logo(self, obj, prefix='logo_', resize=None): |
|
|
|
|
logo = getattr(obj, 'logo') |
|
|
|
|
if not logo: |
|
|
|
|
return None |
|
|
|
|
@ -318,7 +328,12 @@ class NewsLetterSender(object): |
|
|
|
|
|
|
|
|
|
maintype, subtype = ctype.split('/', 1) |
|
|
|
|
try: |
|
|
|
|
fd = open(logo.path, 'rb') |
|
|
|
|
if resize is not None: |
|
|
|
|
fd = get_thumbnail(logo.path, resize) |
|
|
|
|
# import pdb; pdb.set_trace() |
|
|
|
|
# fd = open(thumb.path, 'rb') |
|
|
|
|
else: |
|
|
|
|
fd = open(logo.path, 'rb') |
|
|
|
|
except IOError: |
|
|
|
|
return None |
|
|
|
|
if maintype == 'image': |
|
|
|
|
|