Merge branch 'feature/subscriptions' into 'develop'

Feature/subscriptions

See merge request gm/gm-backend!228
This commit is contained in:
Олег Хаятов 2020-01-21 12:50:08 +00:00
commit 716c6b3af2
8 changed files with 241 additions and 41 deletions

View File

@ -89,11 +89,15 @@ class SignupSerializer(serializers.ModelSerializer):
user_id=obj.id,
country_code=request.country_code)
# Make subscriber
Subscriber.objects.make_subscriber(
email=obj.email, user=obj, ip_address=get_user_ip(request),
country_code=request.country_code, locale=request.locale
)
# Make subscriber or associate user
subscriber = Subscriber.objects.associate_user(user=obj)
if subscriber is None:
Subscriber.objects.make_subscriber(
email=obj.email, user=obj, ip_address=get_user_ip(request),
country_code=request.country_code, locale=request.locale
)
return obj

View File

@ -30,11 +30,17 @@ class SubscriberManager(models.Manager):
locale=None, subscription_types=None, *args, **kwargs):
"""Make subscriber and update info."""
# search existing object
if not user:
user = User.objects.filter(email=email).first()
if user:
obj = self.model.objects.filter(models.Q(user=user) | models.Q(email=user.email)).first()
obj = self.model.objects.filter(user=user).first()
if obj is None:
obj = self.model.objects.filter(email=user.email).first()
self.associate_user(user=user)
else:
obj = self.model.objects.filter(email=email).first()
@ -56,12 +62,15 @@ class SubscriberManager(models.Manager):
else:
obj = self.model.objects.create(
user=user, email=email, ip_address=ip_address,
country_code=country_code, locale=locale
country_code=country_code, locale=locale,
)
if subscription_types is not None:
obj.subscription_types.set(subscription_types)
obj.subscribe_set.update(unsubscribe_date=None)
if subscription_types is None:
subscription_types = []
obj.subscription_types.set(subscription_types)
obj.subscribe_set.update(unsubscribe_date=None)
obj.save()
return obj
@ -74,7 +83,7 @@ class SubscriberManager(models.Manager):
if obj:
obj.user = user
obj.email = None
obj.email = user.email
obj.save()
return obj
@ -127,9 +136,9 @@ class Subscriber(ProjectBaseMixin):
self.subscribe_set.update(unsubscribe_date=now())
if settings.USE_CELERY:
send_unsubscribe_email.delay(self.pk)
send_unsubscribe_email.delay(self.email)
else:
send_unsubscribe_email(self.pk)
send_unsubscribe_email(self.email)
@property
def send_to(self):
@ -140,10 +149,11 @@ class Subscriber(ProjectBaseMixin):
def link_to_unsubscribe(self):
"""Link to unsubscribe."""
schema = settings.SCHEMA_URI
site_domain = settings.SITE_DOMAIN_URI
site_domain = settings.DOMAIN_URI
url = settings.SITE_REDIRECT_URL_UNSUBSCRIBE
query = f'?code={self.update_code}'
return f'{schema}://{site_domain}{url}{query}'
country_code = '%s.' % self.country_code if self.country_code else ''
return f'{schema}://{country_code}{site_domain}{url}{query}'
@property
def active_subscriptions(self):

View File

@ -28,7 +28,7 @@ class SubscriptionTypeSerializer(serializers.ModelSerializer):
)
class CreateSubscribeSerializer(serializers.ModelSerializer):
class CreateAndUpdateSubscribeSerializer(serializers.ModelSerializer):
"""Create and Update Subscribe serializer."""
email = serializers.EmailField(required=False, source='send_to')
@ -89,20 +89,12 @@ class CreateSubscribeSerializer(serializers.ModelSerializer):
subscriber = models.Subscriber.objects.make_subscriber(**validated_data)
if settings.USE_CELERY:
send_subscribes_update_email.delay(subscriber.pk)
send_subscribes_update_email.delay(subscriber.email)
else:
send_subscribes_update_email(subscriber.pk)
send_subscribes_update_email(subscriber.email)
return subscriber
def update(self, instance, validated_data):
if settings.USE_CELERY:
send_subscribes_update_email.delay(instance.pk)
else:
send_subscribes_update_email(instance.pk)
return super().update(instance, validated_data)
class UpdateSubscribeSerializer(serializers.ModelSerializer):
"""Update with code Subscribe serializer."""

View File

