tool.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:USTC-Software-2017 作者: igemsoftware2017 项目源码 文件源码
def send_or_update(self, user, template, **context):
        """
        Update or send a notice.
        The notices to update are located by `target`, `target_slug`, `actor`,
        which are ALL required to be specified.
        """

        filter_fields = set(context.get('filter_fields', {'target', 'target_slug', 'actor', 'user', 'category'}))
        assert filter_fields, '`filter_fields` should not be empty.'

        specified = {'user', 'category', *context}
        missing = filter_fields - specified
        assert not missing, 'Members %s of `filter_fields` are missing.' % ', '.join(missing)

        keywords = {
            name: value
            for name, value in {
                'user': user, 'template': template, 'category': self.category,
                **context
            }.items()
            if name in filter_fields
        }

        if 'target' in filter_fields and context['target'] is not None:
            target = context['target']
            ctype = ContentType.objects.get_for_model(target)
            keywords.pop('target')
            keywords.update({
                'target_type': ctype,
                'target_id': target.pk
            })

        try_find = Notice.objects.filter(**keywords)

        if try_find.count():
            message = render_notice_message(
                template, self, user=user, **context)
            try_find.update(message=message, has_read=False, created=Now())
            notice = None
        else:
            notice = self.send(user, template, **context)

        Notice.objects.broadcast_stats([user])
        return notice
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号