3 #-------------------------------------------------------------------------------
4 # Name: Award badges command
5 # Purpose: This is a command file croning in background process regularly to
6 # query database and award badges for user's special acitivities.
8 # Author: Mike, Sailing
11 # Copyright: (c) Mike 2009
13 #-------------------------------------------------------------------------------
15 from datetime import datetime, date
16 from django.core.management.base import NoArgsCommand
17 from django.db import connection
18 from django.shortcuts import get_object_or_404
19 from django.contrib.contenttypes.models import ContentType
21 from forum.models import *
22 from forum.const import *
23 from base_command import BaseCommand
25 (1, '炼狱法师', 3, '炼狱法师', '删除自己有3个以上赞成票的帖子', 1, 0),
26 (2, '压力白领', 3, '压力白领', '删除自己有3个以上反对票的帖子', 1, 0),
27 (3, '优秀回答', 3, '优秀回答', '回答好评10次以上', 1, 0),
28 (4, '优秀问题', 3, '优秀问题', '问题好评10次以上', 1, 0),
29 (5, '评论家', 3, '评论家', '评论10次以上', 0, 0),
30 (6, '流行问题', 3, '流行问题', '问题的浏览量超过1000人次', 1, 0),
31 (7, '巡逻兵', 3, '巡逻兵', '第一次标记垃圾帖子', 0, 0),
32 (8, '清洁工', 3, '清洁工', '第一次撤销投票', 0, 0),
33 (9, '批评家', 3, '批评家', '第一次反对票', 0, 0),
34 (10, '小编', 3, '小编', '第一次编辑更新', 0, 0),
35 (11, '村长', 3, '村长', '第一次重新标签', 0, 0),
36 (12, '学者', 3, '学者', '第一次标记答案', 0, 0),
37 (13, '学生', 3, '学生', '第一次提问并且有一次以上赞成票', 0, 0),
38 (14, '支持者', 3, '支持者', '第一次赞成票', 0, 0),
39 (15, '教师', 3, '教师', '第一次回答问题并且得到一个以上赞成票', 0, 0),
40 (16, '自传作者', 3, '自传作者', '完整填写用户资料所有选项', 0, 0),
41 (17, '自学成才', 3, '自学成才', '回答自己的问题并且有3个以上赞成票', 1, 0),
42 (18, '最有价值回答', 1, '最有价值回答', '回答超过100次赞成票', 1, 0),
43 (19, '最有价值问题', 1, '最有价值问题', '问题超过100次赞成票', 1, 0),
44 (20, '万人迷', 1, '万人迷', '问题被100人以上收藏', 1, 0),
45 (21, '著名问题', 1, '著名问题', '问题的浏览量超过10000人次', 1, 0),
46 (22, 'alpha用户', 2, 'alpha用户', '内测期间的活跃用户', 0, 0),
47 (23, '极好回答', 2, '极好回答', '回答超过25次赞成票', 1, 0),
48 (24, '极好问题', 2, '极好问题', '问题超过25次赞成票', 1, 0),
49 (25, '受欢迎问题', 2, '受欢迎问题', '问题被25人以上收藏', 1, 0),
50 (26, '优秀市民', 2, '优秀市民', '投票300次以上', 0, 0),
51 (27, '编辑主任', 2, '编辑主任', '编辑了100个帖子', 0, 0),
52 (28, '通才', 2, '通才', '在多个标签领域活跃', 0, 0),
53 (29, '专家', 2, '专家', '在一个标签领域活跃出众', 0, 0),
54 (30, '老鸟', 2, '老鸟', '活跃超过一年的用户', 0, 0),
55 (31, '最受关注问题', 2, '最受关注问题', '问题的浏览量超过2500人次', 1, 0),
56 (32, '学问家', 2, '学问家', '第一次回答被投赞成票10次以上', 0, 0),
57 (33, 'beta用户', 2, 'beta用户', 'beta期间活跃参与', 0, 0),
58 (34, '导师', 2, '导师', '被指定为最佳答案并且赞成票40以上', 1, 0),
59 (35, '巫师', 2, '巫师', '在提问60天之后回答并且赞成票5次以上', 1, 0),
60 (36, '分类专家', 2, '分类专家', '创建的标签被50个以上问题使用', 1, 0);
63 TYPE_ACTIVITY_ASK_QUESTION=1
64 TYPE_ACTIVITY_ANSWER=2
65 TYPE_ACTIVITY_COMMENT_QUESTION=3
66 TYPE_ACTIVITY_COMMENT_ANSWER=4
67 TYPE_ACTIVITY_UPDATE_QUESTION=5
68 TYPE_ACTIVITY_UPDATE_ANSWER=6
70 TYPE_ACTIVITY_MARK_ANSWER=8
71 TYPE_ACTIVITY_VOTE_UP=9
72 TYPE_ACTIVITY_VOTE_DOWN=10
73 TYPE_ACTIVITY_CANCEL_VOTE=11
74 TYPE_ACTIVITY_DELETE_QUESTION=12
75 TYPE_ACTIVITY_DELETE_ANSWER=13
76 TYPE_ACTIVITY_MARK_OFFENSIVE=14
77 TYPE_ACTIVITY_UPDATE_TAGS=15
78 TYPE_ACTIVITY_FAVORITE=16
79 TYPE_ACTIVITY_USER_FULL_UPDATED = 17
82 class Command(BaseCommand):
83 def handle_noargs(self, **options):
86 self.delete_question_be_voted_up_3()
87 self.delete_answer_be_voted_up_3()
88 self.delete_question_be_vote_down_3()
89 self.delete_answer_be_voted_down_3()
90 self.answer_be_voted_up_10()
91 self.question_be_voted_up_10()
92 self.question_view_1000()
93 self.answer_self_question_be_voted_up_3()
94 self.answer_be_voted_up_100()
95 self.question_be_voted_up_100()
96 self.question_be_favorited_100()
97 self.question_view_10000()
98 self.answer_be_voted_up_25()
99 self.question_be_voted_up_25()
100 self.question_be_favorited_25()
101 self.question_view_2500()
102 self.answer_be_accepted_and_voted_up_40()
103 self.question_be_answered_after_60_days_and_be_voted_up_5()
104 self.created_tag_be_used_in_question_50()
110 def delete_question_be_voted_up_3(self):
112 (1, '炼狱法师', 3, '炼狱法师', '删除自己有3个以上赞成票的帖子', 1, 0),
114 query = "SELECT act.id, act.user_id, act.object_id FROM activity act, question q WHERE act.object_id = q.id AND\
115 act.activity_type = %s AND\
116 q.vote_up_count >=3 AND \
117 act.is_auditted = 0" % (TYPE_ACTIVITY_DELETE_QUESTION)
118 self.__process_activities_badge(query, 1, Question)
120 def delete_answer_be_voted_up_3(self):
122 (1, '炼狱法师', 3, '炼狱法师', '删除自己有3个以上赞成票的帖子', 1, 0),
124 query = "SELECT act.id, act.user_id, act.object_id FROM activity act, answer an WHERE act.object_id = an.id AND\
125 act.activity_type = %s AND\
126 an.vote_up_count >=3 AND \
127 act.is_auditted = 0" % (TYPE_ACTIVITY_DELETE_ANSWER)
128 self.__process_activities_badge(query, 1, Answer)
130 def delete_question_be_vote_down_3(self):
132 (2, '压力白领', 3, '压力白领', '删除自己有3个以上反对票的帖子', 1, 0),
134 query = "SELECT act.id, act.user_id, act.object_id FROM activity act, question q WHERE act.object_id = q.id AND\
135 act.activity_type = %s AND\
136 q.vote_down_count >=3 AND \
137 act.is_auditted = 0" % (TYPE_ACTIVITY_DELETE_QUESTION)
138 content_type = ContentType.objects.get_for_model(Question)
139 self.__process_activities_badge(query, 2, Question)
141 def delete_answer_be_voted_down_3(self):
143 (2, '压力白领', 3, '压力白领', '删除自己有3个以上反对票的帖子', 1, 0),
145 query = "SELECT act.id, act.user_id, act.object_id FROM activity act, answer an WHERE act.object_id = an.id AND\
146 act.activity_type = %s AND\
147 an.vote_down_count >=3 AND \
148 act.is_auditted = 0" % (TYPE_ACTIVITY_DELETE_ANSWER)
149 self.__process_activities_badge(query, 2, Answer)
151 def answer_be_voted_up_10(self):
153 (3, '优秀回答', 3, '优秀回答', '回答好评10次以上', 1, 0),
155 query = "SELECT act.id, act.user_id, act.object_id FROM \
156 activity act, answer a WHERE act.object_id = a.id AND\
157 act.activity_type = %s AND \
158 a.vote_up_count >= 10 AND\
159 act.is_auditted = 0" % (TYPE_ACTIVITY_ANSWER)
160 self.__process_activities_badge(query, 3, Answer)
162 def question_be_voted_up_10(self):
164 (4, '优秀问题', 3, '优秀问题', '问题好评10次以上', 1, 0),
166 query = "SELECT act.id, act.user_id, act.object_id FROM \
167 activity act, question q WHERE act.object_id = q.id AND\
168 act.activity_type = %s AND \
169 q.vote_up_count >= 10 AND\
170 act.is_auditted = 0" % (TYPE_ACTIVITY_ASK_QUESTION)
171 self.__process_activities_badge(query, 4, Question)
173 def question_view_1000(self):
175 (6, '流行问题', 3, '流行问题', '问题的浏览量超过1000人次', 1, 0),
177 query = "SELECT act.id, act.user_id, act.object_id FROM \
178 activity act, question q WHERE act.activity_type = %s AND\
179 act.object_id = q.id AND \
180 q.view_count >= 1000 AND\
181 act.object_id NOT IN \
182 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (TYPE_ACTIVITY_ASK_QUESTION, 6)
183 self.__process_activities_badge(query, 6, Question, False)
185 def answer_self_question_be_voted_up_3(self):
187 (17, '自学成才', 3, '自学成才', '回答自己的问题并且有3个以上赞成票', 1, 0),
189 query = "SELECT act.id, act.user_id, act.object_id FROM \
190 activity act, answer an WHERE act.activity_type = %s AND\
191 act.object_id = an.id AND\
192 an.vote_up_count >= 3 AND\
193 act.user_id = (SELECT user_id FROM question q WHERE q.id = an.question_id) AND\
194 act.object_id NOT IN \
195 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (TYPE_ACTIVITY_ANSWER, 17)
196 self.__process_activities_badge(query, 17, Question, False)
198 def answer_be_voted_up_100(self):
200 (18, '最有价值回答', 1, '最有价值回答', '回答超过100次赞成票', 1, 0),
202 query = "SELECT an.id, an.author_id FROM answer an WHERE an.vote_up_count >= 100 AND an.id NOT IN \
203 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (18)
205 self.__process_badge(query, 18, Answer)
207 def question_be_voted_up_100(self):
209 (19, '最有价值问题', 1, '最有价值问题', '问题超过100次赞成票', 1, 0),
211 query = "SELECT q.id, q.author_id FROM question q WHERE q.vote_up_count >= 100 AND q.id NOT IN \
212 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (19)
214 self.__process_badge(query, 19, Question)
216 def question_be_favorited_100(self):
218 (20, '万人迷', 1, '万人迷', '问题被100人以上收藏', 1, 0),
220 query = "SELECT q.id, q.author_id FROM question q WHERE q.favourite_count >= 100 AND q.id NOT IN \
221 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (20)
223 self.__process_badge(query, 20, Question)
225 def question_view_10000(self):
227 (21, '著名问题', 1, '著名问题', '问题的浏览量超过10000人次', 1, 0),
229 query = "SELECT q.id, q.author_id FROM question q WHERE q.view_count >= 10000 AND q.id NOT IN \
230 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (21)
232 self.__process_badge(query, 21, Question)
234 def answer_be_voted_up_25(self):
236 (23, '极好回答', 2, '极好回答', '回答超过25次赞成票', 1, 0),
238 query = "SELECT a.id, a.author_id FROM answer a WHERE a.vote_up_count >= 25 AND a.id NOT IN \
239 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (23)
241 self.__process_badge(query, 23, Answer)
243 def question_be_voted_up_25(self):
245 (24, '极好问题', 2, '极好问题', '问题超过25次赞成票', 1, 0),
247 query = "SELECT q.id, q.author_id FROM question q WHERE q.vote_up_count >= 25 AND q.id NOT IN \
248 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (24)
250 self.__process_badge(query, 24, Question)
252 def question_be_favorited_25(self):
254 (25, '受欢迎问题', 2, '受欢迎问题', '问题被25人以上收藏', 1, 0),
256 query = "SELECT q.id, q.author_id FROM question q WHERE q.favourite_count >= 25 AND q.id NOT IN \
257 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (25)
259 self.__process_badge(query, 25, Question)
261 def question_view_2500(self):
263 (31, '最受关注问题', 2, '最受关注问题', '问题的浏览量超过2500人次', 1, 0),
265 query = "SELECT q.id, q.author_id FROM question q WHERE q.view_count >= 2500 AND q.id NOT IN \
266 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (31)
268 self.__process_badge(query, 31, Question)
270 def answer_be_accepted_and_voted_up_40(self):
272 (34, '导师', 2, '导师', '被指定为最佳答案并且赞成票40以上', 1, 0),
274 query = "SELECT a.id, a.author_id FROM answer a WHERE a.vote_up_count >= 40 AND\
277 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (34)
279 self.__process_badge(query, 34, Answer)
281 def question_be_answered_after_60_days_and_be_voted_up_5(self):
283 (35, '巫师', 2, '巫师', '在提问60天之后回答并且赞成票5次以上', 1, 0),
285 query = "SELECT a.id, a.author_id FROM question q, answer a WHERE q.id = a.question_id AND\
286 DATEDIFF(a.added_at, q.added_at) >= 60 AND\
287 a.vote_up_count >= 5 AND \
289 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (35)
291 self.__process_badge(query, 35, Answer)
293 def created_tag_be_used_in_question_50(self):
295 (36, '分类专家', 2, '分类专家', '创建的标签被50个以上问题使用', 1, 0);
297 query = "SELECT t.id, t.created_by_id FROM tag t, auth_user u WHERE t.created_by_id = u.id AND \
298 t. used_count >= 50 AND \
300 (SELECT object_id FROM award WHERE award.badge_id = %s)" % (36)
302 self.__process_badge(query, 36, Tag)
304 def __process_activities_badge(self, query, badge, content_object, update_auditted=True):
305 content_type = ContentType.objects.get_for_model(content_object)
307 cursor = connection.cursor()
309 cursor.execute(query)
310 rows = cursor.fetchall()
314 badge = get_object_or_404(Badge, id=badge)
320 user = get_object_or_404(User, id=user_id)
321 award = Award(user=user, badge=badge, content_type=content_type, object_id=object_id)
325 activity_ids.append(activity_id)
328 self.update_activities_auditted(cursor, activity_ids)
332 def __process_badge(self, query, badge, content_object):
333 content_type = ContentType.objects.get_for_model(Answer)
334 cursor = connection.cursor()
336 cursor.execute(query)
337 rows = cursor.fetchall()
339 badge = get_object_or_404(Badge, id=badge)
344 user = get_object_or_404(User, id=user_id)
345 award = Award(user=user, badge=badge, content_type=content_type, object_id=object_id)