diff --git a/core/simple_index_view.py b/core/simple_index_view.py index 17a11b05..d5de62ab 100644 --- a/core/simple_index_view.py +++ b/core/simple_index_view.py @@ -56,7 +56,7 @@ class ParticipationViewLanding(JitterCacheMixin, MetadataMixin, FormView): context = super(ParticipationViewLanding, self).get_context_data(**kwargs) context['comments'] = ParticipationComment.objects.language().all() context['articles'] = Article.objects.blogs().filter(theme__url='praktikum-eksponenta')[:4] - context['specialist_count'] = Contact.objects.all().count() + # context['specialist_count'] = Contact.objects.all().count() return context diff --git a/emencia/django/newsletter/admin_forms.py b/emencia/django/newsletter/admin_forms.py index 5cce8731..7ab7a372 100644 --- a/emencia/django/newsletter/admin_forms.py +++ b/emencia/django/newsletter/admin_forms.py @@ -194,7 +194,7 @@ class MailingListForm(forms.ModelForm): class NewsletterForm(forms.ModelForm): test_contacts = forms.ModelMultipleChoiceField(label=_(u'Тестовые контакты'), required=False, queryset=Contact.objects.filter(tester=True)) - content = forms.CharField(label=_('content'), widget=CKEditorWidget(config_name='newsletters')) + content = forms.CharField(label=_('content'), widget=CKEditorWidget(config_name='newsletters'), required=False) content2 = forms.CharField(label=_('Content B'), widget=CKEditorWidget(config_name='newsletters'), required=False) class Meta: diff --git a/emencia/django/newsletter/forms.py b/emencia/django/newsletter/forms.py index 73c753dc..fe43b0c1 100644 --- a/emencia/django/newsletter/forms.py +++ b/emencia/django/newsletter/forms.py @@ -175,6 +175,7 @@ class MailingSettingsForm(forms.ModelForm): obj.r_cities = self.cleaned_data.get('r_cities') or [] obj.tags = self.cleaned_data.get('tg') or [] obj.themes = self.cleaned_data.get('th') or [] + obj.from_users = False obj.save() return obj diff --git a/emencia/django/newsletter/mailer.py b/emencia/django/newsletter/mailer.py index 8f2c35db..e7b4ba89 100644 --- a/emencia/django/newsletter/mailer.py +++ b/emencia/django/newsletter/mailer.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Mailer for emencia.django.newsletter""" +import os import re import sys import time @@ -44,6 +45,10 @@ from django.utils.translation import ugettext as _ from django.core.urlresolvers import reverse from django.core.exceptions import SuspiciousOperation from django.db.models import Q +from django.contrib.staticfiles import finders +from django.conf import settings + +from sorl.thumbnail import get_thumbnail from emencia.django.newsletter.models import Newsletter from emencia.django.newsletter.models import ContactMailingStatus @@ -97,6 +102,26 @@ def decodestring(instring): quopri.decode(StringIO(instring), outfile) return outfile.getvalue() + +dailymail_attahcments = { + 'logo1': 'newsletter/images/logo1.png', + 'logo2': 'newsletter/images/logo2.png', + 'marker': 'newsletter/images/marker.png', + 'calendar': 'newsletter/images/calendar.png', + 'm1': 'newsletter/images/m1.png', + 'expo': 'newsletter/images/expo.png', + 'news2': 'newsletter/images/news2.jpg', + 'b': 'newsletter/images/b.png', + 'site_logo': 'newsletter/images/site_logo.png', + 'instagram': 'newsletter/images/instagram.png', + 'youtube': 'newsletter/images/youtube.png', + 'facebook': 'newsletter/images/facebook.png', + 'linkedin': 'newsletter/images/linkedin.png', + 'vk': 'newsletter/images/vk.png', + 'twitter': 'newsletter/images/twitter.png', +} + + class NewsLetterSender(object): def __init__(self, newsletter, test=False, verbose=0): @@ -151,13 +176,12 @@ class NewsLetterSender(object): message_alt.attach(html) message.attach(message_alt) - for attachment in self.attachments: message.attach(attachment) - if announce_context: + if self.announce and announce_context: # add announce attachments - announce_attachments = self.build_announce_attachments(announce_context) + announce_attachments = self.build_daily_ctx_attachments(announce_context) for attachment in announce_attachments: message.attach(attachment) @@ -205,6 +229,30 @@ class NewsLetterSender(object): return attachments + def build_daily_ctx_attachments(self, context): + attachments = [] + for obj in ['recommended', 'news', 'blog']: + _obj = context.get(obj) + if _obj: + msg_attachment = self.gen_attachment_logo(_obj, prefix=obj) + if msg_attachment: + attachments.append(msg_attachment) + for section in ['moscow', 'russia', 'foreign']: + _section = context.get(section) + if _section: + for event in _section: + prefix = '{}_{}_'.format(_section, event.object.event_type) + msg_attachment = self.gen_attachment_logo(event.object, prefix) + if msg_attachment: + attachments.append(msg_attachment) + return attachments + + def build_daily_attachments(self): + attachments = [] + for cid, path in dailymail_attahcments.iteritems(): + attachments.append(self.gen_attachment_by_path(path, cid)) + return attachments + def build_announce_attachments(self, context): # todo: move hardcoded prefixes to setting (uses in templates) conf = context.get('conf', []) @@ -231,6 +279,29 @@ class NewsLetterSender(object): return attachments + def gen_attachment_by_path(self, path, cid): + try: + ctype, encoding = mimetypes.guess_type(path) + except SuspiciousOperation as e: + return None + if ctype is None or encoding is not None: + ctype = 'application/octet-stream' + maintype, subtype = ctype.split('/', 1) + try: + fd = open(os.path.join(settings.MEDIA_ROOT, path), 'rb') + if maintype == 'image': + message_attachment = MIMEImage(fd.read(), _subtype=subtype) + else: + message_attachment = MIMEBase(maintype, subtype) + message_attachment.set_payload(fd.read()) + encode_base64(message_attachment) + except IOError as e: + return None + else: + fd.close() + message_attachment.add_header('Content-ID', '<{}>'.format(cid)) + return message_attachment + def gen_attachment_logo(self, obj, prefix='logo_'): logo = getattr(obj, 'logo') if not logo: @@ -460,7 +531,10 @@ class Mailer(NewsLetterSender): if not self.smtp: self.smtp_connect() - self.attachments = self.build_attachments() + if self.newsletter.dailymail: + self.attachments = self.build_daily_attachments() + else: + self.attachments = self.build_attachments() expedition_list = self.expedition_list @@ -491,6 +565,7 @@ class Mailer(NewsLetterSender): message.as_string()) except (Exception, ) as e: exception = e + print(exception) log.info(u'Exception was raised while sending to {email}: {exception}'.format( email=contact.email, exception=exception)) else: @@ -531,7 +606,7 @@ class Mailer(NewsLetterSender): if self.credits <= 0: return [] - if self.newsletter.dailymail == True and not self.test: + if self.newsletter.dailymail and not self.test: return self.newsletter.get_dailymail_subscribers() qs = super(Mailer, self).expedition_list diff --git a/emencia/django/newsletter/models.py b/emencia/django/newsletter/models.py index 66678173..c31612d9 100644 --- a/emencia/django/newsletter/models.py +++ b/emencia/django/newsletter/models.py @@ -290,12 +290,15 @@ class Contact(models.Model): areas_with_country.add(area) f_countries.update(map(lambda x: x[0], group)) full_areas = areas.difference(areas_with_country) - + params_list = [] + if f_countries: + params_list.append(SQ(country_id__in=f_countries)) + if full_areas: + params_list.append(SQ(area_id__in=full_areas)) foreign_sqs = SearchQuerySet().models(Exposition, Conference)\ - .filter( - SQ(country_id__in=f_countries) |\ - SQ(area_id__in=full_areas), - data_begin__gte=date) + .filter(data_begin__gte=date) + if params_list: + foreign_sqs = foreign_sqs.filter(reduce(operator.or_, params_list)) if th_tg_filter is not None: foreign_sqs = foreign_sqs.filter(th_tg_filter) ctx['foreign'] = foreign_sqs.order_by('data_begin')[:4] @@ -685,9 +688,11 @@ class Newsletter(models.Model): periodic=Contact.PERIODIC_CHOICES.MONTH, last_mailing_date=self.sending_date - month, )) + periodic_filter.append(Q(last_mailing_date__isnull=True)) qs = Contact.objects.subscribers().filter( reduce(operator.or_, periodic_filter), - periodic_day=self.sending_date.isoweekday()) + periodic_day=self.sending_date.isoweekday(), + from_users=False) return qs diff --git a/emencia/django/newsletter/templates/newsletter/AutomaticEmail.html b/emencia/django/newsletter/templates/newsletter/AutomaticEmail.html index 2d5ee6ad..4c79ae50 100644 --- a/emencia/django/newsletter/templates/newsletter/AutomaticEmail.html +++ b/emencia/django/newsletter/templates/newsletter/AutomaticEmail.html @@ -57,11 +57,11 @@
-
+ |
-
+ |