diff --git a/apps/authorization/serializers/common.py b/apps/authorization/serializers/common.py index 8f5bfc57..8249e062 100644 --- a/apps/authorization/serializers/common.py +++ b/apps/authorization/serializers/common.py @@ -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 diff --git a/apps/notification/models.py b/apps/notification/models.py index 48362894..e0855c62 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -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): diff --git a/apps/notification/serializers/common.py b/apps/notification/serializers/common.py index 4acb9deb..869879cb 100644 --- a/apps/notification/serializers/common.py +++ b/apps/notification/serializers/common.py @@ -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.""" diff --git a/apps/notification/tasks.py b/apps/notification/tasks.py index 38b11cd5..790c9650 100644 --- a/apps/notification/tasks.py +++ b/apps/notification/tasks.py @@ -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": "".join([ - name.get(subscriber.locale) + "title": _("You have subscribed on news G&M"), + "description": _("".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, diff --git a/apps/notification/views/common.py b/apps/notification/views/common.py index 9830683d..b6584394 100644 --- a/apps/notification/views/common.py +++ b/apps/notification/views/common.py @@ -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): diff --git a/project/locale/de/LC_MESSAGES/django.po b/project/locale/de/LC_MESSAGES/django.po new file mode 100644 index 00000000..6aaffc76 --- /dev/null +++ b/project/locale/de/LC_MESSAGES/django.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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 "" diff --git a/project/locale/fr/LC_MESSAGES/django.po b/project/locale/fr/LC_MESSAGES/django.po new file mode 100644 index 00000000..7951deec --- /dev/null +++ b/project/locale/fr/LC_MESSAGES/django.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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 "" diff --git a/project/templates/notification/update_email.html b/project/templates/notification/update_email.html index 0b4444c2..dd39f790 100644 --- a/project/templates/notification/update_email.html +++ b/project/templates/notification/update_email.html @@ -1,4 +1,5 @@ +{% load i18n %} @@ -40,9 +41,9 @@ - Follow us + {% trans "Follow us" %} - You can also us on our social network below + {% trans "You can also us on our social network below" %} @@ -58,8 +59,8 @@ - This email has been sent to {{ send_to }} , - click here to unsubscribe + {% trans "This email has been sent to" %} {{ send_to }} , + {% trans "click here to unsubscribe" %}