]> git.openstreetmap.org Git - osqa.git/blob - osqa/forum/reputation.py
initial import
[osqa.git] / osqa / forum / reputation.py
1 from django.db.models.signals import post_save\r
2 from forum.models.meta import vote_canceled\r
3 \r
4 from forum.models import *\r
5 from forum.const import *\r
6 import settings\r
7 \r
8 def on_flagged_item(instance, created, **kwargs):\r
9     if not created:\r
10         return\r
11 \r
12     post = instance.content_object\r
13     question = (post.__class__ == Question) and post or post.question\r
14 \r
15     user.reputes.create(value=-int(settings.REP_LOST_BY_FLAGGED), question=question,\r
16                reputation_type=TYPE_REPUTATION_LOST_BY_FLAGGED)\r
17 \r
18 \r
19     if post.offensive_flag_count == settings.FLAG_COUNT_TO_HIDE_POST:\r
20         post.author.reputes.create(value=-int(settings.REP_LOST_BY_FLAGGED_3_TIMES),\r
21                    question=question, reputation_type=TYPE_REPUTATION_LOST_BY_FLAGGED_3_TIMES)\r
22 \r
23     if post.offensive_flag_count == settings.FLAG_COUNT_TO_DELETE_POST:\r
24         post.author.reputes.create(value=-int(settings.REP_LOST_BY_FLAGGED_5_TIMES),\r
25                    question=question, reputation_type=TYPE_REPUTATION_LOST_BY_FLAGGED_5_TIMES)\r
26 \r
27         post.mark_deleted(User.objects.get_site_owner())\r
28 \r
29 post_save.connect(on_flagged_item, sender=FlaggedItem)\r
30 \r
31 \r
32 def on_answer_accepted_switch(instance, created, **kwargs):\r
33     if not created and 'accepted' in instance.get_dirty_fields() and (\r
34             not instance.accepted_by == instance.question.author):\r
35         repute_type, repute_value = instance.accepted and (\r
36             TYPE_REPUTATION_GAIN_BY_ANSWER_ACCEPTED, int(settings.REP_GAIN_BY_ACCEPTED)) or (\r
37             TYPE_REPUTATION_LOST_BY_ACCEPTED_ANSWER_CANCELED, -int(settings.REP_LOST_BY_ACCEPTED_CANCELED))\r
38 \r
39         instance.author.reputes.create(value=repute_value, question=instance.question, reputation_type=repute_type)\r
40         \r
41         if instance.accepted_by == instance.question.author:\r
42             repute_type, repute_value = instance.accepted and (\r
43             TYPE_REPUTATION_GAIN_BY_ACCEPTING_ANSWER, int(settings.REP_GAIN_BY_ACCEPTING)) or (\r
44             TYPE_REPUTATION_LOST_BY_CANCELLING_ACCEPTED_ANSWER, -int(settings.REP_LOST_BY_CANCELING_ACCEPTED))\r
45 \r
46             instance.question.author.reputes.create(value=repute_value, question=instance.question, reputation_type=repute_type)\r
47 \r
48 post_save.connect(on_answer_accepted_switch, sender=Answer)\r
49 \r
50 \r
51 def on_vote(instance, created, **kwargs):\r
52     if created and not instance.content_object.wiki:\r
53         post = instance.content_object\r
54         question = (post.__class__ == Question) and post or post.question\r
55 \r
56         if instance.vote == -1:\r
57             instance.user.reputes.create(value=-int(settings.REP_LOST_BY_DOWNVOTING),\r
58             question=question, reputation_type=TYPE_REPUTATION_LOST_BY_DOWNVOTING)\r
59 \r
60         if instance.vote == 1 and post.author.get_reputation_by_upvoted_today() >= int(settings.MAX_REP_BY_UPVOTE_DAY):\r
61             return\r
62 \r
63         repute_type, repute_value = (instance.vote == 1) and (\r
64             TYPE_REPUTATION_GAIN_BY_UPVOTED, int(settings.REP_GAIN_BY_UPVOTED)) or (\r
65             TYPE_REPUTATION_LOST_BY_DOWNVOTED, -int(settings.REP_LOST_BY_DOWNVOTED))\r
66 \r
67         post.author.reputes.create(value=repute_value, question=question, reputation_type=repute_type)\r
68 \r
69 post_save.connect(on_vote, sender=Vote)\r
70 \r
71 \r
72 def on_vote_canceled(instance, **kwargs):\r
73     if not instance.content_object.wiki:\r
74         post = instance.content_object\r
75         question = (post.__class__ == Question) and post or post.question\r
76 \r
77         if instance.vote == -1:\r
78             instance.user.reputes.create(value=int(settings.REP_GAIN_BY_CANCELING_DOWNVOTE),\r
79             question=question, reputation_type=TYPE_REPUTATION_GAIN_BY_CANCELING_DOWNVOTE)\r
80 \r
81         repute_type, repute_value = (instance.vote == 1) and (\r
82             TYPE_REPUTATION_LOST_BY_UPVOTE_CANCELED, -int(settings.REP_LOST_BY_UPVOTE_CANCELED)) or (\r
83             TYPE_REPUTATION_GAIN_BY_DOWNVOTE_CANCELED, int(settings.REP_GAIN_BY_DOWNVOTE_CANCELED))\r
84 \r
85         post.author.reputes.create(value=repute_value, question=question, reputation_type=repute_type)\r
86 \r
87 vote_canceled.connect(on_vote_canceled)\r
88 \r
89 \r
90     \r
91 \r
92 \r