from django db models signals import post_save post_delete from blog m

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
from django.db.models.signals import post_save, post_delete
from blog.models import Post
from feedback.models import Comment
from voting.models import Vote
from accounts.models import Profile, Friend
from graphic.models import Picture, Video
from pm.models import Message
from project.models import Activity
from project.vars import FRIEND_ADDED, FRIEND_REMOVED, VOTE_FOR
import settings
def blog_post_save(sender, **kwargs):
created = kwargs.get('created')
post = kwargs['instance']
if created:
post.user.posts_counter = post.user.post_set.count()
post.user.score += settings.POST_SCORE
post.user.save()
def blog_post_delete(sender, **kwargs):
post = kwargs['instance']
post.user.posts_counter = post.user.post_set.count()
post.user.score -= settings.POST_SCORE
post.user.save()
post_save.connect(blog_post_save, sender=Post)
post_delete.connect(blog_post_delete, sender=Post)
def comments_save(sender, **kwargs):
created = kwargs['created']
comment = kwargs['instance']
if created:
comment.user.comments_counter = comment.user.comment_set.count()
comment.user.score += settings.POST_COMMENT_SCORE
comment.user.save()
def comments_delete(sender, **kwargs):
comment = kwargs['instance']
comment.user.comments_counter = comment.user.comment_set.count()
comment.user.score -= settings.POST_COMMENT_SCORE
comment.user.save()
post_save.connect(comments_save, sender=Comment)
post_delete.connect(comments_delete, sender=Comment)
def voted_for(sender, **kwargs):
created = kwargs['created']
vote = kwargs['instance']
if created:
if isinstance(vote.object, Profile):
vote.user.score -= settings.PROFILE_VOTE_SCORE
vote.object.carma += settings.PROFILE_VOTE_SCORE
vote.object.save()
vote.user.save()
activity = Activity(user=vote.user, object=vote.object, activity_type=VOTE_FOR)
activity.save()
for friend in vote.user.friends.all():
activity.recipients.add(friend)
if isinstance(vote.object, Post):
vote.user.score -= settings.POST_VOTE_SCORE
vote.object.user.carma += settings.POST_VOTE_SCORE
vote.object.user.save()
vote.user.save()
if isinstance(vote.object, Picture):
vote.user.score -= 3
vote.user.save()
if isinstance(vote.object, Video):
vote.user.score -= 3
vote.user.save()
post_save.connect(voted_for, sender=Vote)
def message_sent(sender, **kwargs):
created = kwargs['created']
message = kwargs['instance']
if created:
message.sender.pm_counter += 1
message.sender.score += 1
message.sender.save()
post_save.connect(message_sent, sender=Message)
def friend_added(sender, **kwargs):
created = kwargs['created']
instance = kwargs['instance']
if created:
activity = Activity(user=instance.friend1, object=instance.friend2, activity_type=FRIEND_ADDED)
activity.save()
for friend in instance.friend1.friends.all():
activity.recipients.add(friend)
def friend_removed(sender, **kwargs):
instance = kwargs['instance']
activity = Activity(user=instance.friend1, object=instance.friend2, activity_type=FRIEND_REMOVED)
activity.save()
for friend in instance.friend1.friends.all():
activity.recipients.add(friend)
post_save.connect(friend_added, sender=Friend)
post_delete.connect(friend_removed, sender=Friend)