@ -3,6 +3,7 @@ from datetime import datetime
from celery import shared_task
from django.conf import settings
from django.core.mail import send_mail
from django.utils.translation import gettext_lazy as _
from django.template.loader import get_template, render_to_string
from main.models import SiteSettings
@ -10,8 +11,11 @@ from notification import models
@shared_task
def send_subscribes_update_email(subscriber_id):
subscriber = models.Subscriber.objects.get(id=subscriber_id)
def send_subscribes_update_email(email):
subscriber = models.Subscriber.objects.filter(email=email).first()
if subscriber is None:
return
country_code = subscriber.country_code
@ -24,11 +28,11 @@ def send_subscribes_update_email(subscriber_id):
socials_for_subscriber = socials.get(country_code)
context = {
"title": "You have subscribed on news G&M",
"description": "<br>".join([
name.get(subscriber.locale)
"title": _("You have subscribed on news G&M"),
"description": _("<br>".join([
name.get(subscriber.locale) if subscriber.locale in name else name.get(next(iter(name.keys())))
for name in subscriber.subscription_types.values_list('name', flat=True)
]),
])),
"code": subscriber.update_code,
"link_to_unsubscribe": subscriber.link_to_unsubscribe,
"twitter_page_url": socials_for_subscriber.twitter_page_url if socials_for_subscriber else '#',
@ -39,7 +43,7 @@ def send_subscribes_update_email(subscriber_id):
}
send_mail(
subject="G&M Subscriptions",
subject=_("G&M Subscriptions"),
message=render_to_string(settings.NOTIFICATION_SUBSCRIBE_TEMPLATE, context),
from_email=settings.EMAIL_HOST_USER,
recipient_list=[subscriber.send_to],
@ -49,8 +53,11 @@ def send_subscribes_update_email(subscriber_id):
@shared_task
def send_unsubscribe_email(subscriber_id):
subscriber = models.Subscriber.objects.get(id=subscriber_id)
def send_unsubscribe_email(email):
subscriber = models.Subscriber.objects.filter(email=email).first()
if subscriber is None:
return
country_code = subscriber.country_code
@ -63,7 +70,7 @@ def send_unsubscribe_email(subscriber_id):
socials_for_subscriber = socials.get(country_code)
context = {
"title": "You have successfully unsubscribed from G&M news",
"title": _("You have successfully unsubscribed from G&M news"),
"description": "",
"code": subscriber.update_code,
"link_to_unsubscribe": subscriber.link_to_unsubscribe,

View File

@ -13,7 +13,7 @@ class CreateSubscribeView(generics.CreateAPIView):
queryset = models.Subscriber.objects.all()
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.CreateSubscribeSerializer
serializer_class = serializers.CreateAndUpdateSubscribeSerializer
class UpdateSubscribeView(generics.UpdateAPIView):

View File

@ -0,0 +1,93 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-01-21 11:28+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: project/templates/account/change_email.html:33
#, python-format
msgid ""
"You're receiving this email because you want to change email address at "
"%(site_name)s."
msgstr ""
#: project/templates/account/change_email.html:35
msgid "Please go to the following page for confirmation new email address:"
msgstr ""
#: project/templates/account/change_email.html:39
#: project/templates/account/password_change_email.html:36
#: project/templates/account/password_reset_email.html:39
#: project/templates/authorization/confirm_email.html:42
msgid "Thanks for using our site!"
msgstr ""
#: project/templates/account/change_email.html:42
#: project/templates/account/password_change_email.html:39
#: project/templates/account/password_reset_email.html:41
#: project/templates/authorization/confirm_email.html:44
#, python-format
msgid "The %(site_name)s team"
msgstr ""
#: project/templates/account/password_change_email.html:34
#, python-format
msgid ""
"You're receiving this email because your account's password address at "
"%(site_name)s."
msgstr ""
#: project/templates/account/password_reset_email.html:33
#, python-format
msgid ""
"You're receiving this email because you requested a password reset for your "
"user account at %(site_name)s."
msgstr ""
#: project/templates/account/password_reset_email.html:35
msgid "Please go to the following page and choose a new password:"
msgstr ""
#: project/templates/authorization/confirm_email.html:34
#, python-format
msgid ""
"You're receiving this email because you trying to register new account at "
"%(site_name)s."
msgstr ""
#: project/templates/authorization/confirm_email.html:36
msgid "Please confirm your email address to complete the registration:"
msgstr ""
#: project/templates/authorization/confirm_email.html:39
msgid "If you use the mobile app, enter the following code in the form:"
msgstr ""
#: project/templates/notification/update_email.html:44
msgid "Follow us"
msgstr ""
#: project/templates/notification/update_email.html:46
msgid "You can also us on our social network below"
msgstr ""
#: project/templates/notification/update_email.html:62
msgid "This email has been sent to"
msgstr ""
#: project/templates/notification/update_email.html:63
msgid "click here to unsubscribe"
msgstr ""

View File

@ -0,0 +1,93 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-01-21 11:48+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: project/templates/account/change_email.html:33
#, python-format
msgid ""
"You're receiving this email because you want to change email address at "
"%(site_name)s."
msgstr ""
#: project/templates/account/change_email.html:35
msgid "Please go to the following page for confirmation new email address:"
msgstr ""
#: project/templates/account/change_email.html:39
#: project/templates/account/password_change_email.html:36
#: project/templates/account/password_reset_email.html:39
#: project/templates/authorization/confirm_email.html:42
msgid "Thanks for using our site!"
msgstr ""
#: project/templates/account/change_email.html:42
#: project/templates/account/password_change_email.html:39
#: project/templates/account/password_reset_email.html:41
#: project/templates/authorization/confirm_email.html:44
#, python-format
msgid "The %(site_name)s team"
msgstr ""
#: project/templates/account/password_change_email.html:34
#, python-format
msgid ""
"You're receiving this email because your account's password address at "
"%(site_name)s."
msgstr ""
#: project/templates/account/password_reset_email.html:33
#, python-format
msgid ""
"You're receiving this email because you requested a password reset for your "
"user account at %(site_name)s."
msgstr ""
#: project/templates/account/password_reset_email.html:35
msgid "Please go to the following page and choose a new password:"
msgstr ""
#: project/templates/authorization/confirm_email.html:34
#, python-format
msgid ""
"You're receiving this email because you trying to register new account at "
"%(site_name)s."
msgstr ""
#: project/templates/authorization/confirm_email.html:36
msgid "Please confirm your email address to complete the registration:"
msgstr ""
#: project/templates/authorization/confirm_email.html:39
msgid "If you use the mobile app, enter the following code in the form:"
msgstr ""
#: project/templates/notification/update_email.html:44
msgid "Follow us"
msgstr ""
#: project/templates/notification/update_email.html:46
msgid "You can also us on our social network below"
msgstr ""
#: project/templates/notification/update_email.html:62
msgid "This email has been sent to"
msgstr ""
#: project/templates/notification/update_email.html:63
msgid "click here to unsubscribe"
msgstr ""

View File

@ -1,4 +1,5 @@
<!DOCTYPE html>
{% load i18n %}
<html lang="en" style="box-sizing: border-box;margin: 0;">
<head>
<meta charset="UTF-8">
@ -40,9 +41,9 @@
<div class="letter__follow-content" style="padding: 1.25rem 0;background: #fff;text-align: center;">
<div class="letter__follow-header" style="display: inline-block;margin: 0 0 18px;font-family: &quot;PT Serif&quot;, sans-serif;font-size: 1.25rem;text-transform: uppercase;">
<img alt="thumb" style="width:30px;vertical-align: sub; display: inline-block" src="https://s3.eu-central-1.amazonaws.com/gm-test.com/manually_uploaded/2.png" />
<span class="letter__follow-title">Follow us</span>
<span class="letter__follow-title">{% trans "Follow us" %}</span>
</div>
<div class="letter__follow-text" style="display: block;margin: 0 0 30px;font-size: 12px;font-style: italic;">You can also us on our social network below
<div class="letter__follow-text" style="display: block;margin: 0 0 30px;font-size: 12px;font-style: italic;">{% trans "You can also us on our social network below" %}
</div>
<div class="letter__follow-social">
<a href="{{ facebook_page_url }}" class="letter__follow-link" target="_blank" style="color: #000;font-weight: 700;text-decoration: none;padding: 0;border-bottom: 1.5px solid #ffee29;cursor: pointer;display: inline-block;width: 30px;height: 30px;margin: 0 1rem 0 0;background: #ffee29;border: none;">
@ -58,8 +59,8 @@
</div>
</div>
<div class="letter__unsubscribe" style="margin: 0 0 1.25rem;font-size: 12px;text-align: center;">
<span class="letter__unsubscribe-dscr" style="display: inline-block;">This email has been sent to {{ send_to }} ,</span>
<a href="{{ link_to_unsubscribe }}" target="_blank" style="color: #000;font-weight: 700;text-decoration: none;padding: 0;border-bottom: 1.5px solid #ffee29;cursor: pointer;">click here to unsubscribe</a>
<span class="letter__unsubscribe-dscr" style="display: inline-block;">{% trans "This email has been sent to" %} {{ send_to }} ,</span>
<a href="{{ link_to_unsubscribe }}" target="_blank" style="color: #000;font-weight: 700;text-decoration: none;padding: 0;border-bottom: 1.5px solid #ffee29;cursor: pointer;">{% trans "click here to unsubscribe" %}</a>
</div>
<div class="letter__footer" style="padding: 24px 0 15px;text-align: center;background: #ffee29;">
<div class="letter__footer-logo" style="width: 71px;height: 42px;margin: 0 auto 14px auto;">