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
评论列表
文章目录