def handler signal kwargs def wrapper func signal connect func kwargs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def handler(signal, **kwargs)
def wrapper(func)
signal.connect(func, **kwargs)
return wrapper
### Example:
from django.db.models.signals import post_save
@handler(post_save, sender=User)
def vote_recived(sender, **kwargs):
instance = kwargs['instance']
if isinstance(instance.object, Video) or isinstance(instance.object, Picture):
object = instance.object
counter = [vote.vote for vote in object.votes.all()]
object.score = sum(counter)
object.save()