some code improvements

This commit is contained in:
Kuroshini 2020-01-13 12:47:07 +03:00
parent 114304b3c9
commit 2deb4b3b7f
2 changed files with 12 additions and 4 deletions

View File

@ -15,9 +15,8 @@ from notification.models import Subscribe
@shared_task @shared_task
def send_email_with_news(news_ids): def send_email_with_news(news_ids):
subscribes = Subscribe.objects.all() \ subscribes = Subscribe.objects.all() \
.prefetch_related('subscriber') \ .prefetch_related('subscriber', 'subscription_type') \
.prefetch_related('subscription_type') \ .active()
.exclude(unsubscrube_date__isnull=False)
sent_news = models.News.objects.filter(id__in=news_ids) 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 country = subscribe.subscription_type.country
if country is None: if country is None:
country_code = subscriber.country_code continue # subscription_type has no country
else: else:
country_code = country.code country_code = country.code

View File

@ -135,6 +135,13 @@ class Subscriber(ProjectBaseMixin):
return self.subscription_types.exclude(subscriber__subscribe__unsubscribe_date__isnull=False) 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): class Subscribe(ProjectBaseMixin):
"""Subscribe model.""" """Subscribe model."""
@ -144,6 +151,8 @@ class Subscribe(ProjectBaseMixin):
subscriber = models.ForeignKey(Subscriber, on_delete=models.CASCADE) subscriber = models.ForeignKey(Subscriber, on_delete=models.CASCADE)
subscription_type = models.ForeignKey(SubscriptionType, on_delete=models.CASCADE) subscription_type = models.ForeignKey(SubscriptionType, on_delete=models.CASCADE)
objects = SubscribeQuerySet.as_manager()
class Meta: class Meta:
"""Meta class.""" """Meta class."""