From 2deb4b3b7f6c5b446d129384f56baee9c64fd5c5 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Mon, 13 Jan 2020 12:47:07 +0300 Subject: [PATCH] some code improvements --- apps/news/tasks.py | 7 +++---- apps/notification/models.py | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/news/tasks.py b/apps/news/tasks.py index 5256c598..21afdafb 100644 --- a/apps/news/tasks.py +++ b/apps/news/tasks.py @@ -15,9 +15,8 @@ from notification.models import Subscribe @shared_task def send_email_with_news(news_ids): subscribes = Subscribe.objects.all() \ - .prefetch_related('subscriber') \ - .prefetch_related('subscription_type') \ - .exclude(unsubscrube_date__isnull=False) + .prefetch_related('subscriber', 'subscription_type') \ + .active() sent_news = models.News.objects.filter(id__in=news_ids) @@ -33,7 +32,7 @@ def send_email_with_news(news_ids): country = subscribe.subscription_type.country if country is None: - country_code = subscriber.country_code + continue # subscription_type has no country else: country_code = country.code diff --git a/apps/notification/models.py b/apps/notification/models.py index 88c87ce1..ef3ca70b 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -135,6 +135,13 @@ class Subscriber(ProjectBaseMixin): return self.subscription_types.exclude(subscriber__subscribe__unsubscribe_date__isnull=False) +class SubscribeQuerySet(models.QuerySet): + + def active(self, switcher=True): + """Fetches active subscriptions.""" + return self.exclude(unsubscribe_date__isnull=not switcher) + + class Subscribe(ProjectBaseMixin): """Subscribe model.""" @@ -144,6 +151,8 @@ class Subscribe(ProjectBaseMixin): subscriber = models.ForeignKey(Subscriber, on_delete=models.CASCADE) subscription_type = models.ForeignKey(SubscriptionType, on_delete=models.CASCADE) + objects = SubscribeQuerySet.as_manager() + class Meta: """Meta class."""