Commit 17cba28932280bac88da73510bdf3cb279d85fc3

Authored by zhenchaozhu
1 parent 5988f9bea4
Exists in master and in 1 other branch de

修改发表评论的bug

Showing 1 changed file with 2 additions and 1 deletions   Show diff stats
forum/views/topic.py
1 # coding: utf-8 1 # coding: utf-8
2 2
3 import json, math, hashlib 3 import json, math, hashlib
4 from django.http import HttpResponse, Http404 4 from django.http import HttpResponse, Http404
5 from django.shortcuts import render_to_response, redirect, get_object_or_404 5 from django.shortcuts import render_to_response, redirect, get_object_or_404
6 from django.contrib import auth 6 from django.contrib import auth
7 from django.contrib.auth.decorators import login_required 7 from django.contrib.auth.decorators import login_required
8 from django.template import RequestContext 8 from django.template import RequestContext
9 from django.utils import timezone 9 from django.utils import timezone
10 from django.conf import settings 10 from django.conf import settings
11 from forum.models import ForumUser, Topic, Favorite, Vote, Reply, Node, Notification, Plane 11 from forum.models import ForumUser, Topic, Favorite, Vote, Reply, Node, Notification, Plane
12 from forum.forms.topic import ReplyForm, CreateForm 12 from forum.forms.topic import ReplyForm, CreateForm
13 from common import find_mentions 13 from common import find_mentions
14 14
15 15
16 def get_index(request): 16 def get_index(request):
17 user = request.user 17 user = request.user
18 if user.is_authenticated(): 18 if user.is_authenticated():
19 counter = { 19 counter = {
20 'topics': user.topic_author.all().count(), 20 'topics': user.topic_author.all().count(),
21 'replies': user.reply_author.all().count(), 21 'replies': user.reply_author.all().count(),
22 'favorites': user.fav_user.all().count() 22 'favorites': user.fav_user.all().count()
23 } 23 }
24 notifications_count = user.notify_user.filter(status=0).count() 24 notifications_count = user.notify_user.filter(status=0).count()
25 25
26 status_counter = { 26 status_counter = {
27 'users': ForumUser.objects.all().count(), 27 'users': ForumUser.objects.all().count(),
28 'nodes': Node.objects.all().count(), 28 'nodes': Node.objects.all().count(),
29 'topics': Topic.objects.all().count(), 29 'topics': Topic.objects.all().count(),
30 'replies': Reply.objects.all().count(), 30 'replies': Reply.objects.all().count(),
31 } 31 }
32 32
33 try: 33 try:
34 current_page = int(request.GET.get('p', '1')) 34 current_page = int(request.GET.get('p', '1'))
35 except ValueError: 35 except ValueError:
36 current_page = 1 36 current_page = 1
37 37
38 topics, topic_page = Topic.objects.get_all_topic(current_page=current_page) 38 topics, topic_page = Topic.objects.get_all_topic(current_page=current_page)
39 planes = Plane.objects.all().prefetch_related('node_set') 39 planes = Plane.objects.all().prefetch_related('node_set')
40 # hot_nodes = Node.objects.get_all_hot_nodes() 40 # hot_nodes = Node.objects.get_all_hot_nodes()
41 hot_nodes = Node.objects.all() 41 hot_nodes = Node.objects.all()
42 active_page = 'topic' 42 active_page = 'topic'
43 return render_to_response('topic/topics.html', locals(), 43 return render_to_response('topic/topics.html', locals(),
44 context_instance=RequestContext(request)) 44 context_instance=RequestContext(request))
45 45
46 46
47 def get_view(request, topic_id, errors=None): 47 def get_view(request, topic_id, errors=None):
48 try: 48 try:
49 topic = Topic.objects.get_topic_by_topic_id(topic_id) 49 topic = Topic.objects.get_topic_by_topic_id(topic_id)
50 except Topic.DoesNotExist: 50 except Topic.DoesNotExist:
51 raise Http404 51 raise Http404
52
52 user = request.user 53 user = request.user
53 if user.is_authenticated(): 54 if user.is_authenticated():
54 counter = { 55 counter = {
55 'topics': user.topic_author.all().count(), 56 'topics': user.topic_author.all().count(),
56 'replies': user.reply_author.all().count(), 57 'replies': user.reply_author.all().count(),
57 'favorites': user.fav_user.all().count() 58 'favorites': user.fav_user.all().count()
58 } 59 }
59 notifications_count = user.notify_user.filter(status=0).count() 60 notifications_count = user.notify_user.filter(status=0).count()
60 topic_favorited = Favorite.objects.filter(involved_topic=topic, owner_user=user).exists() 61 topic_favorited = Favorite.objects.filter(involved_topic=topic, owner_user=user).exists()
61 62
62 reply_num = 106 63 reply_num = 106
63 reply_count = topic.reply_count 64 reply_count = topic.reply_count
64 reply_last_page = (reply_count // reply_num + (reply_count % reply_num and 1)) or 1 65 reply_last_page = (reply_count // reply_num + (reply_count % reply_num and 1)) or 1
65 try: 66 try:
66 current_page = int(request.GET.get('p', reply_last_page)) 67 current_page = int(request.GET.get('p', reply_last_page))
67 except ValueError: 68 except ValueError:
68 current_page = reply_last_page 69 current_page = reply_last_page
69 70
70 replies, reply_page = Reply.objects.get_all_replies_by_topic_id(topic.id, current_page=current_page, num = reply_num) 71 replies, reply_page = Reply.objects.get_all_replies_by_topic_id(topic.id, current_page=current_page, num = reply_num)
71 active_page = 'topic' 72 active_page = 'topic'
72 floor = reply_num * (current_page - 1) 73 floor = reply_num * (current_page - 1)
73 74
74 topic.reply_count = reply_page.total 75 topic.reply_count = reply_page.total
75 topic.hits = (topic.hits or 0) + 1 76 topic.hits = (topic.hits or 0) + 1
76 topic.save() 77 topic.save()
77 return render_to_response('topic/view.html', locals(), 78 return render_to_response('topic/view.html', locals(),
78 context_instance=RequestContext(request)) 79 context_instance=RequestContext(request))
79 80
80 81
81 @login_required 82 @login_required
82 def post_view(request, topic_id): 83 def post_view(request, topic_id):
83 try: 84 try:
84 topic = Topic.objects.select_related('author').get(pk=topic_id) 85 topic = Topic.objects.select_related('author').get(pk=topic_id)
85 except Topic.DoesNotExist: 86 except Topic.DoesNotExist:
86 raise Http404 87 raise Http404
87 form = ReplyForm(request.POST) 88 form = ReplyForm(request.POST)
88 if not form.is_valid(): 89 if not form.is_valid():
89 return get_view(request, topic_id, errors=form.errors) 90 return get_view(request, topic_id, errors=form.errors)
90 91
91 user = request.user 92 user = request.user
92 try: 93 try:
93 last_reply = topic.reply_set.all().order_by('-created')[0] 94 last_reply = topic.reply_set.all().order_by('-created')[0]
94 except IndexError: 95 except IndexError:
95 last_reply = None 96 last_reply = None
96 if last_reply: 97 if last_reply:
97 last_replied_fingerprint = hashlib.sha1(str(topic.id) + str(last_reply.author_id) + last_reply.content).hexdigest() 98 last_replied_fingerprint = hashlib.sha1(str(topic.id) + str(last_reply.author_id) + last_reply.content).hexdigest()
98 new_replied_fingerprint = hashlib.sha1(str(topic.id) + str(user.id) + form.cleaned_data.get('content')).hexdigest() 99 new_replied_fingerprint = hashlib.sha1(str(topic.id) + str(user.id) + form.cleaned_data.get('content')).hexdigest()
99 if last_replied_fingerprint == new_replied_fingerprint: 100 if last_replied_fingerprint == new_replied_fingerprint:
100 errors = {'duplicated_reply': [u'回复重复提交']} 101 errors = {'duplicated_reply': [u'回复重复提交']}
101 return get_view(request, topic.id, errors=errors) 102 return get_view(request, topic.id, errors=errors)
102 103
103 now = timezone.now() 104 now = timezone.now()
104 reply = Reply( 105 reply = Reply(
105 topic = topic, 106 topic = topic,
106 author = user, 107 author = user,
107 content = form.cleaned_data.get('content'), 108 content = form.cleaned_data.get('content'),
108 created = now, 109 created = now,
109 ) 110 )
110 reply.save() 111 reply.save()
111 Topic.objects.filter(pk=topic.id).update(last_replied_by=user, last_replied_time=now, last_touched=now) 112 Topic.objects.filter(pk=topic.id).update(last_replied_by=user, last_replied_time=now, last_touched=now)
112 113
113 notifications = [] 114 notifications = []
114 if user.id != topic.author.id: 115 if user.id != topic.author.id:
115 notification = Notification( 116 notification = Notification(
116 content = form.cleaned_data.get('content'), 117 content = form.cleaned_data.get('content'),
117 status = 0, 118 status = 0,
118 involved_type = 1, # 0: mention, 1: reply 119 involved_type = 1, # 0: mention, 1: reply
119 involved_user = topic.author, 120 involved_user = topic.author,
120 involved_topic = topic, 121 involved_topic = topic,
121 trigger_user = user, 122 trigger_user = user,
122 occurrence_time = now, 123 occurrence_time = now,
123 ) 124 )
124 notifications.append(notification) 125 notifications.append(notification)
125 126
126 mentions = find_mentions(form.cleaned_data.get('content')) 127 mentions = find_mentions(form.cleaned_data.get('content'))
127 if user.username in mentions: 128 if user.username in mentions:
128 mentions.remove(user.username) 129 mentions.remove(user.username)
129 if topic.author.username in mentions: 130 if topic.author.username in mentions:
130 mentions.remove(topic.author.username) 131 mentions.remove(topic.author.username)
131 if mentions: 132 if mentions:
132 mention_users = ForumUser.objects.filter(username__in=mentions) 133 mention_users = ForumUser.objects.filter(username__in=mentions)
133 if mention_users: 134 if mention_users:
134 for mention_user in mention_users: 135 for mention_user in mention_users:
135 notification = Notification( 136 notification = Notification(
136 content = form.cleaned_data.get('content'), 137 content = form.cleaned_data.get('content'),
137 status = 0, 138 status = 0,
138 involved_type = 0, # 0: mention, 1: reply 139 involved_type = 0, # 0: mention, 1: reply
139 involved_user = mention_user, 140 involved_user = mention_user,
140 involved_topic = topic, 141 involved_topic = topic,
141 trigger_user = user, 142 trigger_user = user,
142 occurrence_time = now, 143 occurrence_time = now,
143 ) 144 )
144 notifications.append(notification) 145 notifications.append(notification)
145 if notifications: 146 if notifications:
146 Notification.objects.bulk_create(notifications) 147 Notification.objects.bulk_create(notifications)
147 148
148 if user.id != topic.author.id: 149 if user.id != topic.author.id:
149 topic_time_diff = timezone.now() - topic.created 150 topic_time_diff = timezone.now() - topic.created
150 reputation = topic.author.reputation or 0 151 reputation = topic.author.reputation or 0
151 reputation = reputation + 2 * math.log(user.reputation or 0 + topic_time_diff.days + 10, 10) 152 reputation = reputation + 2 * math.log(user.reputation or 0 + topic_time_diff.days + 10, 10)
152 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation) 153 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation)
153 154
154 return redirect('/t/%s/#reply%s' % (topic.id, topic.reply_count + 1)) 155 return get_view(request, topic_id, errors=form.errors)
155 156
156 157
157 @login_required 158 @login_required
158 def get_create(request, slug=None, errors=None): 159 def get_create(request, slug=None, errors=None):
159 node = get_object_or_404(Node, slug=slug) 160 node = get_object_or_404(Node, slug=slug)
160 user = request.user 161 user = request.user
161 counter = { 162 counter = {
162 'topics': user.topic_author.all().count(), 163 'topics': user.topic_author.all().count(),
163 'replies': user.reply_author.all().count(), 164 'replies': user.reply_author.all().count(),
164 'favorites': user.fav_user.all().count() 165 'favorites': user.fav_user.all().count()
165 } 166 }
166 notifications_count = user.notify_user.filter(status=0).count() 167 notifications_count = user.notify_user.filter(status=0).count()
167 node_slug = node.slug 168 node_slug = node.slug
168 active_page = 'topic' 169 active_page = 'topic'
169 return render_to_response('topic/create.html', locals(), 170 return render_to_response('topic/create.html', locals(),
170 context_instance=RequestContext(request)) 171 context_instance=RequestContext(request))
171 172
172 173
173 @login_required 174 @login_required
174 def post_create(request, slug=None): 175 def post_create(request, slug=None):
175 node = get_object_or_404(Node, slug=slug) 176 node = get_object_or_404(Node, slug=slug)
176 177
177 form = CreateForm(request.POST) 178 form = CreateForm(request.POST)
178 if not form.is_valid(): 179 if not form.is_valid():
179 return get_create(request, slug=slug, errors=form.errors) 180 return get_create(request, slug=slug, errors=form.errors)
180 181
181 user = request.user 182 user = request.user
182 try: 183 try:
183 last_created = user.topic_author.all().order_by('-created')[0] 184 last_created = user.topic_author.all().order_by('-created')[0]
184 except IndexError: 185 except IndexError:
185 last_created = None 186 last_created = None
186 187
187 if last_created: # 如果用户最后一篇的标题内容与提交的相同 188 if last_created: # 如果用户最后一篇的标题内容与提交的相同
188 last_created_fingerprint = hashlib.sha1(last_created.title + \ 189 last_created_fingerprint = hashlib.sha1(last_created.title + \
189 last_created.content + str(last_created.node_id)).hexdigest() 190 last_created.content + str(last_created.node_id)).hexdigest()
190 new_created_fingerprint = hashlib.sha1(form.cleaned_data.get('title') + \ 191 new_created_fingerprint = hashlib.sha1(form.cleaned_data.get('title') + \
191 form.cleaned_data.get('content') + str(node.id)).hexdigest() 192 form.cleaned_data.get('content') + str(node.id)).hexdigest()
192 193
193 if last_created_fingerprint == new_created_fingerprint: 194 if last_created_fingerprint == new_created_fingerprint:
194 errors = {'duplicated_topic': [u'帖子重复提交']} 195 errors = {'duplicated_topic': [u'帖子重复提交']}
195 return get_create(request, slug=slug, errors=errors) 196 return get_create(request, slug=slug, errors=errors)
196 197
197 now = timezone.now() 198 now = timezone.now()
198 topic = Topic( 199 topic = Topic(
199 title = form.cleaned_data.get('title'), 200 title = form.cleaned_data.get('title'),
200 content = form.cleaned_data.get('content'), 201 content = form.cleaned_data.get('content'),
201 created = now, 202 created = now,
202 node = node, 203 node = node,
203 author = user, 204 author = user,
204 reply_count = 0, 205 reply_count = 0,
205 last_touched = now, 206 last_touched = now,
206 ) 207 )
207 topic.save() 208 topic.save()
208 209
209 reputation = user.reputation or 0 210 reputation = user.reputation or 0
210 reputation = reputation - 5 # 每次发布话题扣除用户威望5点 211 reputation = reputation - 5 # 每次发布话题扣除用户威望5点
211 reputation = 0 if reputation < 0 else reputation 212 reputation = 0 if reputation < 0 else reputation
212 ForumUser.objects.filter(pk=user.id).update(reputation=reputation) 213 ForumUser.objects.filter(pk=user.id).update(reputation=reputation)
213 214
214 return redirect('/') 215 return redirect('/')
215 216
216 217
217 @login_required 218 @login_required
218 def get_edit(request, topic_id, errors=None): 219 def get_edit(request, topic_id, errors=None):
219 topic = get_object_or_404(Topic, pk=topic_id) 220 topic = get_object_or_404(Topic, pk=topic_id)
220 221
221 user = request.user 222 user = request.user
222 counter = { 223 counter = {
223 'topics': user.topic_author.all().count(), 224 'topics': user.topic_author.all().count(),
224 'replies': user.reply_author.all().count(), 225 'replies': user.reply_author.all().count(),
225 'favorites': user.fav_user.all().count() 226 'favorites': user.fav_user.all().count()
226 } 227 }
227 notifications_count = user.notify_user.filter(status=0).count() 228 notifications_count = user.notify_user.filter(status=0).count()
228 229
229 active_page = 'topic' 230 active_page = 'topic'
230 return render_to_response('topic/edit.html', locals(), 231 return render_to_response('topic/edit.html', locals(),
231 context_instance=RequestContext(request)) 232 context_instance=RequestContext(request))
232 233
233 234
234 @login_required 235 @login_required
235 def post_edit(request, topic_id): 236 def post_edit(request, topic_id):
236 topic = get_object_or_404(Topic, pk=topic_id) 237 topic = get_object_or_404(Topic, pk=topic_id)
237 238
238 form = CreateForm(request.POST) 239 form = CreateForm(request.POST)
239 if not form.is_valid(): 240 if not form.is_valid():
240 return get_edit(request, topic_id, errors=form.errors) 241 return get_edit(request, topic_id, errors=form.errors)
241 242
242 user = request.user 243 user = request.user
243 if topic.author_id != user.id: 244 if topic.author_id != user.id:
244 errors = {'invalid_permission': [u'没有权限修改该主题']} 245 errors = {'invalid_permission': [u'没有权限修改该主题']}
245 return get_edit(request, topic_id, errors=errors) 246 return get_edit(request, topic_id, errors=errors)
246 247
247 now = timezone.now() 248 now = timezone.now()
248 Topic.objects.filter(pk=topic.id).update(updated=now, last_touched=now, **form.cleaned_data) 249 Topic.objects.filter(pk=topic.id).update(updated=now, last_touched=now, **form.cleaned_data)
249 250
250 reputation = user.reputation or 0 251 reputation = user.reputation or 0
251 reputation = reputation - 2 # 每次修改回复扣除用户威望2点 252 reputation = reputation - 2 # 每次修改回复扣除用户威望2点
252 reputation = 0 if reputation < 0 else reputation 253 reputation = 0 if reputation < 0 else reputation
253 ForumUser.objects.filter(pk=user.id).update(reputation=reputation) 254 ForumUser.objects.filter(pk=user.id).update(reputation=reputation)
254 255
255 return redirect('/t/%s/' % topic.id) 256 return redirect('/t/%s/' % topic.id)
256 257
257 258
258 @login_required 259 @login_required
259 def get_reply_edit(request, reply_id, errors=None): 260 def get_reply_edit(request, reply_id, errors=None):
260 reply = get_object_or_404(Reply, pk=reply_id) 261 reply = get_object_or_404(Reply, pk=reply_id)
261 user = request.user 262 user = request.user
262 counter = { 263 counter = {
263 'topics': user.topic_author.all().count(), 264 'topics': user.topic_author.all().count(),
264 'replies': user.reply_author.all().count(), 265 'replies': user.reply_author.all().count(),
265 'favorites': user.fav_user.all().count() 266 'favorites': user.fav_user.all().count()
266 } 267 }
267 notifications_count = user.notify_user.filter(status=0).count() 268 notifications_count = user.notify_user.filter(status=0).count()
268 active_page = 'topic' 269 active_page = 'topic'
269 return render_to_response('topic/reply_edit.html', locals(), 270 return render_to_response('topic/reply_edit.html', locals(),
270 context_instance=RequestContext(request)) 271 context_instance=RequestContext(request))
271 272
272 273
273 @login_required 274 @login_required
274 def post_reply_edit(request, reply_id): 275 def post_reply_edit(request, reply_id):
275 reply = get_object_or_404(Reply, pk=reply_id) 276 reply = get_object_or_404(Reply, pk=reply_id)
276 277
277 form = ReplyForm(request.POST) 278 form = ReplyForm(request.POST)
278 if not form.is_valid(): 279 if not form.is_valid():
279 return get_reply_edit(request, reply_id, errors=form.errors) 280 return get_reply_edit(request, reply_id, errors=form.errors)
280 281
281 user = request.user 282 user = request.user
282 if reply.author_id != user.id: 283 if reply.author_id != user.id:
283 errors = {'invalid_permission': [u'没有权限修改该回复']} 284 errors = {'invalid_permission': [u'没有权限修改该回复']}
284 return get_reply_edit(request, reply_id, errors=errors) 285 return get_reply_edit(request, reply_id, errors=errors)
285 286
286 Reply.objects.filter(pk=reply.id).update(updated=timezone.now(), **form.cleaned_data) 287 Reply.objects.filter(pk=reply.id).update(updated=timezone.now(), **form.cleaned_data)
287 288
288 reputation = user.reputation or 0 289 reputation = user.reputation or 0
289 reputation = reputation - 2 # 每次修改回复扣除用户威望2点 290 reputation = reputation - 2 # 每次修改回复扣除用户威望2点
290 reputation = 0 if reputation < 0 else reputation 291 reputation = 0 if reputation < 0 else reputation
291 ForumUser.objects.filter(pk=user.id).update(reputation=reputation) 292 ForumUser.objects.filter(pk=user.id).update(reputation=reputation)
292 293
293 return redirect('/t/%s/' % reply.topic_id) 294 return redirect('/t/%s/' % reply.topic_id)
294 295
295 296
296 def get_node_topics(request, slug): 297 def get_node_topics(request, slug):
297 node = get_object_or_404(Node, slug=slug) 298 node = get_object_or_404(Node, slug=slug)
298 299
299 user = request.user 300 user = request.user
300 if user.is_authenticated(): 301 if user.is_authenticated():
301 counter = { 302 counter = {
302 'topics': user.topic_author.all().count(), 303 'topics': user.topic_author.all().count(),
303 'replies': user.reply_author.all().count(), 304 'replies': user.reply_author.all().count(),
304 'favorites': user.fav_user.all().count() 305 'favorites': user.fav_user.all().count()
305 } 306 }
306 notifications_count = user.notify_user.filter(status=0).count() 307 notifications_count = user.notify_user.filter(status=0).count()
307 308
308 try: 309 try:
309 current_page = int(request.GET.get('p', '1')) 310 current_page = int(request.GET.get('p', '1'))
310 except ValueError: 311 except ValueError:
311 current_page = 1 312 current_page = 1
312 313
313 topics, topic_page = Topic.objects.get_all_topics_by_node_slug(node_slug=slug, current_page=current_page) 314 topics, topic_page = Topic.objects.get_all_topics_by_node_slug(node_slug=slug, current_page=current_page)
314 active_page = 'topic' 315 active_page = 'topic'
315 return render_to_response('topic/node_topics.html', locals(), 316 return render_to_response('topic/node_topics.html', locals(),
316 context_instance=RequestContext(request)) 317 context_instance=RequestContext(request))
317 318
318 319
319 def get_user_topics(request, uid): 320 def get_user_topics(request, uid):
320 try: 321 try:
321 if uid.isdigit(): 322 if uid.isdigit():
322 user_info = ForumUser.objects.get(pk=uid) 323 user_info = ForumUser.objects.get(pk=uid)
323 else: 324 else:
324 user_info = ForumUser.objects.get(username=uid) 325 user_info = ForumUser.objects.get(username=uid)
325 except ForumUser.DoesNotExist: 326 except ForumUser.DoesNotExist:
326 raise Http404 327 raise Http404
327 328
328 try: 329 try:
329 current_page = int(request.GET.get('p', '1')) 330 current_page = int(request.GET.get('p', '1'))
330 except ValueError: 331 except ValueError:
331 current_page = 1 332 current_page = 1
332 333
333 counter = { 334 counter = {
334 'topics': user_info.topic_author.all().count(), 335 'topics': user_info.topic_author.all().count(),
335 'replies': user_info.reply_author.all().count(), 336 'replies': user_info.reply_author.all().count(),
336 'favorites': user_info.fav_user.all().count() 337 'favorites': user_info.fav_user.all().count()
337 } 338 }
338 339
339 user = request.user 340 user = request.user
340 if user.is_authenticated(): 341 if user.is_authenticated():
341 notifications_count = user.notify_user.filter(status=0).count() 342 notifications_count = user.notify_user.filter(status=0).count()
342 343
343 topics, topic_page = Topic.objects.get_user_all_topics(user_info.id, current_page=current_page) 344 topics, topic_page = Topic.objects.get_user_all_topics(user_info.id, current_page=current_page)
344 active_page = 'topic' 345 active_page = 'topic'
345 return render_to_response('topic/user_topics.html', locals(), 346 return render_to_response('topic/user_topics.html', locals(),
346 context_instance=RequestContext(request)) 347 context_instance=RequestContext(request))
347 348
348 349
349 def get_user_replies(request, uid): 350 def get_user_replies(request, uid):
350 try: 351 try:
351 if uid.isdigit(): 352 if uid.isdigit():
352 user_info = ForumUser.objects.get(pk=uid) 353 user_info = ForumUser.objects.get(pk=uid)
353 else: 354 else:
354 user_info = ForumUser.objects.get(username=uid) 355 user_info = ForumUser.objects.get(username=uid)
355 except ForumUser.DoesNotExist: 356 except ForumUser.DoesNotExist:
356 raise Http404 357 raise Http404
357 358
358 try: 359 try:
359 current_page = int(request.GET.get('p', '1')) 360 current_page = int(request.GET.get('p', '1'))
360 except ValueError: 361 except ValueError:
361 current_page = 1 362 current_page = 1
362 363
363 counter = { 364 counter = {
364 'topics': user_info.topic_author.all().count(), 365 'topics': user_info.topic_author.all().count(),
365 'replies': user_info.reply_author.all().count(), 366 'replies': user_info.reply_author.all().count(),
366 'favorites': user_info.fav_user.all().count() 367 'favorites': user_info.fav_user.all().count()
367 } 368 }
368 369
369 user = request.user 370 user = request.user
370 if user.is_authenticated(): 371 if user.is_authenticated():
371 notifications_count = user.notify_user.filter(status=0).count() 372 notifications_count = user.notify_user.filter(status=0).count()
372 373
373 replies, reply_page = Reply.objects.get_user_all_replies(user_info.id, current_page=current_page) 374 replies, reply_page = Reply.objects.get_user_all_replies(user_info.id, current_page=current_page)
374 active_page = 'topic' 375 active_page = 'topic'
375 return render_to_response('topic/user_replies.html', locals(), 376 return render_to_response('topic/user_replies.html', locals(),
376 context_instance=RequestContext(request)) 377 context_instance=RequestContext(request))
377 378
378 379
379 def get_user_favorites(request, uid): 380 def get_user_favorites(request, uid):
380 try: 381 try:
381 if uid.isdigit(): 382 if uid.isdigit():
382 user_info = ForumUser.objects.get(pk=uid) 383 user_info = ForumUser.objects.get(pk=uid)
383 else: 384 else:
384 user_info = ForumUser.objects.get(username=uid) 385 user_info = ForumUser.objects.get(username=uid)
385 except ForumUser.DoesNotExist: 386 except ForumUser.DoesNotExist:
386 raise Http404 387 raise Http404
387 388
388 try: 389 try:
389 current_page = int(request.GET.get('p', '1')) 390 current_page = int(request.GET.get('p', '1'))
390 except ValueError: 391 except ValueError:
391 current_page = 1 392 current_page = 1
392 393
393 counter = { 394 counter = {
394 'topics': user_info.topic_author.all().count(), 395 'topics': user_info.topic_author.all().count(),
395 'replies': user_info.reply_author.all().count(), 396 'replies': user_info.reply_author.all().count(),
396 'favorites': user_info.fav_user.all().count() 397 'favorites': user_info.fav_user.all().count()
397 } 398 }
398 399
399 user = request.user 400 user = request.user
400 if user.is_authenticated(): 401 if user.is_authenticated():
401 notifications_count = user.notify_user.filter(status=0).count() 402 notifications_count = user.notify_user.filter(status=0).count()
402 403
403 favorites, favorite_page = Favorite.objects.get_user_all_favorites(user_info.id, current_page=current_page) 404 favorites, favorite_page = Favorite.objects.get_user_all_favorites(user_info.id, current_page=current_page)
404 active_page = 'topic' 405 active_page = 'topic'
405 return render_to_response('topic/user_favorites.html', locals(), 406 return render_to_response('topic/user_favorites.html', locals(),
406 context_instance=RequestContext(request)) 407 context_instance=RequestContext(request))
407 408
408 409
409 def get_profile(request, uid): 410 def get_profile(request, uid):
410 try: 411 try:
411 if uid.isdigit(): 412 if uid.isdigit():
412 user_info = ForumUser.objects.get(pk=uid) 413 user_info = ForumUser.objects.get(pk=uid)
413 else: 414 else:
414 user_info = ForumUser.objects.get(username=uid) 415 user_info = ForumUser.objects.get(username=uid)
415 except ForumUser.DoesNotExist: 416 except ForumUser.DoesNotExist:
416 raise Http404 417 raise Http404
417 418
418 try: 419 try:
419 current_page = int(request.GET.get('p', '1')) 420 current_page = int(request.GET.get('p', '1'))
420 except ValueError: 421 except ValueError:
421 current_page = 1 422 current_page = 1
422 423
423 counter = { 424 counter = {
424 'topics': user_info.topic_author.all().count(), 425 'topics': user_info.topic_author.all().count(),
425 'replies': user_info.reply_author.all().count(), 426 'replies': user_info.reply_author.all().count(),
426 'favorites': user_info.fav_user.all().count() 427 'favorites': user_info.fav_user.all().count()
427 } 428 }
428 429
429 user = request.user 430 user = request.user
430 if user.is_authenticated(): 431 if user.is_authenticated():
431 notifications_count = user.notify_user.filter(status=0).count() 432 notifications_count = user.notify_user.filter(status=0).count()
432 433
433 topics, topic_page = Topic.objects.get_user_all_topics(user_info.id, current_page=current_page) 434 topics, topic_page = Topic.objects.get_user_all_topics(user_info.id, current_page=current_page)
434 replies, reply_page = Reply.objects.get_user_all_replies(user_info.id, current_page=current_page) 435 replies, reply_page = Reply.objects.get_user_all_replies(user_info.id, current_page=current_page)
435 active_page = '_blank' 436 active_page = '_blank'
436 return render_to_response('topic/profile.html', locals(), 437 return render_to_response('topic/profile.html', locals(),
437 context_instance=RequestContext(request)) 438 context_instance=RequestContext(request))
438 439
439 440
440 def get_vote(request): 441 def get_vote(request):
441 user = request.user 442 user = request.user
442 if not user.is_authenticated(): 443 if not user.is_authenticated():
443 return HttpResponse(json.dumps({ 444 return HttpResponse(json.dumps({
444 'success': 0, 445 'success': 0,
445 'message': 'user_not_login' 446 'message': 'user_not_login'
446 }), content_type='application/json') 447 }), content_type='application/json')
447 448
448 try: 449 try:
449 topic_id = int(request.GET.get('topic_id')) 450 topic_id = int(request.GET.get('topic_id'))
450 except (TypeError, ValueError): 451 except (TypeError, ValueError):
451 topic_id = None 452 topic_id = None
452 topic = None 453 topic = None
453 if topic_id: 454 if topic_id:
454 try: 455 try:
455 topic = Topic.objects.select_related('author').get(pk=topic_id) 456 topic = Topic.objects.select_related('author').get(pk=topic_id)
456 except Topic.DoesNotExist: 457 except Topic.DoesNotExist:
457 pass 458 pass
458 459
459 if not (topic_id and topic): 460 if not (topic_id and topic):
460 return HttpResponse(json.dumps({ 461 return HttpResponse(json.dumps({
461 'success': 0, 462 'success': 0,
462 'message': 'topic_not_exist' 463 'message': 'topic_not_exist'
463 }), content_type='application/json') 464 }), content_type='application/json')
464 465
465 if user.id == topic.author.id: 466 if user.id == topic.author.id:
466 return HttpResponse(json.dumps({ 467 return HttpResponse(json.dumps({
467 'success': 0, 468 'success': 0,
468 'message': 'can_not_vote_your_topic' 469 'message': 'can_not_vote_your_topic'
469 }), content_type='application/json') 470 }), content_type='application/json')
470 471
471 if Vote.objects.filter(trigger_user=user, involved_topic=topic).exists(): 472 if Vote.objects.filter(trigger_user=user, involved_topic=topic).exists():
472 return HttpResponse(json.dumps({ 473 return HttpResponse(json.dumps({
473 'success': 0, 474 'success': 0,
474 'message': 'already_voted' 475 'message': 'already_voted'
475 }), content_type='application/json') 476 }), content_type='application/json')
476 477
477 vote = Vote(trigger_user=user, involved_type=0, involved_topic=topic, \ 478 vote = Vote(trigger_user=user, involved_type=0, involved_topic=topic, \
478 involved_user=topic.author, status=0, occurrence_time=timezone.now()) 479 involved_user=topic.author, status=0, occurrence_time=timezone.now())
479 vote.save() 480 vote.save()
480 481
481 # 更新话题作者声誉 482 # 更新话题作者声誉
482 topic_time_diff = timezone.now() - topic.created 483 topic_time_diff = timezone.now() - topic.created
483 reputation = topic.author.reputation or 0 484 reputation = topic.author.reputation or 0
484 reputation = reputation + 2 * math.log((user.reputation or 0) + topic_time_diff.days + 10, 10) 485 reputation = reputation + 2 * math.log((user.reputation or 0) + topic_time_diff.days + 10, 10)
485 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation) 486 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation)
486 487
487 return HttpResponse(json.dumps({ 488 return HttpResponse(json.dumps({
488 'success': 1, 489 'success': 1,
489 'message': 'thanks_for_your_vote' 490 'message': 'thanks_for_your_vote'
490 }), content_type='application/json') 491 }), content_type='application/json')
491 492
492 493
493 def get_favorite(request): 494 def get_favorite(request):
494 user = request.user 495 user = request.user
495 if not user.is_authenticated(): 496 if not user.is_authenticated():
496 return HttpResponse(json.dumps({ 497 return HttpResponse(json.dumps({
497 'success': 0, 498 'success': 0,
498 'message': 'user_not_login' 499 'message': 'user_not_login'
499 }), content_type='application/json') 500 }), content_type='application/json')
500 501
501 try: 502 try:
502 topic_id = int(request.GET.get('topic_id')) 503 topic_id = int(request.GET.get('topic_id'))
503 except (TypeError, ValueError): 504 except (TypeError, ValueError):
504 topic_id = None 505 topic_id = None
505 topic = None 506 topic = None
506 if topic_id: 507 if topic_id:
507 try: 508 try:
508 topic = Topic.objects.select_related('author').get(pk=topic_id) 509 topic = Topic.objects.select_related('author').get(pk=topic_id)
509 except Topic.DoesNotExist: 510 except Topic.DoesNotExist:
510 pass 511 pass
511 512
512 if not (topic_id and topic): 513 if not (topic_id and topic):
513 return HttpResponse(json.dumps({ 514 return HttpResponse(json.dumps({
514 'success': 0, 515 'success': 0,
515 'message': 'topic_not_exist' 516 'message': 'topic_not_exist'
516 }), content_type='application/json') 517 }), content_type='application/json')
517 518
518 if user.id == topic.author.id: 519 if user.id == topic.author.id:
519 return HttpResponse(json.dumps({ 520 return HttpResponse(json.dumps({
520 'success': 0, 521 'success': 0,
521 'message': 'can_not_favorite_your_topic' 522 'message': 'can_not_favorite_your_topic'
522 }), content_type='application/json') 523 }), content_type='application/json')
523 524
524 if Favorite.objects.filter(owner_user=user, involved_topic=topic).exists(): 525 if Favorite.objects.filter(owner_user=user, involved_topic=topic).exists():
525 return HttpResponse(json.dumps({ 526 return HttpResponse(json.dumps({
526 'success': 0, 527 'success': 0,
527 'message': 'already_favorited' 528 'message': 'already_favorited'
528 }), content_type='application/json') 529 }), content_type='application/json')
529 530
530 favorite = Favorite(owner_user=user, involved_type=0, involved_topic=topic, created=timezone.now()) 531 favorite = Favorite(owner_user=user, involved_type=0, involved_topic=topic, created=timezone.now())
531 favorite.save() 532 favorite.save()
532 533
533 # 更新话题作者声誉 534 # 更新话题作者声誉
534 topic_time_diff = timezone.now() - topic.created 535 topic_time_diff = timezone.now() - topic.created
535 reputation = topic.author.reputation or 0 536 reputation = topic.author.reputation or 0
536 reputation = reputation + 2 * math.log((user.reputation or 0) + topic_time_diff.days + 10, 10) 537 reputation = reputation + 2 * math.log((user.reputation or 0) + topic_time_diff.days + 10, 10)
537 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation) 538 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation)
538 539
539 return HttpResponse(json.dumps({ 540 return HttpResponse(json.dumps({
540 'success': 1, 541 'success': 1,
541 'message': 'cancel_favorite_success' 542 'message': 'cancel_favorite_success'
542 }), content_type='application/json') 543 }), content_type='application/json')
543 544
544 545
545 def get_cancel_favorite(request): 546 def get_cancel_favorite(request):
546 user = request.user 547 user = request.user
547 if not user.is_authenticated(): 548 if not user.is_authenticated():
548 return HttpResponse(json.dumps({ 549 return HttpResponse(json.dumps({
549 'success': 0, 550 'success': 0,
550 'message': 'user_not_login' 551 'message': 'user_not_login'
551 }), content_type='application/json') 552 }), content_type='application/json')
552 553
553 try: 554 try:
554 topic_id = int(request.GET.get('topic_id')) 555 topic_id = int(request.GET.get('topic_id'))
555 except (TypeError, ValueError): 556 except (TypeError, ValueError):
556 topic_id = None 557 topic_id = None
557 topic = None 558 topic = None
558 if topic_id: 559 if topic_id:
559 try: 560 try:
560 topic = Topic.objects.select_related('author').get(pk=topic_id) 561 topic = Topic.objects.select_related('author').get(pk=topic_id)
561 except Topic.DoesNotExist: 562 except Topic.DoesNotExist:
562 pass 563 pass
563 564
564 if not (topic_id and topic): 565 if not (topic_id and topic):
565 return HttpResponse(json.dumps({ 566 return HttpResponse(json.dumps({
566 'success': 0, 567 'success': 0,
567 'message': 'topic_not_exist' 568 'message': 'topic_not_exist'
568 }), content_type='application/json') 569 }), content_type='application/json')
569 570
570 try: 571 try:
571 favorite = Favorite.objects.get(owner_user=user, involved_topic=topic) 572 favorite = Favorite.objects.get(owner_user=user, involved_topic=topic)
572 except Favorite.DoesNotExist: 573 except Favorite.DoesNotExist:
573 favorite = None 574 favorite = None
574 575
575 if not favorite: 576 if not favorite:
576 return HttpResponse(json.dumps({ 577 return HttpResponse(json.dumps({
577 'success': 0, 578 'success': 0,
578 'message': 'not_been_favorited' 579 'message': 'not_been_favorited'
579 }), content_type='application/json') 580 }), content_type='application/json')
580 581
581 favorite.delete() 582 favorite.delete()
582 583
583 # 更新话题作者声誉 584 # 更新话题作者声誉
584 topic_time_diff = timezone.now() - topic.created 585 topic_time_diff = timezone.now() - topic.created
585 reputation = topic.author.reputation or 0 586 reputation = topic.author.reputation or 0
586 reputation = reputation - math.log(user.reputation or 0 + topic_time_diff.days + 10, 15) 587 reputation = reputation - math.log(user.reputation or 0 + topic_time_diff.days + 10, 15)
587 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation) 588 ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation)
588 589
589 return HttpResponse(json.dumps({ 590 return HttpResponse(json.dumps({
590 'success': 1, 591 'success': 1,
591 'message': 'cancel_favorite_success' 592 'message': 'cancel_favorite_success'
592 }), content_type='application/json') 593 }), content_type='application/json')
593 594
594 595
595 def get_members(request): 596 def get_members(request):
596 user = request.user 597 user = request.user
597 if user.is_authenticated(): 598 if user.is_authenticated():
598 counter = { 599 counter = {
599 'topics': user.topic_author.all().count(), 600 'topics': user.topic_author.all().count(),
600 'replies': user.reply_author.all().count(), 601 'replies': user.reply_author.all().count(),
601 'favorites': user.fav_user.all().count() 602 'favorites': user.fav_user.all().count()
602 } 603 }
603 notifications_count = user.notify_user.filter(status=0).count() 604 notifications_count = user.notify_user.filter(status=0).count()
604 605
605 members = ForumUser.objects.all().order_by('-id')[:49] 606 members = ForumUser.objects.all().order_by('-id')[:49]
606 active_members = ForumUser.objects.all().order_by('-last_login')[:49] 607 active_members = ForumUser.objects.all().order_by('-last_login')[:49]
607 active_page = 'members' 608 active_page = 'members'
608 return render_to_response('topic/members.html', locals(), 609 return render_to_response('topic/members.html', locals(),
609 context_instance=RequestContext(request)) 610 context_instance=RequestContext(request))
610 611