2 from django.db.models.signals import post_save
\r
3 from forum.models import *
\r
4 from forum.models.base import marked_deleted, mark_canceled
\r
5 from forum.models.node import node_create
\r
6 from forum.models.answer import answer_accepted
\r
7 from forum.authentication import user_updated
\r
8 from forum.const import *
\r
10 def record_ask_event(instance, **kwargs):
\r
11 activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)
\r
14 node_create.connect(record_ask_event, sender=Question)
\r
17 def record_answer_event(instance, **kwargs):
\r
18 activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ANSWER)
\r
21 node_create.connect(record_answer_event, sender=Answer)
\r
24 def record_comment_event(instance, **kwargs):
\r
25 act_type = (instance.content_object.__class__ is Question) and TYPE_ACTIVITY_COMMENT_QUESTION or TYPE_ACTIVITY_COMMENT_ANSWER
\r
26 activity = Activity(user=instance.user, active_at=instance.added_at, content_object=instance, activity_type=act_type)
\r
29 node_create.connect(record_comment_event, sender=Comment)
\r
32 def record_revision_event(instance, created, **kwargs):
\r
33 if created and instance.revision <> 1 and instance.node.node_type in ('question', 'answer',):
\r
34 activity_type = instance.node.node_type == 'question' and TYPE_ACTIVITY_UPDATE_QUESTION or TYPE_ACTIVITY_UPDATE_ANSWER
\r
35 activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=activity_type)
\r
38 post_save.connect(record_revision_event, sender=NodeRevision)
\r
41 def record_award_event(instance, created, **kwargs):
\r
43 activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,
\r
44 activity_type=TYPE_ACTIVITY_PRIZE)
\r
47 post_save.connect(record_award_event, sender=Award)
\r
50 def record_answer_accepted(answer, user, **kwargs):
\r
51 activity = Activity(user=user, active_at=datetime.datetime.now(), content_object=answer, activity_type=TYPE_ACTIVITY_MARK_ANSWER)
\r
54 answer_accepted.connect(record_answer_accepted)
\r
57 def update_last_seen(instance, **kwargs):
\r
58 user = instance.user
\r
59 user.last_seen = datetime.datetime.now()
\r
62 post_save.connect(update_last_seen, sender=Activity)
\r
65 def record_vote(instance, created, **kwargs):
\r
67 act_type = (instance.vote == 1) and TYPE_ACTIVITY_VOTE_UP or TYPE_ACTIVITY_VOTE_DOWN
\r
69 activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=act_type)
\r
72 post_save.connect(record_vote, sender=Vote)
\r
75 def record_cancel_vote(instance, **kwargs):
\r
76 act_type = (instance.vote == 1) and TYPE_ACTIVITY_CANCEL_VOTE_UP or TYPE_ACTIVITY_CANCEL_VOTE_DOWN
\r
77 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=act_type)
\r
80 mark_canceled.connect(record_cancel_vote, sender=Vote)
\r
83 def record_delete_post(instance, **kwargs):
\r
84 act_type = (instance.__class__ is Question) and TYPE_ACTIVITY_DELETE_QUESTION or TYPE_ACTIVITY_DELETE_ANSWER
\r
85 activity = Activity(user=instance.deleted_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=act_type)
\r
88 marked_deleted.connect(record_delete_post, sender=Question)
\r
89 marked_deleted.connect(record_delete_post, sender=Answer)
\r
92 def record_update_tags(instance, created, **kwargs):
\r
93 if not created and 'tagnames' in instance.get_dirty_fields():
\r
94 activity = Activity(user=instance.author, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_TAGS)
\r
97 post_save.connect(record_update_tags, sender=Question)
\r
100 def record_mark_offensive(instance, created, **kwargs):
\r
102 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance.content_object, activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE)
\r
105 post_save.connect(record_mark_offensive, sender=FlaggedItem)
\r
108 def record_favorite_question(instance, created, **kwargs):
\r
110 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)
\r
113 post_save.connect(record_favorite_question, sender=FavoriteQuestion)
\r
116 def record_user_full_updated(instance, **kwargs):
\r
117 activity = Activity(user=instance, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_USER_FULL_UPDATED)
\r
120 user_updated.connect(record_user_full_updated, sender=User)
\r