本文整理汇总了Python中notifications.models.Notification.subscription方法的典型用法代码示例。如果您正苦于以下问题:Python Notification.subscription方法的具体用法?Python Notification.subscription怎么用?Python Notification.subscription使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类notifications.models.Notification
的用法示例。
在下文中一共展示了Notification.subscription方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from notifications.models import Notification [as 别名]
# 或者: from notifications.models.Notification import subscription [as 别名]
def handle(self, *args, **options):
today = date.today()
to_notify = EmptyQuerySet()
# Iterate over sales that are in effect
for sale in Sale.objects.filter(start__lte=today,
end__gte=today):
# Find subscriptions that require the products on sale
subscriptions = Subscription.objects.filter(product__in=sale.products.all())
# Remove subscriptions that have been notified
subscriptions = subscriptions.exclude(notifications__sale=sale)
to_notify |= subscriptions
# Collect the subscriptions by user
user_subs = defaultdict(list)
for sub in to_notify:
user_subs[sub.user].append(sub)
from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', '[email protected]')
# Build the emails
for user, subs in user_subs.iteritems():
to_email = user.email
context = {'user': user, 'subscriptions': subs}
subject = render_to_string('notifications/sale_subject.txt', context).replace('\n', '').strip()
context['subject'] = subject
text_content = render_to_string('notifications/sale_body.txt', context)
html_content = render_to_string('notifications/sale_body.html', context)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email])
msg.attach_alternative(html_content, 'text/html')
msg.send() # Will raise exceptions on failure
# Mark all active sales for this product as notified
for sub in subs:
for sale in sub.product.active_sales.all():
notified = Notification()
notified.sale = sale
notified.subscription = sub
notified.save()