當前位置: 首頁>>代碼示例>>Python>>正文


Python Notification.sale方法代碼示例

本文整理匯總了Python中notifications.models.Notification.sale方法的典型用法代碼示例。如果您正苦於以下問題:Python Notification.sale方法的具體用法?Python Notification.sale怎麽用?Python Notification.sale使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在notifications.models.Notification的用法示例。


在下文中一共展示了Notification.sale方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: handle

# 需要導入模塊: from notifications.models import Notification [as 別名]
# 或者: from notifications.models.Notification import sale [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()
開發者ID:Mustack,項目名稱:LoLSales,代碼行數:50,代碼來源:salesnotify.py


注:本文中的notifications.models.Notification.sale方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。