1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from django.utils.translation import ugettext as _
from django.db.models import F
from forum.models.action import ActionProxy, DummyActionProxy
from forum.models import Vote, Flag
from forum import settings
class VoteAction(ActionProxy):
def update_node_score(self, inc):
self.node.score = F('score') + inc
self.node.save()
def process_vote_action(self, value):
self.update_node_score(value)
vote = Vote(node=self.node, user=self.user, action=self, value=value)
vote.save()
def cancel_action(self):
vote = self.vote
self.update_node_score(-vote.value)
vote.delete()
@classmethod
def get_for(cls, user, node):
try:
vote = Vote.objects.get(user=user, node=node)
return vote.value
except:
return None
@classmethod
def get_action_for(cls, user, node):
try:
vote = Vote.objects.get(user=user, node=node)
return vote.action
except:
return None
def describe_vote(self, vote_desc, viewer=None):
return _("%(user)s %(vote_desc)s %(post_desc)s") % {
'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
'vote_desc': vote_desc, 'post_desc': self.describe_node(viewer, self.node)
}
class VoteUpAction(VoteAction):
def repute_users(self):
self.repute(self.node.author, int(settings.REP_GAIN_BY_UPVOTED))
def process_action(self):
self.process_vote_action(1)
self.user.reset_vote_up_count_cache()
def cancel_action(self):
super(VoteUpAction, self).cancel_action()
self.user.reset_vote_up_count_cache()
def describe(self, viewer=None):
return self.describe_vote(_("voted up"), viewer)
class VoteDownAction(VoteAction):
def repute_users(self):
self.repute(self.node.author, -int(settings.REP_LOST_BY_DOWNVOTED))
self.repute(self.user, -int(settings.REP_LOST_BY_DOWNVOTING))
def process_action(self):
self.process_vote_action(-1)
self.user.reset_vote_down_count_cache()
def cancel_action(self):
super(VoteDownAction, self).cancel_action()
self.user.reset_vote_down_count_cache()
def describe(self, viewer=None):
return self.describe_vote(_("voted down"), viewer)
class VoteUpCommentAction(VoteUpAction):
def repute_users(self):
pass
def process_action(self):
self.process_vote_action(1)
def cancel_action(self):
super(VoteUpAction, self).cancel_action()
def describe(self, viewer=None):
return self.describe_vote(_("liked"), viewer)
class FlagAction(ActionProxy):
def repute_users(self):
self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED))
def process_action(self):
flag = Flag(user=self.user, node=self.node, action=self, reason=self.extra)
flag.save()
self.node.reset_flag_count_cache()
if self.node.flag_count >= int(settings.FLAG_COUNT_TO_HIDE_POST):
self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_3_TIMES))
if self.node.flag_count >= int(settings.FLAG_COUNT_TO_DELETE_POST):
self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_5_TIMES))
if not self.node.nis.deleted:
DeleteAction(node=self.node, user=self.user, extra="BYFLAGGED").save()
def cancel_action(self):
self.flag.delete()
self.node.reset_flag_count_cache()
@classmethod
def get_for(cls, user, node):
try:
flag = Flag.objects.get(user=user, node=node)
return flag.reason or _("No reason given")
except:
return None
def describe(self, viewer=None):
return _("%(user)s flagged %(post_desc)s: %(reason)s") % {
'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
'post_desc': self.describe_node(viewer, self.node), 'reason': self.extra
}
class AcceptAnswerAction(ActionProxy):
def repute_users(self):
if (self.user == self.node.parent.author) and (not self.user == self.node.author):
self.repute(self.user, int(settings.REP_GAIN_BY_ACCEPTING))
if self.user != self.node.author:
self.repute(self.node.author, int(settings.REP_GAIN_BY_ACCEPTED))
def process_action(self):
self.node.marked = True
self.node.nstate.accepted = self
self.node.save()
self.node.question.reset_accepted_count_cache()
def cancel_action(self):
self.node.marked = False
self.node.nstate.accepted = None
self.node.save()
self.node.question.reset_accepted_count_cache()
def describe(self, viewer=None):
answer = self.node
question = answer.parent
if self.user == question.author:
asker = (self.user == viewer) and _("your") or _("his")
else:
asker = self.hyperlink(question.author.get_profile_url(), question.author.username)
return _("%(user)s accepted %(answerer)s answer on %(asker)s question %(question)s") % {
'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
'answerer': self.hyperlink(answer.author.get_profile_url(), self.friendly_ownername(viewer, answer.author)),
'asker': asker,
'question': self.hyperlink(question.get_absolute_url(), question.title)
}
class FavoriteAction(ActionProxy):
def process_action(self):
self.node.reset_favorite_count_cache()
def cancel_action(self):
self.process_action()
def describe(self, viewer=None):
return _("%(user)s marked %(post_desc)s as favorite") % {
'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
'post_desc': self.describe_node(viewer, self.node),
}
class DeleteAction(ActionProxy):
def process_action(self):
self.node.mark_deleted(self)
if self.node.node_type == "answer":
self.node.question.reset_answer_count_cache()
def cancel_action(self):
self.node.mark_deleted(None)
if self.node.node_type == "answer":
self.node.question.reset_answer_count_cache()
def describe(self, viewer=None):
return _("%(user)s deleted %(post_desc)s") % {
'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),
'post_desc': self.describe_node(viewer, self.node)
}
def reason(self):
if self.extra != "BYFLAGGED":
return self.extra
else:
return _("flagged by multiple users: ") + "; ".join([f.extra for f in FlagAction.objects.filter(node=self.node)])
class UnknownAction(ActionProxy):
pass
class QuestionViewAction(DummyActionProxy):
def __init__(self, node, user, ip=None):
self.viewuser = user
self.node = node
super(QuestionViewAction, self).__init__(ip)
def process_action(self):
self.node.extra_count = F('extra_count') + 1
self.node.save()