当前位置: 首页>>代码示例>>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;未经允许,请勿转载。