2 from django.db.models.signals import post_save
\r
3 from forum.models import *
\r
4 from forum.models.base import marked_deleted
\r
5 from forum.models.meta import vote_canceled
\r
6 from forum.authentication import user_updated
\r
7 from forum.const import *
\r
9 def record_ask_event(instance, created, **kwargs):
\r
11 activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)
\r
14 post_save.connect(record_ask_event, sender=Question)
\r
17 def record_answer_event(instance, created, **kwargs):
\r
19 activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ANSWER)
\r
22 post_save.connect(record_answer_event, sender=Answer)
\r
25 def record_comment_event(instance, created, **kwargs):
\r
27 act_type = (instance.content_object.__class__ is Question) and TYPE_ACTIVITY_COMMENT_QUESTION or TYPE_ACTIVITY_COMMENT_ANSWER
\r
28 activity = Activity(user=instance.user, active_at=instance.added_at, content_object=instance, activity_type=act_type)
\r
31 post_save.connect(record_comment_event, sender=Comment)
\r
34 def record_question_revision_event(instance, created, **kwargs):
\r
35 if created and instance.revision <> 1:
\r
36 activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_QUESTION)
\r
39 post_save.connect(record_question_revision_event, sender=QuestionRevision)
\r
42 def record_answer_revision_event(instance, created, **kwargs):
\r
43 if created and instance.revision <> 1:
\r
44 activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_ANSWER)
\r
47 post_save.connect(record_answer_revision_event, sender=AnswerRevision)
\r
50 def record_award_event(instance, created, **kwargs):
\r
52 activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,
\r
53 activity_type=TYPE_ACTIVITY_PRIZE)
\r
56 post_save.connect(record_award_event, sender=Award)
\r
59 def record_answer_accepted(instance, created, **kwargs):
\r
60 if not created and 'accepted' in instance.get_dirty_fields() and instance.accepted:
\r
61 activity = Activity(user=instance.question.author, active_at=datetime.datetime.now(), \
\r
62 content_object=instance, activity_type=TYPE_ACTIVITY_MARK_ANSWER)
\r
65 post_save.connect(record_answer_accepted, sender=Answer)
\r
68 def update_last_seen(instance, **kwargs):
\r
69 user = instance.user
\r
70 user.last_seen = datetime.datetime.now()
\r
73 post_save.connect(update_last_seen, sender=Activity)
\r
76 def record_vote(instance, created, **kwargs):
\r
78 act_type = (instance.vote == 1) and TYPE_ACTIVITY_VOTE_UP or TYPE_ACTIVITY_VOTE_DOWN
\r
80 activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=act_type)
\r
83 post_save.connect(record_vote, sender=Vote)
\r
86 def record_cancel_vote(instance, **kwargs):
\r
87 act_type = (instance.vote == 1) and TYPE_ACTIVITY_CANCEL_VOTE_UP or TYPE_ACTIVITY_CANCEL_VOTE_DOWN
\r
88 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=act_type)
\r
91 vote_canceled.connect(record_cancel_vote)
\r
94 def record_delete_post(instance, **kwargs):
\r
95 act_type = (instance.__class__ is Question) and TYPE_ACTIVITY_DELETE_QUESTION or TYPE_ACTIVITY_DELETE_ANSWER
\r
96 activity = Activity(user=instance.deleted_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=act_type)
\r
99 marked_deleted.connect(record_delete_post, sender=Question)
\r
100 marked_deleted.connect(record_delete_post, sender=Answer)
\r
103 def record_update_tags(instance, created, **kwargs):
\r
104 if not created and 'tagnames' in instance.get_dirty_fields():
\r
105 activity = Activity(user=instance.author, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_TAGS)
\r
108 post_save.connect(record_update_tags, sender=Question)
\r
111 def record_mark_offensive(instance, created, **kwargs):
\r
113 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance.content_object, activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE)
\r
116 post_save.connect(record_mark_offensive, sender=FlaggedItem)
\r
119 def record_favorite_question(instance, created, **kwargs):
\r
121 activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)
\r
124 post_save.connect(record_favorite_question, sender=FavoriteQuestion)
\r
127 def record_user_full_updated(instance, **kwargs):
\r
128 activity = Activity(user=instance, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_USER_FULL_UPDATED)
\r
131 user_updated.connect(record_user_full_updated, sender=User)
\r