From f01418ce2333402576d8abfbfe9d48ab9befcddd Mon Sep 17 00:00:00 2001 From: Alexander Burdeiny Date: Tue, 11 Oct 2016 02:55:40 +0300 Subject: [PATCH] 1625, 1577, 1581 --- core/simple_index_view.py | 2 +- emencia/django/newsletter/admin_forms.py | 2 +- emencia/django/newsletter/forms.py | 1 + emencia/django/newsletter/mailer.py | 85 ++- emencia/django/newsletter/models.py | 17 +- .../templates/newsletter/AutomaticEmail.html | 61 +- .../newsletter/AutomaticEmail_test.html | 4 +- .../newsletter/AutomaticEmail_web.html | 582 ++++++++++++++++++ functions/views_help.py | 15 +- proj/views.py | 4 + .../images/{s3.png => facebook.png} | Bin .../images/{s1.png => instagram.png} | Bin .../images/{s4.png => linkedin.png} | Bin .../newsletter/images/{s6.png => twitter.png} | Bin static/newsletter/images/{s5.png => vk.png} | Bin .../newsletter/images/{s2.png => youtube.png} | Bin .../client/popups/announce_subscription.html | 4 +- .../simple_pages/participation_landing.html | 2 +- 18 files changed, 738 insertions(+), 41 deletions(-) create mode 100644 emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html rename static/newsletter/images/{s3.png => facebook.png} (100%) rename static/newsletter/images/{s1.png => instagram.png} (100%) rename static/newsletter/images/{s4.png => linkedin.png} (100%) rename static/newsletter/images/{s6.png => twitter.png} (100%) rename static/newsletter/images/{s5.png => vk.png} (100%) rename static/newsletter/images/{s2.png => youtube.png} (100%) 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 @@
- + - +
@@ -84,16 +84,19 @@
+ + {% comment %} {% thumbnail obj.get_logo '281x225' as im %} {% endthumbnail %} + {% endcomment %} @@ -104,7 +107,7 @@ @@ -153,9 +156,12 @@
- + {{ obj.get_event_place_name }}
- + {{ obj.get_dates }}, {{ obj.get_event_place_name }}
+ + {% comment %} {% thumbnail obj.get_logo '109x114' as im %} {% endthumbnail %} + {% endcomment %} @@ -169,7 +175,7 @@ @@ -177,7 +183,7 @@ @@ -223,16 +229,19 @@
- + {{ obj.get_dates }}
{% if obj.get_event_place_name %} - + {{ obj.get_event_place_name }} {% endif %}
+ + {% comment %} {% thumbnail obj.get_logo '109x114' as im %} {% endthumbnail %} + {% endcomment %} @@ -248,7 +257,7 @@ @@ -296,15 +305,18 @@
{% if obj.get_event_place_name %} - + {{ obj.get_event_place_name }} {% endif %}
- + {{ obj.get_dates }}
@@ -314,7 +326,7 @@ @@ -343,7 +355,7 @@ {% endif %} -
+ + {% comment %} {% thumbnail obj.get_logo '109x114' as im %} {% endthumbnail %} + {% endcomment %}
{% if obj.get_event_place_name %} - + {{ obj.get_event_place_name }} {% endif %}
- + {{ obj.get_dates }}
+
@@ -367,7 +379,7 @@
{% trans "Команда" %}
- +
@@ -387,10 +399,14 @@ @@ -435,9 +451,12 @@
+ + {% comment %} {% thumbnail news.logo "272x195" as im %} {% endthumbnail %} - + + {% endcomment %} + @@ -400,7 +416,7 @@
- + {# С 1 по 4 сентября #}
+ + {% comment %} {% thumbnail blog.logo "272x195" as im %} {% endthumbnail %} + {% endcomment %} @@ -473,12 +492,12 @@ - - - - - - + + + + + +
@@ -511,7 +530,7 @@
- + diff --git a/emencia/django/newsletter/templates/newsletter/AutomaticEmail_test.html b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_test.html index 0824f732..50d52746 100644 --- a/emencia/django/newsletter/templates/newsletter/AutomaticEmail_test.html +++ b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_test.html @@ -15,10 +15,10 @@ {% csrf_token %} {{ form.as_p }} - + {% if contact %} - {% include "newsletter/AutomaticEmail.html" %} + {% include "newsletter/AutomaticEmail_web.html" %} {% endif %} diff --git a/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html new file mode 100644 index 00000000..d5eeb553 --- /dev/null +++ b/emencia/django/newsletter/templates/newsletter/AutomaticEmail_web.html @@ -0,0 +1,582 @@ +{% load i18n %} +{% load static %} +{% load thumbnail %} + +{# {% trans "ПОСМОТРЕТЬ ВСЕ" %} > #} +{# {% trans "ПОСМОТРЕТЬ ВСЕ" %} > #} +{# {% trans "ПОСМОТРЕТЬ ВСЕ" %} > #} +{# {% trans "НАСТРОИТЬ" %} > #} + + + + + +
+ + + + + + + + +
 
+ + + + + + +
+ + + + + + +
+ + + + + + +
+ + + +
+
+ + {% if recommended %} + {% with recommended.object as obj %} + + + + + +
+ + + + +
{% trans "Самые важные события для вас" %}
+ + + + + + + +
+ {% thumbnail obj.get_logo '281x225' as im %} + + {% endthumbnail %} + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + {{ obj.get_event_place_name }} +
{{ obj.name|safe }}
+ + {{ obj.get_dates }}, {{ obj.get_event_place_name }} +
+ {{ obj.main_title|safe }} +
+ {% for tag in obj.tags %} + {{ tag.name }}{% if not forloop.last %},{% endif %} + {% endfor %} +
+ {% trans "посмотреть Подробнее" %} +
+
+
+ {% endwith %} + {% endif %} + + {% if moscow %} + + + + + +
+ + + + +
{% trans "в москве" %}
+ {% for sqs_obj in moscow %} + {% with sqs_obj.object as obj %} + + + + + +
+ {% thumbnail obj.get_logo '109x114' as im %} + + {% endthumbnail %} + + + + + + + + + + + + + + + + + + + + + +
{{ obj.name|safe }}
{{ obj.main_title|safe }}
+ + {{ obj.get_dates }} +
+ {% if obj.get_event_place_name %} + + {{ obj.get_event_place_name }} + {% endif %} +
+ {% trans "подробнее" %} +
+
+ {% endwith %} + {% endfor %} + + + + +
+ {% trans "посмотреть все" %} +
+
+ {% endif %} + + + + {% if russia %} + + + + + +
+ + + + +
{% trans "В России" %}
+ {% for sqs_obj in russia %} + {% with sqs_obj.object as obj %} + + + + + +
+ {% thumbnail obj.get_logo '109x114' as im %} + + {% endthumbnail %} + + + + + + + + + + + + + + + + + + + + + +
+ {% if obj.get_event_place_name %} + + {{ obj.get_event_place_name }} + {% endif %} +
{{ obj.name|safe }}
{{ obj.main_title|safe }}
+ + {{ obj.get_dates }} +
+ {% trans "подробнее" %} +
+
+ {% endwith %} + {% endfor %} + + + + + +
+ {% trans "все события" %} +
+
+ {% endif %} + + + + {% if foreign %} + + + + + + + + + + + + + +
{% trans "За рубежом" %}
+ + + {% for sqs_obj in foreign %} + {% with sqs_obj.object as obj %} + + {% endwith %} + {% endfor %} + +
+ + + + + + + + + + + + + + + + +
+ {% thumbnail obj.get_logo '109x114' as im %} + + {% endthumbnail %} +
+ {% if obj.get_event_place_name %} + + {{ obj.get_event_place_name }} + {% endif %} +
{{ obj.name|safe }}
+ + {{ obj.get_dates }} +
+ {% for tag in obj.tags %} + {{ tag.name }}{% if not forloop.last %},{% endif %} + {% endfor %} +
+
+
+ {% trans "Посмотреть все" %} +
+ {% endif %} + + + + + + + + +
+ + + + + + + + + + + + +
{% trans "Снимаем всю гололвную боль" %} +
{% blocktrans %}По организации участия в зарубежной
выставке{% endblocktrans %}
{% trans "Отдыхайте, пока мы работаем!" %}
+
+ + + + +
+ {% trans "Команда" %}
+ +
+
+ + {% if news %} + + + + + + + + + +
{% trans "Новости событий" %}
+ + + + + + +
+ {% thumbnail news.logo "272x195" as im %} + + {% endthumbnail %} + + + + + + + + + + + + + + + +
{{ news.main_title|safe }}
+ + {# С 1 по 4 сентября #} +
{{ news.preview|safe }}
+ {% trans "Все новости" %} +
+
+
+ {% endif %} + +
+ + {% if blog %} + + + + + + + + + +
{% trans "Новенькое из блога" %}
+ + + + + + +
+ {% thumbnail blog.logo "272x195" as im %} + + {% endthumbnail %} + + + + + + + + + + + + + +
{{ blog.main_title|safe }}
{{ blog.preview|safe }}
+ {% trans "посмотреть Подробнее" %} +
+
+
+ {% endif %} + + + + + + + + + +
+ {% trans "Следуйте за нами" %}: + + + + + + + +
+ + + + + + + + + + + + + + + +
{% trans "Управление подпиской:" %}
{% trans "Вы можете настроить включение различных блоков информации в свое письмо, выбратьфильтры по темам, странам и городам, а также выбрать комфортную периодичность получения писем." %}
+ {% trans "Настроить" %} +
+ + + + + + + +
+ + + + + + +
+ + + + + + + + +
{% trans "Хорошего Вам дня!" %}
+
+ + + + + + + + +
+ {% trans "Поисковик деловых событий" %}
+ expomap.ru
+ mail@expomap.ru +
+ {% trans "По вопросам участия в выставках звоните" %}:
+ +7 (499) 999 -12-07 +
+
+
+ + + + + + + + + + + +
{% trans "Выставки" %}{% trans "Конференции" %}{% trans "Участие в выставках" %}{% trans "Новости" %}{% trans "Статьи" %}
+ + + + + + + + + + + + + +
{% trans "Вы получили это письмо, так как подписаны на рассылку" %} https://www.expomap.ru
{% trans "Переслать другу" %} {% trans "или" %} {% trans "Отписаться" %}
© 2008 — 2016 Expomap.ru
+ + +
+ +
diff --git a/functions/views_help.py b/functions/views_help.py index 35e36cad..4697b12e 100644 --- a/functions/views_help.py +++ b/functions/views_help.py @@ -1,8 +1,13 @@ # -*- coding: utf-8 -*- import re import datetime + from django.shortcuts import get_object_or_404 +from django.core.cache import cache + from accounts.models import User +from emencia.django.newsletter.models import Contact + def get_referer(request, default=None): referer = request.META.get('HTTP_REFERER') @@ -38,4 +43,12 @@ def get_user(url): user = get_object_or_404(User, id=url) except ValueError: user = get_object_or_404(User, url=url) - return user \ No newline at end of file + return user + +def get_subscribers_count(): + key = 'contact_count' + count = cache.get(key) + if count is None: + count = Contact.objects.valid_subscribers().filter(from_users=False).count() + cache.set(key, count, datetime.timedelta(days=1).total_seconds()) + return count diff --git a/proj/views.py b/proj/views.py index afca73ad..c8af000b 100644 --- a/proj/views.py +++ b/proj/views.py @@ -4,9 +4,12 @@ from django.template import RequestContext from django.views.generic import TemplateView from django.conf import settings from django.utils.translation import get_language + from functions.cache_mixin import JitterCacheMixin from functions.forms import ThemeSearch from functions.search_forms import ExpositionSearchForm +from functions.views_help import get_subscribers_count + from accounts.forms import RegistrationCompleteForm, SocialRegistrationCompleteForm from meta.models import SeoText from theme.models import Theme @@ -46,6 +49,7 @@ def expo_context(request): 'seo_text': add_seo(request), 'announce_subscribe': SubscribeAssideForm(), 'NO_EXTERNAL_JS': getattr(settings, 'NO_EXTERNAL_JS', False), 'NO_BANNERS': getattr(settings, 'NO_BANNERS', False), + 'SUBSCRIBERS_COUNT': get_subscribers_count(), } user = request.user diff --git a/static/newsletter/images/s3.png b/static/newsletter/images/facebook.png similarity index 100% rename from static/newsletter/images/s3.png rename to static/newsletter/images/facebook.png diff --git a/static/newsletter/images/s1.png b/static/newsletter/images/instagram.png similarity index 100% rename from static/newsletter/images/s1.png rename to static/newsletter/images/instagram.png diff --git a/static/newsletter/images/s4.png b/static/newsletter/images/linkedin.png similarity index 100% rename from static/newsletter/images/s4.png rename to static/newsletter/images/linkedin.png diff --git a/static/newsletter/images/s6.png b/static/newsletter/images/twitter.png similarity index 100% rename from static/newsletter/images/s6.png rename to static/newsletter/images/twitter.png diff --git a/static/newsletter/images/s5.png b/static/newsletter/images/vk.png similarity index 100% rename from static/newsletter/images/s5.png rename to static/newsletter/images/vk.png diff --git a/static/newsletter/images/s2.png b/static/newsletter/images/youtube.png similarity index 100% rename from static/newsletter/images/s2.png rename to static/newsletter/images/youtube.png diff --git a/templates/client/popups/announce_subscription.html b/templates/client/popups/announce_subscription.html index 76286e2f..bc4de38c 100644 --- a/templates/client/popups/announce_subscription.html +++ b/templates/client/popups/announce_subscription.html @@ -18,9 +18,7 @@
-

- {% trans 'Более 40 000 профессионалов получают наши анонсы событий каждую среду. Присоединяйтесь!' %} -

+

{% blocktrans with count=SUBSCRIBERS_COUNT %}Более {{ count }} профессионалов получают наши анонсы событий каждую среду. Присоединяйтесь!{% endblocktrans %}

diff --git a/templates/client/simple_pages/participation_landing.html b/templates/client/simple_pages/participation_landing.html index b1cea9d5..df54b342 100644 --- a/templates/client/simple_pages/participation_landing.html +++ b/templates/client/simple_pages/participation_landing.html @@ -314,7 +314,7 @@
-

{% blocktrans with specialist_count=specialist_count %}Нас читают уже {{ specialist_count }} специалиста!{% endblocktrans %}

+

{% blocktrans with count=SUBSCRIBERS_COUNT %}Нас читают уже {{ count }} специалиста!{% endblocktrans %}