Reformat sending mail structure
This commit is contained in:
parent
241012da42
commit
140cc6cfa5
|
|
@ -9,12 +9,14 @@ from django.template.loader import get_template, render_to_string
|
||||||
|
|
||||||
from main.models import SiteSettings
|
from main.models import SiteSettings
|
||||||
from news import models
|
from news import models
|
||||||
from notification.models import Subscriber
|
from notification.models import Subscribe
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def send_email_with_news(news_ids):
|
def send_email_with_news(news_ids):
|
||||||
subscribers = Subscriber.objects.all()
|
subscribes = Subscribe.objects.all() \
|
||||||
|
.prefetch_related('subscriber') \
|
||||||
|
.prefetch_related('subscription_type')
|
||||||
sent_news = models.News.objects.filter(id__in=news_ids)
|
sent_news = models.News.objects.filter(id__in=news_ids)
|
||||||
|
|
||||||
htmly = get_template(settings.NEWS_EMAIL_TEMPLATE)
|
htmly = get_template(settings.NEWS_EMAIL_TEMPLATE)
|
||||||
|
|
@ -23,11 +25,16 @@ def send_email_with_news(news_ids):
|
||||||
socials = list(SiteSettings.objects.with_country().select_related('country'))
|
socials = list(SiteSettings.objects.with_country().select_related('country'))
|
||||||
socials = dict(zip(map(lambda social: social.country.code, socials), socials))
|
socials = dict(zip(map(lambda social: social.country.code, socials), socials))
|
||||||
|
|
||||||
for subscriber in subscribers:
|
for subscribe in subscribes.filter(unsubscribe_date=None):
|
||||||
socials_for_subscriber = socials.get(subscriber.country_code)
|
country = subscribe.subscription_type.country
|
||||||
|
|
||||||
|
if country is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
socials_for_subscriber = socials.get(country.code)
|
||||||
|
subscriber = subscribe.subscriber
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# TODO: вот тут надо учесть, подписки на какие страны есть у юзера активные (нулл время отписки) и не посылать лишнего
|
|
||||||
# TODO: обрати внимание на кол-во запросов в БД плс. они пишутся в консоль
|
|
||||||
for new in sent_news:
|
for new in sent_news:
|
||||||
context = {
|
context = {
|
||||||
"title": new.title.get(subscriber.locale),
|
"title": new.title.get(subscriber.locale),
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ class NotificationSubscribeInfoTestCase(APITestCase):
|
||||||
class NotificationUnsubscribeAuthUserTestCase(BaseTestCase):
|
class NotificationUnsubscribeAuthUserTestCase(BaseTestCase):
|
||||||
|
|
||||||
def test_unsubscribe_auth_user(self):
|
def test_unsubscribe_auth_user(self):
|
||||||
Subscriber.objects.create(user=self.user, email=self.email, state=1)
|
Subscriber.objects.create(user=self.user, email=self.email)
|
||||||
|
|
||||||
self.test_data = {
|
self.test_data = {
|
||||||
"email": self.email
|
"email": self.email
|
||||||
|
|
@ -174,7 +174,7 @@ class NotificationManySubscribeTestCase(APITestCase):
|
||||||
test_data = {
|
test_data = {
|
||||||
'email': self.email,
|
'email': self.email,
|
||||||
'subscription_types_pk': [
|
'subscription_types_pk': [
|
||||||
self.test_subscribe_type
|
self.test_subscribe_type.id
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -183,6 +183,12 @@ class NotificationManySubscribeTestCase(APITestCase):
|
||||||
self.assertEqual(response.json()["email"], test_data["email"])
|
self.assertEqual(response.json()["email"], test_data["email"])
|
||||||
|
|
||||||
def test_unsubscribe(self):
|
def test_unsubscribe(self):
|
||||||
|
self.username = 'sedragurda'
|
||||||
|
self.password = 'sedragurdaredips19'
|
||||||
|
self.email = 'sedragurda@desoz.com'
|
||||||
|
self.user = User.objects.create_user(
|
||||||
|
username=self.username, email=self.email, password=self.password)
|
||||||
|
|
||||||
test_data = {
|
test_data = {
|
||||||
"email": self.email
|
"email": self.email
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ class SubscribeInfoView(generics.RetrieveAPIView):
|
||||||
class SubscribeInfoAuthUserView(generics.ListAPIView):
|
class SubscribeInfoAuthUserView(generics.ListAPIView):
|
||||||
"""Subscribe info auth user view."""
|
"""Subscribe info auth user view."""
|
||||||
|
|
||||||
# TODO: тут пользователь должен видеть свои подписки. проверь плс, что работает
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
serializer_class = serializers.SubscribeSerializer
|
serializer_class = serializers.SubscribeSerializer
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user