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