diff --git a/apps/notification/models.py b/apps/notification/models.py index 6dbf0230..c7d277d8 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -9,7 +9,7 @@ from account.models import User from location.models import Country from utils.methods import generate_string_code from utils.models import ProjectBaseMixin, TJSONField, TranslatedFieldsMixin -from notification.tasks import send_subscribes_update_email, send_unsubscribe_email +from notification.tasks import send_unsubscribe_email class SubscriptionType(ProjectBaseMixin, TranslatedFieldsMixin): @@ -101,8 +101,6 @@ class Subscriber(ProjectBaseMixin): objects = SubscriberManager() - # todo: Add sending subscribes with update email - class Meta: """Meta class.""" diff --git a/apps/notification/serializers/common.py b/apps/notification/serializers/common.py index e1e69991..88585da7 100644 --- a/apps/notification/serializers/common.py +++ b/apps/notification/serializers/common.py @@ -1,10 +1,11 @@ """Notification app serializers.""" - +from django.conf import settings from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers from location.serializers import CountrySimpleSerializer from notification import models +from notification.tasks import send_subscribes_update_email from utils.methods import get_user_ip from utils.serializers import TranslatedField @@ -74,7 +75,23 @@ class CreateSubscribeSerializer(serializers.ModelSerializer): def create(self, validated_data): """Create obj.""" - return models.Subscriber.objects.make_subscriber(**validated_data) + + subscriber = models.Subscriber.objects.make_subscriber(**validated_data) + + if settings.USE_CELERY: + send_subscribes_update_email.delay(subscriber.pk) + else: + send_subscribes_update_email(subscriber.pk) + + 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 SubscribeObjectSerializer(serializers.ModelSerializer): diff --git a/apps/notification/tasks.py b/apps/notification/tasks.py index 4a0ff5f3..f2e4ccc3 100644 --- a/apps/notification/tasks.py +++ b/apps/notification/tasks.py @@ -6,12 +6,12 @@ from django.core.mail import send_mail from django.template.loader import get_template, render_to_string from main.models import SiteSettings -from notification.models import Subscriber +from notification import models @shared_task def send_subscribes_update_email(subscriber_id): - subscriber = Subscriber.objects.get(subscriber_id) + subscriber = models.Subscriber.objects.get(id=subscriber_id) country_code = subscriber.country_code @@ -24,9 +24,8 @@ def send_subscribes_update_email(subscriber_id): socials_for_subscriber = socials.get(country_code) context = { - "title": "G&M Subscriptions", - "subtitle": "You have subscribed on news G&M", - "description": "\n".join([ + "title": "You have subscribed on news G&M", + "description": "
".join([ name.get(subscriber.locale) for name in subscriber.subscription_types.values_list('name', flat=True) ]), @@ -52,7 +51,7 @@ def send_subscribes_update_email(subscriber_id): @shared_task def send_unsubscribe_email(subscriber_id): - subscriber = Subscriber.objects.get(subscriber_id) + subscriber = models.Subscriber.objects.get(id=subscriber_id) country_code = subscriber.country_code @@ -65,8 +64,7 @@ def send_unsubscribe_email(subscriber_id): socials_for_subscriber = socials.get(country_code) context = { - "title": "G&M Subscriptions", - "subtitle": "You have successfully unsubscribed from G&M news", + "title": "You have successfully unsubscribed from G&M news", "description": "", "code": subscriber.update_code, "domain_uri": settings.DOMAIN_URI,