]> git.openstreetmap.org Git - osqa.git/blob - forum/activity.py
Directory 'osqa' created by PyCharm
[osqa.git] / forum / activity.py
1 import datetime\r
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
8 \r
9 def record_ask_event(instance, created, **kwargs):\r
10     if created:\r
11         activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)\r
12         activity.save()\r
13 \r
14 post_save.connect(record_ask_event, sender=Question)\r
15 \r
16 \r
17 def record_answer_event(instance, created, **kwargs):\r
18     if created:\r
19         activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ANSWER)\r
20         activity.save()\r
21 \r
22 post_save.connect(record_answer_event, sender=Answer)\r
23 \r
24 \r
25 def record_comment_event(instance, created, **kwargs):\r
26     if created:\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
29         activity.save()\r
30 \r
31 post_save.connect(record_comment_event, sender=Comment)\r
32 \r
33 \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
37         activity.save()\r
38 \r
39 post_save.connect(record_question_revision_event, sender=QuestionRevision)\r
40 \r
41 \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
45         activity.save()\r
46 \r
47 post_save.connect(record_answer_revision_event, sender=AnswerRevision)\r
48 \r
49 \r
50 def record_award_event(instance, created, **kwargs):\r
51     if created:\r
52         activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,\r
53             activity_type=TYPE_ACTIVITY_PRIZE)\r
54         activity.save()\r
55 \r
56 post_save.connect(record_award_event, sender=Award)\r
57 \r
58 \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
63         activity.save()\r
64 \r
65 post_save.connect(record_answer_accepted, sender=Answer)\r
66 \r
67 \r
68 def update_last_seen(instance, **kwargs):\r
69     user = instance.user\r
70     user.last_seen = datetime.datetime.now()\r
71     user.save()\r
72 \r
73 post_save.connect(update_last_seen, sender=Activity)\r
74 \r
75 \r
76 def record_vote(instance, created, **kwargs):\r
77     if created:\r
78         act_type = (instance.vote == 1) and TYPE_ACTIVITY_VOTE_UP or TYPE_ACTIVITY_VOTE_DOWN\r
79 \r
80         activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=act_type)\r
81         activity.save()\r
82 \r
83 post_save.connect(record_vote, sender=Vote)\r
84 \r
85 \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
89     activity.save()\r
90 \r
91 vote_canceled.connect(record_cancel_vote)\r
92 \r
93 \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
97     activity.save()\r
98 \r
99 marked_deleted.connect(record_delete_post, sender=Question)\r
100 marked_deleted.connect(record_delete_post, sender=Answer)\r
101 \r
102 \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
106         activity.save()\r
107 \r
108 post_save.connect(record_update_tags, sender=Question)\r
109 \r
110 \r
111 def record_mark_offensive(instance, created, **kwargs):\r
112     if created:\r
113         activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance.content_object, activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE)\r
114         activity.save()\r
115 \r
116 post_save.connect(record_mark_offensive, sender=FlaggedItem)\r
117 \r
118 \r
119 def record_favorite_question(instance, created, **kwargs):\r
120     if created:\r
121         activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)\r
122         activity.save()\r
123 \r
124 post_save.connect(record_favorite_question, sender=FavoriteQuestion)\r
125 \r
126 \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
129     activity.save()\r
130 \r
131 user_updated.connect(record_user_full_updated, sender=User)\r
132 \r