Reformat sending mail structure

This commit is contained in:
dormantman 2019-12-31 16:09:04 +03:00
parent 241012da42
commit 140cc6cfa5
3 changed files with 21 additions and 9 deletions

View File

@ -9,12 +9,14 @@ from django.template.loader import get_template, render_to_string
from main.models import SiteSettings
from news import models
from notification.models import Subscriber
from notification.models import Subscribe
@shared_task
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)
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 = dict(zip(map(lambda social: social.country.code, socials), socials))
for subscriber in subscribers:
socials_for_subscriber = socials.get(subscriber.country_code)
for subscribe in subscribes.filter(unsubscribe_date=None):
country = subscribe.subscription_type.country
if country is None:
continue
socials_for_subscriber = socials.get(country.code)
subscriber = subscribe.subscriber
try:
# TODO: вот тут надо учесть, подписки на какие страны есть у юзера активные (нулл время отписки) и не посылать лишнего
# TODO: обрати внимание на кол-во запросов в БД плс. они пишутся в консоль
for new in sent_news:
context = {
"title": new.title.get(subscriber.locale),

View File

@ -80,7 +80,7 @@ class NotificationSubscribeInfoTestCase(APITestCase):
class NotificationUnsubscribeAuthUserTestCase(BaseTestCase):
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 = {
"email": self.email
@ -174,7 +174,7 @@ class NotificationManySubscribeTestCase(APITestCase):
test_data = {
'email': self.email,
'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"])
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 = {
"email": self.email
}

View File

@ -28,7 +28,6 @@ class SubscribeInfoView(generics.RetrieveAPIView):
class SubscribeInfoAuthUserView(generics.ListAPIView):
"""Subscribe info auth user view."""
# TODO: тут пользователь должен видеть свои подписки. проверь плс, что работает
permission_classes = (permissions.IsAuthenticated,)
serializer_class = serializers.SubscribeSerializer