本文整理匯總了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()