bgneal@232
|
1 """
|
bgneal@232
|
2 Views for the forums application.
|
bgneal@680
|
3
|
bgneal@232
|
4 """
|
bgneal@329
|
5 import collections
|
bgneal@232
|
6 import datetime
|
bgneal@232
|
7
|
bgneal@232
|
8 from django.contrib.auth.decorators import login_required
|
bgneal@232
|
9 from django.contrib.auth.models import User
|
bgneal@232
|
10 from django.http import Http404
|
bgneal@232
|
11 from django.http import HttpResponse
|
bgneal@232
|
12 from django.http import HttpResponseBadRequest
|
bgneal@232
|
13 from django.http import HttpResponseForbidden
|
bgneal@232
|
14 from django.http import HttpResponseRedirect
|
bgneal@232
|
15 from django.core.urlresolvers import reverse
|
bgneal@232
|
16 from django.core.paginator import InvalidPage
|
bgneal@232
|
17 from django.shortcuts import get_object_or_404
|
bgneal@1031
|
18 from django.shortcuts import render
|
bgneal@232
|
19 from django.template.loader import render_to_string
|
bgneal@232
|
20 from django.views.decorators.http import require_POST
|
bgneal@232
|
21 from django.db.models import F
|
bgneal@232
|
22
|
bgneal@459
|
23 import antispam
|
bgneal@459
|
24 import antispam.utils
|
bgneal@791
|
25 from bio.models import BadgeOwnership
|
bgneal@232
|
26 from core.paginator import DiggPaginator
|
bgneal@566
|
27 from core.functions import email_admins, quote_message
|
bgneal@232
|
28
|
bgneal@459
|
29 from forums.models import (Forum, Topic, Post, FlaggedPost, TopicLastVisit,
|
bgneal@791
|
30 ForumLastVisit)
|
bgneal@459
|
31 from forums.forms import (NewTopicForm, NewPostForm, PostForm, MoveTopicForm,
|
bgneal@459
|
32 SplitTopicForm)
|
bgneal@459
|
33 from forums.unread import (get_forum_unread_status, get_topic_unread_status,
|
bgneal@459
|
34 get_post_unread_status, get_unread_topics)
|
bgneal@459
|
35
|
bgneal@459
|
36 import forums.permissions as perms
|
bgneal@469
|
37 from forums.signals import (notify_new_topic, notify_updated_topic,
|
bgneal@469
|
38 notify_new_post, notify_updated_post)
|
bgneal@522
|
39 from forums.latest import get_latest_topic_ids
|
bgneal@232
|
40
|
bgneal@232
|
41 #######################################################################
|
bgneal@232
|
42
|
bgneal@232
|
43 TOPICS_PER_PAGE = 50
|
bgneal@232
|
44 POSTS_PER_PAGE = 20
|
bgneal@263
|
45 FEED_BASE = '/feeds/forums/'
|
bgneal@263
|
46 FORUM_FEED = FEED_BASE + '%s/'
|
bgneal@232
|
47
|
bgneal@232
|
48
|
bgneal@232
|
49 def get_page_num(request):
|
bgneal@232
|
50 """Returns the value of the 'page' variable in GET if it exists, or 1
|
bgneal@232
|
51 if it does not."""
|
bgneal@232
|
52
|
bgneal@232
|
53 try:
|
bgneal@232
|
54 page_num = int(request.GET.get('page', 1))
|
bgneal@232
|
55 except ValueError:
|
bgneal@232
|
56 page_num = 1
|
bgneal@232
|
57
|
bgneal@232
|
58 return page_num
|
bgneal@232
|
59
|
bgneal@232
|
60
|
bgneal@232
|
61 def create_topic_paginator(topics):
|
bgneal@232
|
62 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@232
|
63
|
bgneal@232
|
64 def create_post_paginator(posts):
|
bgneal@232
|
65 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@232
|
66
|
bgneal@232
|
67
|
bgneal@232
|
68 def attach_topic_page_ranges(topics):
|
bgneal@232
|
69 """Attaches a page_range attribute to each topic in the supplied list.
|
bgneal@232
|
70 This attribute will be None if it is a single page topic. This is used
|
bgneal@232
|
71 by the templates to generate "goto page x" links.
|
bgneal@232
|
72 """
|
bgneal@232
|
73 for topic in topics:
|
bgneal@232
|
74 if topic.post_count > POSTS_PER_PAGE:
|
bgneal@286
|
75 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE,
|
bgneal@232
|
76 body=2, tail=3, margin=1)
|
bgneal@232
|
77 topic.page_range = pp.page(1).page_range
|
bgneal@232
|
78 else:
|
bgneal@232
|
79 topic.page_range = None
|
bgneal@232
|
80
|
bgneal@232
|
81 #######################################################################
|
bgneal@232
|
82
|
bgneal@232
|
83 def index(request):
|
bgneal@232
|
84 """
|
bgneal@232
|
85 This view displays all the forums available, ordered in each category.
|
bgneal@232
|
86 """
|
bgneal@232
|
87 public_forums = Forum.objects.public_forums()
|
bgneal@263
|
88 feeds = [{'name': 'All Forums', 'feed': FEED_BASE}]
|
bgneal@232
|
89
|
bgneal@232
|
90 forums = Forum.objects.forums_for_user(request.user)
|
bgneal@232
|
91 get_forum_unread_status(forums, request.user)
|
bgneal@232
|
92 cats = {}
|
bgneal@232
|
93 for forum in forums:
|
bgneal@232
|
94 forum.has_feed = forum in public_forums
|
bgneal@232
|
95 if forum.has_feed:
|
bgneal@232
|
96 feeds.append({
|
bgneal@232
|
97 'name': '%s Forum' % forum.name,
|
bgneal@263
|
98 'feed': FORUM_FEED % forum.slug,
|
bgneal@232
|
99 })
|
bgneal@232
|
100
|
bgneal@232
|
101 cat = cats.setdefault(forum.category.id, {
|
bgneal@232
|
102 'cat': forum.category,
|
bgneal@232
|
103 'forums': [],
|
bgneal@232
|
104 })
|
bgneal@232
|
105 cat['forums'].append(forum)
|
bgneal@232
|
106
|
bgneal@232
|
107 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@232
|
108 cats = sorted(cats.values(), cmpdef)
|
bgneal@232
|
109
|
bgneal@1031
|
110 return render(request, 'forums/index.html', {
|
bgneal@232
|
111 'cats': cats,
|
bgneal@232
|
112 'feeds': feeds,
|
bgneal@1161
|
113 'V3_DESIGN': True,
|
bgneal@1031
|
114 })
|
bgneal@232
|
115
|
bgneal@232
|
116
|
bgneal@232
|
117 def forum_index(request, slug):
|
bgneal@232
|
118 """
|
bgneal@232
|
119 Displays all the topics in a forum.
|
bgneal@232
|
120 """
|
bgneal@232
|
121 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@232
|
122
|
bgneal@459
|
123 if not perms.can_access(forum.category, request.user):
|
bgneal@232
|
124 return HttpResponseForbidden()
|
bgneal@232
|
125
|
bgneal@263
|
126 feed = None
|
bgneal@263
|
127 if not forum.category.groups.all():
|
bgneal@263
|
128 feed = {
|
bgneal@263
|
129 'name': '%s Forum' % forum.name,
|
bgneal@263
|
130 'feed': FORUM_FEED % forum.slug,
|
bgneal@263
|
131 }
|
bgneal@263
|
132
|
bgneal@232
|
133 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@232
|
134 paginator = create_topic_paginator(topics)
|
bgneal@232
|
135 page_num = get_page_num(request)
|
bgneal@232
|
136 try:
|
bgneal@232
|
137 page = paginator.page(page_num)
|
bgneal@232
|
138 except InvalidPage:
|
bgneal@232
|
139 raise Http404
|
bgneal@232
|
140
|
bgneal@328
|
141 get_topic_unread_status(forum, page.object_list, request.user)
|
bgneal@232
|
142 attach_topic_page_ranges(page.object_list)
|
bgneal@232
|
143
|
bgneal@459
|
144 can_moderate = perms.can_moderate(forum, request.user)
|
bgneal@286
|
145
|
bgneal@1031
|
146 return render(request, 'forums/forum_index.html', {
|
bgneal@232
|
147 'forum': forum,
|
bgneal@263
|
148 'feed': feed,
|
bgneal@232
|
149 'page': page,
|
bgneal@232
|
150 'can_moderate': can_moderate,
|
bgneal@1161
|
151 'V3_DESIGN': True,
|
bgneal@1031
|
152 })
|
bgneal@232
|
153
|
bgneal@232
|
154
|
bgneal@232
|
155 def topic_index(request, id):
|
bgneal@232
|
156 """
|
bgneal@232
|
157 Displays all the posts in a topic.
|
bgneal@232
|
158 """
|
bgneal@232
|
159 topic = get_object_or_404(Topic.objects.select_related(
|
bgneal@232
|
160 'forum', 'forum__category', 'last_post'), pk=id)
|
bgneal@232
|
161
|
bgneal@459
|
162 if not perms.can_access(topic.forum.category, request.user):
|
bgneal@232
|
163 return HttpResponseForbidden()
|
bgneal@232
|
164
|
bgneal@232
|
165 topic.view_count = F('view_count') + 1
|
bgneal@232
|
166 topic.save(force_update=True)
|
bgneal@232
|
167
|
bgneal@791
|
168 posts = topic.posts.\
|
bgneal@791
|
169 select_related('user', 'user__profile').\
|
bgneal@791
|
170 prefetch_related('attachments')
|
bgneal@232
|
171
|
bgneal@232
|
172 paginator = create_post_paginator(posts)
|
bgneal@232
|
173 page_num = get_page_num(request)
|
bgneal@232
|
174 try:
|
bgneal@232
|
175 page = paginator.page(page_num)
|
bgneal@232
|
176 except InvalidPage:
|
bgneal@232
|
177 raise Http404
|
bgneal@232
|
178 get_post_unread_status(topic, page.object_list, request.user)
|
bgneal@232
|
179
|
bgneal@791
|
180 # Get the BadgeOwnership & Badges for each user who has posted in the
|
bgneal@791
|
181 # thread. This is done to save SQL queries in the template.
|
bgneal@232
|
182
|
bgneal@445
|
183 last_post_on_page = None
|
bgneal@791
|
184 profile_ids = []
|
bgneal@232
|
185 for post in page.object_list:
|
bgneal@445
|
186 last_post_on_page = post
|
bgneal@791
|
187 profile_ids.append(post.user.profile.pk)
|
bgneal@285
|
188
|
bgneal@791
|
189 bo_qs = BadgeOwnership.objects.filter(profile__in=profile_ids).\
|
bgneal@791
|
190 select_related()
|
bgneal@329
|
191 bos = collections.defaultdict(list)
|
bgneal@791
|
192 for bo in bo_qs:
|
bgneal@791
|
193 bos[bo.profile.pk].append(bo)
|
bgneal@329
|
194
|
bgneal@791
|
195 for post in page.object_list:
|
bgneal@791
|
196 post.user.profile.badge_ownership = bos[post.user.profile.pk]
|
bgneal@232
|
197
|
bgneal@232
|
198 last_page = page_num == paginator.num_pages
|
bgneal@232
|
199
|
bgneal@445
|
200 if request.user.is_authenticated():
|
bgneal@445
|
201 if last_page or last_post_on_page is None:
|
bgneal@445
|
202 visit_time = datetime.datetime.now()
|
bgneal@445
|
203 else:
|
bgneal@445
|
204 visit_time = last_post_on_page.creation_date
|
bgneal@445
|
205 _update_last_visit(request.user, topic, visit_time)
|
bgneal@232
|
206
|
bgneal@459
|
207 can_moderate = perms.can_moderate(topic.forum, request.user)
|
bgneal@232
|
208
|
bgneal@232
|
209 can_reply = request.user.is_authenticated() and (
|
bgneal@232
|
210 not topic.locked or can_moderate)
|
bgneal@232
|
211
|
bgneal@232
|
212 is_favorite = request.user.is_authenticated() and (
|
bgneal@232
|
213 topic in request.user.favorite_topics.all())
|
bgneal@232
|
214
|
bgneal@232
|
215 is_subscribed = request.user.is_authenticated() and (
|
bgneal@232
|
216 topic in request.user.subscriptions.all())
|
bgneal@232
|
217
|
bgneal@1031
|
218 return render(request, 'forums/topic.html', {
|
bgneal@232
|
219 'forum': topic.forum,
|
bgneal@232
|
220 'topic': topic,
|
bgneal@232
|
221 'page': page,
|
bgneal@232
|
222 'last_page': last_page,
|
bgneal@232
|
223 'can_moderate': can_moderate,
|
bgneal@232
|
224 'can_reply': can_reply,
|
bgneal@232
|
225 'form': NewPostForm(initial={'topic_id': topic.id}),
|
bgneal@232
|
226 'is_favorite': is_favorite,
|
bgneal@232
|
227 'is_subscribed': is_subscribed,
|
bgneal@1164
|
228 'V3_DESIGN': True,
|
bgneal@1031
|
229 })
|
bgneal@232
|
230
|
bgneal@232
|
231
|
bgneal@374
|
232 def topic_unread(request, id):
|
bgneal@374
|
233 """
|
bgneal@374
|
234 This view redirects to the first post the user hasn't read, if we can
|
bgneal@374
|
235 figure that out. Otherwise we redirect to the topic.
|
bgneal@374
|
236
|
bgneal@374
|
237 """
|
bgneal@374
|
238 topic_url = reverse('forums-topic_index', kwargs={'id': id})
|
bgneal@374
|
239
|
bgneal@374
|
240 if request.user.is_authenticated():
|
bgneal@680
|
241 topic = get_object_or_404(Topic.objects.select_related('forum', 'user'), pk=id)
|
bgneal@374
|
242 try:
|
bgneal@376
|
243 tlv = TopicLastVisit.objects.get(user=request.user, topic=topic)
|
bgneal@374
|
244 except TopicLastVisit.DoesNotExist:
|
bgneal@376
|
245 try:
|
bgneal@376
|
246 flv = ForumLastVisit.objects.get(user=request.user,
|
bgneal@376
|
247 forum=topic.forum)
|
bgneal@376
|
248 except ForumLastVisit.DoesNotExist:
|
bgneal@376
|
249 return HttpResponseRedirect(topic_url)
|
bgneal@376
|
250 else:
|
bgneal@376
|
251 last_visit = flv.begin_date
|
bgneal@376
|
252 else:
|
bgneal@376
|
253 last_visit = tlv.last_visit
|
bgneal@374
|
254
|
bgneal@376
|
255 posts = Post.objects.filter(topic=topic, creation_date__gt=last_visit)
|
bgneal@374
|
256 if posts:
|
bgneal@374
|
257 return _goto_post(posts[0])
|
bgneal@374
|
258 else:
|
bgneal@374
|
259 # just go to the last post in the topic
|
bgneal@374
|
260 return _goto_post(topic.last_post)
|
bgneal@374
|
261
|
bgneal@374
|
262 # user isn't authenticated, just go to the topic
|
bgneal@374
|
263 return HttpResponseRedirect(topic_url)
|
bgneal@374
|
264
|
bgneal@374
|
265
|
bgneal@529
|
266 def topic_latest(request, id):
|
bgneal@529
|
267 """
|
bgneal@529
|
268 This view shows the latest (last) post in a given topic.
|
bgneal@529
|
269
|
bgneal@529
|
270 """
|
bgneal@680
|
271 topic = get_object_or_404(Topic.objects.select_related('forum', 'user'), pk=id)
|
bgneal@529
|
272
|
bgneal@529
|
273 if topic.last_post:
|
bgneal@529
|
274 return _goto_post(topic.last_post)
|
bgneal@529
|
275
|
bgneal@529
|
276 raise Http404
|
bgneal@529
|
277
|
bgneal@529
|
278
|
bgneal@232
|
279 @login_required
|
bgneal@232
|
280 def new_topic(request, slug):
|
bgneal@232
|
281 """
|
bgneal@232
|
282 This view handles the creation of new topics.
|
bgneal@232
|
283 """
|
bgneal@232
|
284 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@232
|
285
|
bgneal@459
|
286 if not perms.can_access(forum.category, request.user):
|
bgneal@232
|
287 return HttpResponseForbidden()
|
bgneal@232
|
288
|
bgneal@232
|
289 if request.method == 'POST':
|
bgneal@232
|
290 form = NewTopicForm(request.user, forum, request.POST)
|
bgneal@232
|
291 if form.is_valid():
|
bgneal@232
|
292 if antispam.utils.spam_check(request, form.cleaned_data['body']):
|
bgneal@232
|
293 return HttpResponseRedirect(reverse('antispam-suspended'))
|
bgneal@232
|
294
|
bgneal@232
|
295 topic = form.save(request.META.get("REMOTE_ADDR"))
|
bgneal@232
|
296 _bump_post_count(request.user)
|
bgneal@232
|
297 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@232
|
298 kwargs={'tid': topic.pk}))
|
bgneal@232
|
299 else:
|
bgneal@232
|
300 form = NewTopicForm(request.user, forum)
|
bgneal@286
|
301
|
bgneal@1031
|
302 return render(request, 'forums/new_topic.html', {
|
bgneal@232
|
303 'forum': forum,
|
bgneal@232
|
304 'form': form,
|
bgneal@1031
|
305 })
|
bgneal@232
|
306
|
bgneal@232
|
307
|
bgneal@232
|
308 @login_required
|
bgneal@232
|
309 def new_topic_thanks(request, tid):
|
bgneal@232
|
310 """
|
bgneal@232
|
311 This view displays the success page for a newly created topic.
|
bgneal@232
|
312 """
|
bgneal@232
|
313 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
|
bgneal@1031
|
314 return render(request, 'forums/new_topic_thanks.html', {
|
bgneal@232
|
315 'forum': topic.forum,
|
bgneal@232
|
316 'topic': topic,
|
bgneal@1031
|
317 })
|
bgneal@232
|
318
|
bgneal@232
|
319
|
bgneal@232
|
320 @require_POST
|
bgneal@232
|
321 def quick_reply_ajax(request):
|
bgneal@232
|
322 """
|
bgneal@232
|
323 This function handles the quick reply to a thread function. This
|
bgneal@232
|
324 function is meant to be the target of an AJAX post, and returns
|
bgneal@232
|
325 the HTML for the new post, which the client-side script appends
|
bgneal@232
|
326 to the document.
|
bgneal@232
|
327 """
|
bgneal@232
|
328 if not request.user.is_authenticated():
|
bgneal@232
|
329 return HttpResponseForbidden('Please login or register to post.')
|
bgneal@232
|
330
|
bgneal@232
|
331 form = NewPostForm(request.POST)
|
bgneal@232
|
332 if form.is_valid():
|
bgneal@459
|
333 if not perms.can_post(form.topic, request.user):
|
bgneal@232
|
334 return HttpResponseForbidden("You don't have permission to post in this topic.")
|
bgneal@232
|
335 if antispam.utils.spam_check(request, form.cleaned_data['body']):
|
bgneal@232
|
336 return HttpResponseForbidden(antispam.BUSTED_MESSAGE)
|
bgneal@232
|
337
|
bgneal@232
|
338 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
|
bgneal@232
|
339 post.unread = True
|
bgneal@285
|
340 post.attach_list = post.attachments.all()
|
bgneal@232
|
341 _bump_post_count(request.user)
|
bgneal@446
|
342 _update_last_visit(request.user, form.topic, datetime.datetime.now())
|
bgneal@285
|
343
|
bgneal@1031
|
344 return render(request, 'forums/display_post.html', {
|
bgneal@232
|
345 'post': post,
|
bgneal@459
|
346 'can_moderate': perms.can_moderate(form.topic.forum, request.user),
|
bgneal@232
|
347 'can_reply': True,
|
bgneal@1031
|
348 })
|
bgneal@232
|
349
|
bgneal@963
|
350 # The client side javascript is pretty simplistic right now and we don't
|
bgneal@963
|
351 # want to change it yet. It is expecting a single error string. Just grab
|
bgneal@963
|
352 # the first error message and use that.
|
bgneal@963
|
353 errors = form.errors.as_data()
|
bgneal@963
|
354 msg = errors.values()[0][0].message if errors else 'Unknown error'
|
bgneal@963
|
355 return HttpResponseBadRequest(msg)
|
bgneal@232
|
356
|
bgneal@232
|
357
|
bgneal@374
|
358 def _goto_post(post):
|
bgneal@374
|
359 """
|
bgneal@374
|
360 Calculate what page the given post is on in its parent topic, then
|
bgneal@374
|
361 return a redirect to it.
|
bgneal@374
|
362
|
bgneal@374
|
363 """
|
bgneal@374
|
364 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@374
|
365 page = count / POSTS_PER_PAGE + 1
|
bgneal@374
|
366 url = (reverse('forums-topic_index', kwargs={'id': post.topic.id}) +
|
bgneal@374
|
367 '?page=%s#p%s' % (page, post.id))
|
bgneal@374
|
368 return HttpResponseRedirect(url)
|
bgneal@374
|
369
|
bgneal@374
|
370
|
bgneal@232
|
371 def goto_post(request, post_id):
|
bgneal@232
|
372 """
|
bgneal@232
|
373 This function calculates what page a given post is on, then redirects
|
bgneal@232
|
374 to that URL. This function is the target of get_absolute_url() for
|
bgneal@232
|
375 Post objects.
|
bgneal@232
|
376 """
|
bgneal@232
|
377 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@374
|
378 return _goto_post(post)
|
bgneal@232
|
379
|
bgneal@232
|
380
|
bgneal@232
|
381 @require_POST
|
bgneal@232
|
382 def flag_post(request):
|
bgneal@232
|
383 """
|
bgneal@232
|
384 This function handles the flagging of posts by users. This function should
|
bgneal@232
|
385 be the target of an AJAX post.
|
bgneal@232
|
386 """
|
bgneal@232
|
387 if not request.user.is_authenticated():
|
bgneal@232
|
388 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@232
|
389
|
bgneal@232
|
390 id = request.POST.get('id')
|
bgneal@232
|
391 if id is None:
|
bgneal@232
|
392 return HttpResponseBadRequest('No post id')
|
bgneal@232
|
393
|
bgneal@232
|
394 try:
|
bgneal@232
|
395 post = Post.objects.get(pk=id)
|
bgneal@232
|
396 except Post.DoesNotExist:
|
bgneal@232
|
397 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@232
|
398
|
bgneal@232
|
399 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@232
|
400 flag.save()
|
bgneal@232
|
401 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@232
|
402
|
bgneal@232
|
403 A user has flagged a forum post for review.
|
bgneal@232
|
404 """)
|
bgneal@232
|
405 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@232
|
406 'Thanks for helping to improve the discussions on this site.')
|
bgneal@232
|
407
|
bgneal@232
|
408
|
bgneal@232
|
409 @login_required
|
bgneal@232
|
410 def edit_post(request, id):
|
bgneal@232
|
411 """
|
bgneal@232
|
412 This view function allows authorized users to edit posts.
|
bgneal@232
|
413 The superuser, forum moderators, and original author can edit posts.
|
bgneal@232
|
414 """
|
bgneal@232
|
415 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@232
|
416
|
bgneal@459
|
417 can_moderate = perms.can_moderate(post.topic.forum, request.user)
|
bgneal@232
|
418 can_edit = can_moderate or request.user == post.user
|
bgneal@232
|
419
|
bgneal@232
|
420 if not can_edit:
|
bgneal@232
|
421 return HttpResponseForbidden("You don't have permission to edit that post.")
|
bgneal@232
|
422
|
bgneal@295
|
423 topic_name = None
|
bgneal@295
|
424 first_post = Post.objects.filter(topic=post.topic).order_by('creation_date')[0]
|
bgneal@295
|
425 if first_post.id == post.id:
|
bgneal@295
|
426 topic_name = post.topic.name
|
bgneal@295
|
427
|
bgneal@232
|
428 if request.method == "POST":
|
bgneal@295
|
429 form = PostForm(request.POST, instance=post, topic_name=topic_name)
|
bgneal@232
|
430 if form.is_valid():
|
bgneal@232
|
431 if antispam.utils.spam_check(request, form.cleaned_data['body']):
|
bgneal@232
|
432 return HttpResponseRedirect(reverse('antispam-suspended'))
|
bgneal@232
|
433 post = form.save(commit=False)
|
bgneal@232
|
434 post.touch()
|
bgneal@963
|
435 post.save(html=form.body_html)
|
bgneal@469
|
436 notify_updated_post(post)
|
bgneal@285
|
437
|
bgneal@295
|
438 # if we are editing a first post, save the parent topic as well
|
bgneal@295
|
439 if topic_name:
|
bgneal@295
|
440 post.topic.save()
|
bgneal@469
|
441 notify_updated_topic(post.topic)
|
bgneal@295
|
442
|
bgneal@285
|
443 # Save any attachments
|
bgneal@286
|
444 form.attach_proc.save_attachments(post)
|
bgneal@285
|
445
|
bgneal@232
|
446 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@232
|
447 else:
|
bgneal@295
|
448 form = PostForm(instance=post, topic_name=topic_name)
|
bgneal@232
|
449
|
bgneal@1031
|
450 return render(request, 'forums/edit_post.html', {
|
bgneal@232
|
451 'forum': post.topic.forum,
|
bgneal@232
|
452 'topic': post.topic,
|
bgneal@232
|
453 'post': post,
|
bgneal@232
|
454 'form': form,
|
bgneal@232
|
455 'can_moderate': can_moderate,
|
bgneal@1031
|
456 })
|
bgneal@232
|
457
|
bgneal@232
|
458
|
bgneal@232
|
459 @require_POST
|
bgneal@232
|
460 def delete_post(request):
|
bgneal@232
|
461 """
|
bgneal@232
|
462 This view function allows superusers and forum moderators to delete posts.
|
bgneal@232
|
463 This function is the target of AJAX calls from the client.
|
bgneal@232
|
464 """
|
bgneal@232
|
465 if not request.user.is_authenticated():
|
bgneal@232
|
466 return HttpResponseForbidden('Please login to delete a post.')
|
bgneal@232
|
467
|
bgneal@232
|
468 id = request.POST.get('id')
|
bgneal@232
|
469 if id is None:
|
bgneal@232
|
470 return HttpResponseBadRequest('No post id')
|
bgneal@232
|
471
|
bgneal@232
|
472 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@232
|
473
|
bgneal@460
|
474 if not perms.can_moderate(post.topic.forum, request.user):
|
bgneal@232
|
475 return HttpResponseForbidden("You don't have permission to delete that post.")
|
bgneal@232
|
476
|
bgneal@232
|
477 delete_single_post(post)
|
bgneal@232
|
478 return HttpResponse("The post has been deleted.")
|
bgneal@232
|
479
|
bgneal@232
|
480
|
bgneal@232
|
481 def delete_single_post(post):
|
bgneal@232
|
482 """
|
bgneal@232
|
483 This function deletes a single post. It handles the case of where
|
bgneal@232
|
484 a post is the sole post in a topic by deleting the topic also. It
|
bgneal@232
|
485 adjusts any foreign keys in Topic or Forum objects that might be pointing
|
bgneal@232
|
486 to this post before deleting the post to avoid a cascading delete.
|
bgneal@232
|
487 """
|
bgneal@232
|
488 if post.topic.post_count == 1 and post == post.topic.last_post:
|
bgneal@232
|
489 _delete_topic(post.topic)
|
bgneal@232
|
490 else:
|
bgneal@232
|
491 _delete_post(post)
|
bgneal@232
|
492
|
bgneal@232
|
493
|
bgneal@232
|
494 def _delete_post(post):
|
bgneal@232
|
495 """
|
bgneal@232
|
496 Internal function to delete a single post object.
|
bgneal@232
|
497 Decrements the post author's post count.
|
bgneal@232
|
498 Adjusts the parent topic and forum's last_post as needed.
|
bgneal@232
|
499 """
|
bgneal@232
|
500 # Adjust post creator's post count
|
bgneal@789
|
501 profile = post.user.profile
|
bgneal@232
|
502 if profile.forum_post_count > 0:
|
bgneal@232
|
503 profile.forum_post_count -= 1
|
bgneal@562
|
504 profile.save(content_update=False)
|
bgneal@232
|
505
|
bgneal@232
|
506 # If this post is the last_post in a topic, we need to update
|
bgneal@232
|
507 # both the topic and parent forum's last post fields. If we don't
|
bgneal@232
|
508 # the cascading delete will delete them also!
|
bgneal@232
|
509
|
bgneal@232
|
510 topic = post.topic
|
bgneal@232
|
511 if topic.last_post == post:
|
bgneal@232
|
512 topic.last_post_pre_delete()
|
bgneal@232
|
513 topic.save()
|
bgneal@232
|
514
|
bgneal@232
|
515 forum = topic.forum
|
bgneal@232
|
516 if forum.last_post == post:
|
bgneal@232
|
517 forum.last_post_pre_delete()
|
bgneal@232
|
518 forum.save()
|
bgneal@232
|
519
|
bgneal@285
|
520 # delete any attachments
|
bgneal@285
|
521 post.attachments.clear()
|
bgneal@285
|
522
|
bgneal@232
|
523 # Should be safe to delete the post now:
|
bgneal@232
|
524 post.delete()
|
bgneal@232
|
525
|
bgneal@232
|
526
|
bgneal@232
|
527 def _delete_topic(topic):
|
bgneal@232
|
528 """
|
bgneal@232
|
529 Internal function to delete an entire topic.
|
bgneal@232
|
530 Deletes the topic and all posts contained within.
|
bgneal@232
|
531 Adjusts the parent forum's last_post as needed.
|
bgneal@232
|
532 Note that we don't bother adjusting all the users'
|
bgneal@232
|
533 post counts as that doesn't seem to be worth the effort.
|
bgneal@232
|
534 """
|
bgneal@318
|
535 parent_forum = topic.forum
|
bgneal@318
|
536 if parent_forum.last_post and parent_forum.last_post.topic == topic:
|
bgneal@318
|
537 parent_forum.last_post_pre_delete(deleting_topic=True)
|
bgneal@318
|
538 parent_forum.save()
|
bgneal@232
|
539
|
bgneal@232
|
540 # delete subscriptions to this topic
|
bgneal@232
|
541 topic.subscribers.clear()
|
bgneal@232
|
542 topic.bookmarkers.clear()
|
bgneal@232
|
543
|
bgneal@285
|
544 # delete all attachments
|
bgneal@285
|
545 posts = Post.objects.filter(topic=topic)
|
bgneal@285
|
546 for post in posts:
|
bgneal@285
|
547 post.attachments.clear()
|
bgneal@285
|
548
|
bgneal@318
|
549 # Null out the topic's last post so we don't have a foreign key pointing
|
bgneal@318
|
550 # to a post when we delete posts.
|
bgneal@318
|
551 topic.last_post = None
|
bgneal@318
|
552 topic.save()
|
bgneal@318
|
553
|
bgneal@318
|
554 # delete all posts in bulk
|
bgneal@318
|
555 posts.delete()
|
bgneal@318
|
556
|
bgneal@318
|
557 # It should be safe to just delete the topic now.
|
bgneal@232
|
558 topic.delete()
|
bgneal@232
|
559
|
bgneal@318
|
560 # Resync parent forum's post and topic counts
|
bgneal@318
|
561 parent_forum.sync()
|
bgneal@318
|
562 parent_forum.save()
|
bgneal@318
|
563
|
bgneal@232
|
564
|
bgneal@232
|
565 @login_required
|
bgneal@232
|
566 def new_post(request, topic_id):
|
bgneal@232
|
567 """
|
bgneal@232
|
568 This function is the view for creating a normal, non-quick reply
|
bgneal@232
|
569 to a topic.
|
bgneal@232
|
570 """
|
bgneal@232
|
571 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
|
bgneal@459
|
572 can_post = perms.can_post(topic, request.user)
|
bgneal@232
|
573
|
bgneal@232
|
574 if can_post:
|
bgneal@232
|
575 if request.method == 'POST':
|
bgneal@232
|
576 form = PostForm(request.POST)
|
bgneal@232
|
577 if form.is_valid():
|
bgneal@232
|
578 if antispam.utils.spam_check(request, form.cleaned_data['body']):
|
bgneal@232
|
579 return HttpResponseRedirect(reverse('antispam-suspended'))
|
bgneal@232
|
580 post = form.save(commit=False)
|
bgneal@232
|
581 post.topic = topic
|
bgneal@232
|
582 post.user = request.user
|
bgneal@232
|
583 post.user_ip = request.META.get("REMOTE_ADDR", "")
|
bgneal@963
|
584 post.save(html=form.body_html)
|
bgneal@469
|
585 notify_new_post(post)
|
bgneal@285
|
586
|
bgneal@285
|
587 # Save any attachments
|
bgneal@286
|
588 form.attach_proc.save_attachments(post)
|
bgneal@285
|
589
|
bgneal@232
|
590 _bump_post_count(request.user)
|
bgneal@446
|
591 _update_last_visit(request.user, topic, datetime.datetime.now())
|
bgneal@232
|
592 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@232
|
593 else:
|
bgneal@232
|
594 quote_id = request.GET.get('quote')
|
bgneal@232
|
595 if quote_id:
|
bgneal@232
|
596 quote_post = get_object_or_404(Post.objects.select_related(),
|
bgneal@232
|
597 pk=quote_id)
|
bgneal@566
|
598 form = PostForm(initial={'body': quote_message(quote_post.user.username,
|
bgneal@232
|
599 quote_post.body)})
|
bgneal@232
|
600 else:
|
bgneal@232
|
601 form = PostForm()
|
bgneal@232
|
602 else:
|
bgneal@232
|
603 form = None
|
bgneal@232
|
604
|
bgneal@1031
|
605 return render(request, 'forums/new_post.html', {
|
bgneal@232
|
606 'forum': topic.forum,
|
bgneal@232
|
607 'topic': topic,
|
bgneal@232
|
608 'form': form,
|
bgneal@232
|
609 'can_post': can_post,
|
bgneal@1031
|
610 })
|
bgneal@232
|
611
|
bgneal@232
|
612
|
bgneal@232
|
613 @login_required
|
bgneal@232
|
614 def mod_topic_stick(request, id):
|
bgneal@232
|
615 """
|
bgneal@232
|
616 This view function is for moderators to toggle the sticky status of a topic.
|
bgneal@232
|
617 """
|
bgneal@232
|
618 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@459
|
619 if perms.can_moderate(topic.forum, request.user):
|
bgneal@232
|
620 topic.sticky = not topic.sticky
|
bgneal@232
|
621 topic.save()
|
bgneal@232
|
622 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@232
|
623
|
bgneal@232
|
624 return HttpResponseForbidden()
|
bgneal@232
|
625
|
bgneal@232
|
626
|
bgneal@232
|
627 @login_required
|
bgneal@232
|
628 def mod_topic_lock(request, id):
|
bgneal@232
|
629 """
|
bgneal@232
|
630 This view function is for moderators to toggle the locked status of a topic.
|
bgneal@232
|
631 """
|
bgneal@232
|
632 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@459
|
633 if perms.can_moderate(topic.forum, request.user):
|
bgneal@232
|
634 topic.locked = not topic.locked
|
bgneal@232
|
635 topic.save()
|
bgneal@232
|
636 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@232
|
637
|
bgneal@232
|
638 return HttpResponseForbidden()
|
bgneal@232
|
639
|
bgneal@232
|
640
|
bgneal@232
|
641 @login_required
|
bgneal@232
|
642 def mod_topic_delete(request, id):
|
bgneal@232
|
643 """
|
bgneal@232
|
644 This view function is for moderators to delete an entire topic.
|
bgneal@232
|
645 """
|
bgneal@232
|
646 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@459
|
647 if perms.can_moderate(topic.forum, request.user):
|
bgneal@232
|
648 forum_url = topic.forum.get_absolute_url()
|
bgneal@232
|
649 _delete_topic(topic)
|
bgneal@232
|
650 return HttpResponseRedirect(forum_url)
|
bgneal@232
|
651
|
bgneal@232
|
652 return HttpResponseForbidden()
|
bgneal@232
|
653
|
bgneal@232
|
654
|
bgneal@232
|
655 @login_required
|
bgneal@232
|
656 def mod_topic_move(request, id):
|
bgneal@232
|
657 """
|
bgneal@232
|
658 This view function is for moderators to move a topic to a different forum.
|
bgneal@232
|
659 """
|
bgneal@232
|
660 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@459
|
661 if not perms.can_moderate(topic.forum, request.user):
|
bgneal@232
|
662 return HttpResponseForbidden()
|
bgneal@232
|
663
|
bgneal@232
|
664 if request.method == 'POST':
|
bgneal@232
|
665 form = MoveTopicForm(request.user, request.POST)
|
bgneal@232
|
666 if form.is_valid():
|
bgneal@232
|
667 new_forum = form.cleaned_data['forums']
|
bgneal@232
|
668 old_forum = topic.forum
|
bgneal@232
|
669 _move_topic(topic, old_forum, new_forum)
|
bgneal@232
|
670 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@232
|
671 else:
|
bgneal@232
|
672 form = MoveTopicForm(request.user)
|
bgneal@232
|
673
|
bgneal@1031
|
674 return render(request, 'forums/move_topic.html', {
|
bgneal@232
|
675 'forum': topic.forum,
|
bgneal@232
|
676 'topic': topic,
|
bgneal@232
|
677 'form': form,
|
bgneal@1031
|
678 })
|
bgneal@232
|
679
|
bgneal@232
|
680
|
bgneal@232
|
681 @login_required
|
bgneal@232
|
682 def mod_forum(request, slug):
|
bgneal@232
|
683 """
|
bgneal@232
|
684 Displays a view to allow moderators to perform various operations
|
bgneal@232
|
685 on topics in a forum in bulk. We currently support mass locking/unlocking,
|
bgneal@232
|
686 stickying and unstickying, moving, and deleting topics.
|
bgneal@232
|
687 """
|
bgneal@232
|
688 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@459
|
689 if not perms.can_moderate(forum, request.user):
|
bgneal@232
|
690 return HttpResponseForbidden()
|
bgneal@232
|
691
|
bgneal@232
|
692 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@232
|
693 paginator = create_topic_paginator(topics)
|
bgneal@232
|
694 page_num = get_page_num(request)
|
bgneal@232
|
695 try:
|
bgneal@232
|
696 page = paginator.page(page_num)
|
bgneal@232
|
697 except InvalidPage:
|
bgneal@232
|
698 raise Http404
|
bgneal@232
|
699
|
bgneal@232
|
700 # we do this for the template since it is rendered twice
|
bgneal@232
|
701 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@232
|
702 form = None
|
bgneal@232
|
703
|
bgneal@232
|
704 if request.method == 'POST':
|
bgneal@232
|
705 topic_ids = request.POST.getlist('topic_ids')
|
bgneal@232
|
706 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
|
bgneal@232
|
707 url += '?page=%s' % page_num
|
bgneal@232
|
708
|
bgneal@232
|
709 if len(topic_ids):
|
bgneal@232
|
710 if request.POST.get('sticky'):
|
bgneal@232
|
711 _bulk_sticky(forum, topic_ids)
|
bgneal@232
|
712 return HttpResponseRedirect(url)
|
bgneal@232
|
713 elif request.POST.get('lock'):
|
bgneal@232
|
714 _bulk_lock(forum, topic_ids)
|
bgneal@232
|
715 return HttpResponseRedirect(url)
|
bgneal@232
|
716 elif request.POST.get('delete'):
|
bgneal@232
|
717 _bulk_delete(forum, topic_ids)
|
bgneal@232
|
718 return HttpResponseRedirect(url)
|
bgneal@232
|
719 elif request.POST.get('move'):
|
bgneal@232
|
720 form = MoveTopicForm(request.user, request.POST, hide_label=True)
|
bgneal@232
|
721 if form.is_valid():
|
bgneal@232
|
722 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
|
bgneal@232
|
723 return HttpResponseRedirect(url)
|
bgneal@286
|
724
|
bgneal@232
|
725 if form is None:
|
bgneal@232
|
726 form = MoveTopicForm(request.user, hide_label=True)
|
bgneal@232
|
727
|
bgneal@1031
|
728 return render(request, 'forums/mod_forum.html', {
|
bgneal@232
|
729 'forum': forum,
|
bgneal@232
|
730 'page': page,
|
bgneal@232
|
731 'page_nav': page_nav,
|
bgneal@232
|
732 'form': form,
|
bgneal@1031
|
733 })
|
bgneal@232
|
734
|
bgneal@232
|
735
|
bgneal@232
|
736 @login_required
|
bgneal@232
|
737 @require_POST
|
bgneal@382
|
738 def catchup_all(request):
|
bgneal@382
|
739 """
|
bgneal@382
|
740 This view marks all forums as being read.
|
bgneal@382
|
741 """
|
bgneal@384
|
742 forum_ids = Forum.objects.forum_ids_for_user(request.user)
|
bgneal@382
|
743
|
bgneal@1031
|
744 TopicLastVisit.objects.filter(
|
bgneal@1031
|
745 user=request.user,
|
bgneal@382
|
746 topic__forum__id__in=forum_ids).delete()
|
bgneal@382
|
747
|
bgneal@382
|
748 now = datetime.datetime.now()
|
bgneal@384
|
749 ForumLastVisit.objects.filter(user=request.user,
|
bgneal@384
|
750 forum__in=forum_ids).update(begin_date=now, end_date=now)
|
bgneal@382
|
751
|
bgneal@382
|
752 return HttpResponseRedirect(reverse('forums-index'))
|
bgneal@382
|
753
|
bgneal@382
|
754
|
bgneal@382
|
755 @login_required
|
bgneal@382
|
756 @require_POST
|
bgneal@232
|
757 def forum_catchup(request, slug):
|
bgneal@232
|
758 """
|
bgneal@232
|
759 This view marks all the topics in the forum as being read.
|
bgneal@232
|
760 """
|
bgneal@232
|
761 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@232
|
762
|
bgneal@459
|
763 if not perms.can_access(forum.category, request.user):
|
bgneal@232
|
764 return HttpResponseForbidden()
|
bgneal@232
|
765
|
bgneal@232
|
766 forum.catchup(request.user)
|
bgneal@232
|
767 return HttpResponseRedirect(forum.get_absolute_url())
|
bgneal@232
|
768
|
bgneal@232
|
769
|
bgneal@232
|
770 @login_required
|
bgneal@232
|
771 def mod_topic_split(request, id):
|
bgneal@232
|
772 """
|
bgneal@232
|
773 This view function allows moderators to split posts off to a new topic.
|
bgneal@232
|
774 """
|
bgneal@232
|
775 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@459
|
776 if not perms.can_moderate(topic.forum, request.user):
|
bgneal@232
|
777 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@232
|
778
|
bgneal@232
|
779 if request.method == "POST":
|
bgneal@232
|
780 form = SplitTopicForm(request.user, request.POST)
|
bgneal@232
|
781 if form.is_valid():
|
bgneal@232
|
782 if form.split_at:
|
bgneal@232
|
783 _split_topic_at(topic, form.post_ids[0],
|
bgneal@232
|
784 form.cleaned_data['forums'],
|
bgneal@232
|
785 form.cleaned_data['name'])
|
bgneal@232
|
786 else:
|
bgneal@232
|
787 _split_topic(topic, form.post_ids,
|
bgneal@232
|
788 form.cleaned_data['forums'],
|
bgneal@232
|
789 form.cleaned_data['name'])
|
bgneal@232
|
790
|
bgneal@232
|
791 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@232
|
792 else:
|
bgneal@232
|
793 form = SplitTopicForm(request.user)
|
bgneal@232
|
794
|
bgneal@232
|
795 posts = topic.posts.select_related()
|
bgneal@232
|
796
|
bgneal@1031
|
797 return render(request, 'forums/mod_split_topic.html', {
|
bgneal@232
|
798 'forum': topic.forum,
|
bgneal@232
|
799 'topic': topic,
|
bgneal@232
|
800 'posts': posts,
|
bgneal@232
|
801 'form': form,
|
bgneal@1031
|
802 })
|
bgneal@232
|
803
|
bgneal@232
|
804
|
bgneal@232
|
805 @login_required
|
bgneal@232
|
806 def unread_topics(request):
|
bgneal@232
|
807 """Displays the topics with unread posts for a given user."""
|
bgneal@232
|
808
|
bgneal@232
|
809 topics = get_unread_topics(request.user)
|
bgneal@232
|
810
|
bgneal@232
|
811 paginator = create_topic_paginator(topics)
|
bgneal@232
|
812 page_num = get_page_num(request)
|
bgneal@232
|
813 try:
|
bgneal@232
|
814 page = paginator.page(page_num)
|
bgneal@232
|
815 except InvalidPage:
|
bgneal@232
|
816 raise Http404
|
bgneal@232
|
817
|
bgneal@232
|
818 attach_topic_page_ranges(page.object_list)
|
bgneal@232
|
819
|
bgneal@232
|
820 # we do this for the template since it is rendered twice
|
bgneal@232
|
821 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@232
|
822
|
bgneal@1031
|
823 return render(request, 'forums/topic_list.html', {
|
bgneal@232
|
824 'title': 'Topics With Unread Posts',
|
bgneal@232
|
825 'page': page,
|
bgneal@232
|
826 'page_nav': page_nav,
|
bgneal@374
|
827 'unread': True,
|
bgneal@1031
|
828 })
|
bgneal@232
|
829
|
bgneal@232
|
830
|
bgneal@232
|
831 def unanswered_topics(request):
|
bgneal@232
|
832 """Displays the topics with no replies."""
|
bgneal@232
|
833
|
bgneal@232
|
834 forum_ids = Forum.objects.forum_ids_for_user(request.user)
|
bgneal@232
|
835 topics = Topic.objects.filter(forum__id__in=forum_ids,
|
bgneal@232
|
836 post_count=1).select_related(
|
bgneal@232
|
837 'forum', 'user', 'last_post', 'last_post__user')
|
bgneal@232
|
838
|
bgneal@232
|
839 paginator = create_topic_paginator(topics)
|
bgneal@232
|
840 page_num = get_page_num(request)
|
bgneal@232
|
841 try:
|
bgneal@232
|
842 page = paginator.page(page_num)
|
bgneal@232
|
843 except InvalidPage:
|
bgneal@232
|
844 raise Http404
|
bgneal@232
|
845
|
bgneal@232
|
846 attach_topic_page_ranges(page.object_list)
|
bgneal@232
|
847
|
bgneal@232
|
848 # we do this for the template since it is rendered twice
|
bgneal@232
|
849 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@232
|
850
|
bgneal@1031
|
851 return render(request, 'forums/topic_list.html', {
|
bgneal@232
|
852 'title': 'Unanswered Topics',
|
bgneal@232
|
853 'page': page,
|
bgneal@232
|
854 'page_nav': page_nav,
|
bgneal@374
|
855 'unread': False,
|
bgneal@1031
|
856 })
|
bgneal@232
|
857
|
bgneal@232
|
858
|
bgneal@409
|
859 def active_topics(request, num):
|
bgneal@409
|
860 """Displays the last num topics that have been posted to."""
|
bgneal@409
|
861
|
bgneal@409
|
862 # sanity check num
|
bgneal@409
|
863 num = min(50, max(10, int(num)))
|
bgneal@409
|
864
|
bgneal@409
|
865 # MySQL didn't do this query very well unfortunately...
|
bgneal@522
|
866 #
|
bgneal@522
|
867 #public_forum_ids = Forum.objects.public_forum_ids()
|
bgneal@409
|
868 #topics = Topic.objects.filter(forum__in=public_forum_ids).select_related(
|
bgneal@409
|
869 # 'forum', 'user', 'last_post', 'last_post__user').order_by(
|
bgneal@409
|
870 # '-update_date')[:num]
|
bgneal@522
|
871
|
bgneal@522
|
872 # Save 1 query by using forums.latest to give us a list of the most recent
|
bgneal@522
|
873 # topics; forums.latest doesn't save enough info to give us everything we
|
bgneal@522
|
874 # need so we hit the database for the rest.
|
bgneal@522
|
875
|
bgneal@522
|
876 topic_ids = get_latest_topic_ids(num)
|
bgneal@409
|
877 topics = Topic.objects.filter(id__in=topic_ids).select_related(
|
bgneal@409
|
878 'forum', 'user', 'last_post', 'last_post__user').order_by(
|
bgneal@522
|
879 '-update_date')
|
bgneal@409
|
880
|
bgneal@409
|
881 paginator = create_topic_paginator(topics)
|
bgneal@409
|
882 page_num = get_page_num(request)
|
bgneal@409
|
883 try:
|
bgneal@409
|
884 page = paginator.page(page_num)
|
bgneal@409
|
885 except InvalidPage:
|
bgneal@409
|
886 raise Http404
|
bgneal@409
|
887
|
bgneal@409
|
888 attach_topic_page_ranges(page.object_list)
|
bgneal@409
|
889
|
bgneal@409
|
890 # we do this for the template since it is rendered twice
|
bgneal@409
|
891 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@409
|
892
|
bgneal@409
|
893 title = 'Last %d Active Topics' % num
|
bgneal@409
|
894
|
bgneal@1031
|
895 return render(request, 'forums/topic_list.html', {
|
bgneal@409
|
896 'title': title,
|
bgneal@409
|
897 'page': page,
|
bgneal@409
|
898 'page_nav': page_nav,
|
bgneal@409
|
899 'unread': False,
|
bgneal@1031
|
900 })
|
bgneal@409
|
901
|
bgneal@409
|
902
|
bgneal@232
|
903 @login_required
|
bgneal@232
|
904 def my_posts(request):
|
bgneal@232
|
905 """Displays a list of posts the requesting user made."""
|
bgneal@232
|
906 return _user_posts(request, request.user, request.user, 'My Posts')
|
bgneal@232
|
907
|
bgneal@232
|
908
|
bgneal@232
|
909 @login_required
|
bgneal@232
|
910 def posts_for_user(request, username):
|
bgneal@232
|
911 """Displays a list of posts by the given user.
|
bgneal@232
|
912 Only the forums that the requesting user can see are examined.
|
bgneal@232
|
913 """
|
bgneal@232
|
914 target_user = get_object_or_404(User, username=username)
|
bgneal@232
|
915 return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
|
bgneal@232
|
916
|
bgneal@232
|
917
|
bgneal@232
|
918 @login_required
|
bgneal@232
|
919 def post_ip_info(request, post_id):
|
bgneal@232
|
920 """Displays information about the IP address the post was made from."""
|
bgneal@232
|
921 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@232
|
922
|
bgneal@459
|
923 if not perms.can_moderate(post.topic.forum, request.user):
|
bgneal@232
|
924 return HttpResponseForbidden("You don't have permission for this post.")
|
bgneal@232
|
925
|
bgneal@232
|
926 ip_users = sorted(set(Post.objects.filter(
|
bgneal@232
|
927 user_ip=post.user_ip).values_list('user__username', flat=True)))
|
bgneal@232
|
928
|
bgneal@1031
|
929 return render(request, 'forums/post_ip.html', {
|
bgneal@232
|
930 'post': post,
|
bgneal@232
|
931 'ip_users': ip_users,
|
bgneal@1031
|
932 })
|
bgneal@232
|
933
|
bgneal@232
|
934
|
bgneal@232
|
935 def _user_posts(request, target_user, req_user, page_title):
|
bgneal@286
|
936 """Displays a list of posts made by the target user.
|
bgneal@232
|
937 req_user is the user trying to view the posts. Only the forums
|
bgneal@232
|
938 req_user can see are searched.
|
bgneal@232
|
939 """
|
bgneal@232
|
940 forum_ids = Forum.objects.forum_ids_for_user(req_user)
|
bgneal@232
|
941 posts = Post.objects.filter(user=target_user,
|
bgneal@232
|
942 topic__forum__id__in=forum_ids).order_by(
|
bgneal@232
|
943 '-creation_date').select_related()
|
bgneal@232
|
944
|
bgneal@232
|
945 paginator = create_post_paginator(posts)
|
bgneal@232
|
946 page_num = get_page_num(request)
|
bgneal@232
|
947 try:
|
bgneal@232
|
948 page = paginator.page(page_num)
|
bgneal@232
|
949 except InvalidPage:
|
bgneal@232
|
950 raise Http404
|
bgneal@232
|
951
|
bgneal@232
|
952 # we do this for the template since it is rendered twice
|
bgneal@232
|
953 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@232
|
954
|
bgneal@1031
|
955 return render(request, 'forums/post_list.html', {
|
bgneal@232
|
956 'title': page_title,
|
bgneal@232
|
957 'page': page,
|
bgneal@232
|
958 'page_nav': page_nav,
|
bgneal@1031
|
959 })
|
bgneal@232
|
960
|
bgneal@232
|
961
|
bgneal@232
|
962 def _bump_post_count(user):
|
bgneal@232
|
963 """
|
bgneal@232
|
964 Increments the forum_post_count for the given user.
|
bgneal@232
|
965 """
|
bgneal@789
|
966 profile = user.profile
|
bgneal@232
|
967 profile.forum_post_count += 1
|
bgneal@562
|
968 profile.save(content_update=False)
|
bgneal@232
|
969
|
bgneal@232
|
970
|
bgneal@232
|
971 def _move_topic(topic, old_forum, new_forum):
|
bgneal@232
|
972 if new_forum != old_forum:
|
bgneal@232
|
973 topic.forum = new_forum
|
bgneal@232
|
974 topic.save()
|
bgneal@232
|
975 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
|
bgneal@232
|
976 old_forum.sync()
|
bgneal@232
|
977 old_forum.save()
|
bgneal@232
|
978 new_forum.sync()
|
bgneal@232
|
979 new_forum.save()
|
bgneal@232
|
980
|
bgneal@232
|
981
|
bgneal@232
|
982 def _bulk_sticky(forum, topic_ids):
|
bgneal@232
|
983 """
|
bgneal@232
|
984 Performs a toggle on the sticky status for a given list of topic ids.
|
bgneal@232
|
985 """
|
bgneal@232
|
986 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@232
|
987 for topic in topics:
|
bgneal@232
|
988 if topic.forum == forum:
|
bgneal@232
|
989 topic.sticky = not topic.sticky
|
bgneal@232
|
990 topic.save()
|
bgneal@232
|
991
|
bgneal@232
|
992
|
bgneal@232
|
993 def _bulk_lock(forum, topic_ids):
|
bgneal@232
|
994 """
|
bgneal@232
|
995 Performs a toggle on the locked status for a given list of topic ids.
|
bgneal@232
|
996 """
|
bgneal@232
|
997 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@232
|
998 for topic in topics:
|
bgneal@232
|
999 if topic.forum == forum:
|
bgneal@232
|
1000 topic.locked = not topic.locked
|
bgneal@232
|
1001 topic.save()
|
bgneal@232
|
1002
|
bgneal@232
|
1003
|
bgneal@232
|
1004 def _bulk_delete(forum, topic_ids):
|
bgneal@232
|
1005 """
|
bgneal@232
|
1006 Deletes the list of topics.
|
bgneal@232
|
1007 """
|
bgneal@235
|
1008 # Because we are deleting stuff, retrieve each topic one at a
|
bgneal@235
|
1009 # time since we are going to be adjusting de-normalized fields
|
bgneal@235
|
1010 # during deletes. In particular, we can't do this:
|
bgneal@235
|
1011 # topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@235
|
1012 # for topic in topics:
|
bgneal@235
|
1013 # since topic.forum.last_post can go stale after a delete.
|
bgneal@235
|
1014
|
bgneal@235
|
1015 for id in topic_ids:
|
bgneal@235
|
1016 try:
|
bgneal@235
|
1017 topic = Topic.objects.select_related().get(pk=id)
|
bgneal@235
|
1018 except Topic.DoesNotExist:
|
bgneal@235
|
1019 continue
|
bgneal@235
|
1020 _delete_topic(topic)
|
bgneal@232
|
1021
|
bgneal@232
|
1022
|
bgneal@232
|
1023 def _bulk_move(topic_ids, old_forum, new_forum):
|
bgneal@232
|
1024 """
|
bgneal@232
|
1025 Moves the list of topics to a new forum.
|
bgneal@232
|
1026 """
|
bgneal@232
|
1027 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@232
|
1028 for topic in topics:
|
bgneal@232
|
1029 if topic.forum == old_forum:
|
bgneal@232
|
1030 _move_topic(topic, old_forum, new_forum)
|
bgneal@232
|
1031
|
bgneal@232
|
1032
|
bgneal@445
|
1033 def _update_last_visit(user, topic, visit_time):
|
bgneal@232
|
1034 """
|
bgneal@232
|
1035 Does the bookkeeping for the last visit status for the user to the
|
bgneal@232
|
1036 topic/forum.
|
bgneal@232
|
1037 """
|
bgneal@232
|
1038 now = datetime.datetime.now()
|
bgneal@232
|
1039 try:
|
bgneal@232
|
1040 flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
|
bgneal@232
|
1041 except ForumLastVisit.DoesNotExist:
|
bgneal@232
|
1042 flv = ForumLastVisit(user=user, forum=topic.forum)
|
bgneal@232
|
1043 flv.begin_date = now
|
bgneal@232
|
1044
|
bgneal@232
|
1045 flv.end_date = now
|
bgneal@232
|
1046 flv.save()
|
bgneal@232
|
1047
|
bgneal@232
|
1048 if topic.update_date > flv.begin_date:
|
bgneal@232
|
1049 try:
|
bgneal@232
|
1050 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
|
bgneal@232
|
1051 except TopicLastVisit.DoesNotExist:
|
bgneal@445
|
1052 tlv = TopicLastVisit(user=user, topic=topic, last_visit=datetime.datetime.min)
|
bgneal@232
|
1053
|
bgneal@445
|
1054 if visit_time > tlv.last_visit:
|
bgneal@445
|
1055 tlv.last_visit = visit_time
|
bgneal@445
|
1056 tlv.save()
|
bgneal@232
|
1057
|
bgneal@232
|
1058
|
bgneal@232
|
1059 def _split_topic_at(topic, post_id, new_forum, new_name):
|
bgneal@232
|
1060 """
|
bgneal@232
|
1061 This function splits the post given by post_id and all posts that come
|
bgneal@232
|
1062 after it in the given topic to a new topic in a new forum.
|
bgneal@232
|
1063 It is assumed the caller has been checked for moderator rights.
|
bgneal@232
|
1064 """
|
bgneal@232
|
1065 post = get_object_or_404(Post, id=post_id)
|
bgneal@232
|
1066 if post.topic == topic:
|
bgneal@232
|
1067 post_ids = Post.objects.filter(topic=topic,
|
bgneal@232
|
1068 creation_date__gte=post.creation_date).values_list('id', flat=True)
|
bgneal@232
|
1069 _split_topic(topic, post_ids, new_forum, new_name)
|
bgneal@232
|
1070
|
bgneal@232
|
1071
|
bgneal@232
|
1072 def _split_topic(topic, post_ids, new_forum, new_name):
|
bgneal@232
|
1073 """
|
bgneal@232
|
1074 This function splits the posts given by the post_ids list in the
|
bgneal@232
|
1075 given topic to a new topic in a new forum.
|
bgneal@232
|
1076 It is assumed the caller has been checked for moderator rights.
|
bgneal@232
|
1077 """
|
bgneal@232
|
1078 posts = Post.objects.filter(topic=topic, id__in=post_ids)
|
bgneal@232
|
1079 if len(posts) > 0:
|
bgneal@232
|
1080 new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
|
bgneal@232
|
1081 new_topic.save()
|
bgneal@469
|
1082 notify_new_topic(new_topic)
|
bgneal@232
|
1083 for post in posts:
|
bgneal@232
|
1084 post.topic = new_topic
|
bgneal@232
|
1085 post.save()
|
bgneal@286
|
1086
|
bgneal@232
|
1087 topic.post_count_update()
|
bgneal@232
|
1088 topic.save()
|
bgneal@232
|
1089 new_topic.post_count_update()
|
bgneal@232
|
1090 new_topic.save()
|
bgneal@232
|
1091 topic.forum.sync()
|
bgneal@232
|
1092 topic.forum.save()
|
bgneal@232
|
1093 new_forum.sync()
|
bgneal@232
|
1094 new_forum.save()
|