89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
from datetime import datetime
|
|
|
|
from celery import shared_task
|
|
from django.conf import settings
|
|
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
|
|
|
|
|
|
@shared_task
|
|
def send_subscribes_update_email(subscriber_id):
|
|
subscriber = Subscriber.objects.get(subscriber_id)
|
|
|
|
country_code = subscriber.country_code
|
|
|
|
html_template = get_template(settings.NEWS_EMAIL_TEMPLATE)
|
|
year = datetime.now().year
|
|
|
|
socials = list(SiteSettings.objects.with_country().select_related('country'))
|
|
socials = dict(zip(map(lambda social: social.country.code, socials), socials))
|
|
|
|
socials_for_subscriber = socials.get(country_code)
|
|
|
|
context = {
|
|
"title": "G&M Subscriptions",
|
|
"subtitle": "You have subscribed on news G&M",
|
|
"description": "\n".join([
|
|
name.get(subscriber.locale)
|
|
for name in subscriber.subscription_types.values_list('name', flat=True)
|
|
]),
|
|
"code": subscriber.update_code,
|
|
"domain_uri": settings.DOMAIN_URI,
|
|
"country_code": subscriber.country_code,
|
|
"twitter_page_url": socials_for_subscriber.twitter_page_url if socials_for_subscriber else '#',
|
|
"instagram_page_url": socials_for_subscriber.instagram_page_url if socials_for_subscriber else '#',
|
|
"facebook_page_url": socials_for_subscriber.facebook_page_url if socials_for_subscriber else '#',
|
|
"send_to": subscriber.send_to,
|
|
"year": year
|
|
}
|
|
|
|
send_mail(
|
|
subject="G&M Subscriptions",
|
|
message=render_to_string(settings.NOTIFICATION_SUBSCRIBE_TEMPLATE, context),
|
|
from_email=settings.EMAIL_HOST_USER,
|
|
recipient_list=[subscriber.send_to],
|
|
fail_silently=False,
|
|
html_message=html_template.render(context)
|
|
)
|
|
|
|
|
|
@shared_task
|
|
def send_unsubscribe_email(subscriber_id):
|
|
subscriber = Subscriber.objects.get(subscriber_id)
|
|
|
|
country_code = subscriber.country_code
|
|
|
|
html_template = get_template(settings.NEWS_EMAIL_TEMPLATE)
|
|
year = datetime.now().year
|
|
|
|
socials = list(SiteSettings.objects.with_country().select_related('country'))
|
|
socials = dict(zip(map(lambda social: social.country.code, socials), socials))
|
|
|
|
socials_for_subscriber = socials.get(country_code)
|
|
|
|
context = {
|
|
"title": "G&M Subscriptions",
|
|
"subtitle": "You have successfully unsubscribed from G&M news",
|
|
"description": "",
|
|
"code": subscriber.update_code,
|
|
"domain_uri": settings.DOMAIN_URI,
|
|
"country_code": subscriber.country_code,
|
|
"twitter_page_url": socials_for_subscriber.twitter_page_url if socials_for_subscriber else '#',
|
|
"instagram_page_url": socials_for_subscriber.instagram_page_url if socials_for_subscriber else '#',
|
|
"facebook_page_url": socials_for_subscriber.facebook_page_url if socials_for_subscriber else '#',
|
|
"send_to": subscriber.send_to,
|
|
"year": year
|
|
}
|
|
|
|
send_mail(
|
|
subject="G&M Subscriptions",
|
|
message=render_to_string(settings.NOTIFICATION_SUBSCRIBE_TEMPLATE, context),
|
|
from_email=settings.EMAIL_HOST_USER,
|
|
recipient_list=[subscriber.send_to],
|
|
fail_silently=False,
|
|
html_message=html_template.render(context)
|
|
)
|