bgneal@81
|
1 """
|
bgneal@81
|
2 Views for the forums application.
|
bgneal@81
|
3 """
|
bgneal@113
|
4 import datetime
|
bgneal@113
|
5
|
bgneal@83
|
6 from django.contrib.auth.decorators import login_required
|
bgneal@172
|
7 from django.contrib.auth.models import User
|
bgneal@82
|
8 from django.http import Http404
|
bgneal@98
|
9 from django.http import HttpResponse
|
bgneal@89
|
10 from django.http import HttpResponseBadRequest
|
bgneal@90
|
11 from django.http import HttpResponseForbidden
|
bgneal@83
|
12 from django.http import HttpResponseRedirect
|
bgneal@83
|
13 from django.core.urlresolvers import reverse
|
bgneal@91
|
14 from django.core.paginator import InvalidPage
|
bgneal@82
|
15 from django.shortcuts import get_object_or_404
|
bgneal@81
|
16 from django.shortcuts import render_to_response
|
bgneal@169
|
17 from django.shortcuts import redirect
|
bgneal@97
|
18 from django.template.loader import render_to_string
|
bgneal@81
|
19 from django.template import RequestContext
|
bgneal@89
|
20 from django.views.decorators.http import require_POST
|
bgneal@108
|
21 from django.utils.text import wrap
|
bgneal@189
|
22 from django.db.models import F
|
bgneal@81
|
23
|
bgneal@90
|
24 from core.paginator import DiggPaginator
|
bgneal@98
|
25 from core.functions import email_admins
|
bgneal@113
|
26 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
|
bgneal@113
|
27 ForumLastVisit
|
bgneal@115
|
28 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
|
bgneal@115
|
29 SplitTopicForm
|
bgneal@114
|
30 from forums.unread import get_forum_unread_status, get_topic_unread_status, \
|
bgneal@167
|
31 get_post_unread_status, get_unread_topics
|
bgneal@81
|
32
|
bgneal@117
|
33 from bio.models import UserProfile
|
bgneal@90
|
34 #######################################################################
|
bgneal@90
|
35
|
bgneal@93
|
36 TOPICS_PER_PAGE = 50
|
bgneal@113
|
37 POSTS_PER_PAGE = 20
|
bgneal@90
|
38
|
bgneal@167
|
39
|
bgneal@167
|
40 def get_page_num(request):
|
bgneal@167
|
41 """Returns the value of the 'page' variable in GET if it exists, or 1
|
bgneal@167
|
42 if it does not."""
|
bgneal@167
|
43
|
bgneal@167
|
44 try:
|
bgneal@167
|
45 page_num = int(request.GET.get('page', 1))
|
bgneal@167
|
46 except ValueError:
|
bgneal@167
|
47 page_num = 1
|
bgneal@167
|
48
|
bgneal@167
|
49 return page_num
|
bgneal@167
|
50
|
bgneal@167
|
51
|
bgneal@93
|
52 def create_topic_paginator(topics):
|
bgneal@93
|
53 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@93
|
54
|
bgneal@93
|
55 def create_post_paginator(posts):
|
bgneal@93
|
56 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
57
|
bgneal@167
|
58
|
bgneal@167
|
59 def attach_topic_page_ranges(topics):
|
bgneal@167
|
60 """Attaches a page_range attribute to each topic in the supplied list.
|
bgneal@167
|
61 This attribute will be None if it is a single page topic. This is used
|
bgneal@167
|
62 by the templates to generate "goto page x" links.
|
bgneal@167
|
63 """
|
bgneal@167
|
64 for topic in topics:
|
bgneal@167
|
65 if topic.post_count > POSTS_PER_PAGE:
|
bgneal@167
|
66 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE,
|
bgneal@167
|
67 body=2, tail=3, margin=1)
|
bgneal@167
|
68 topic.page_range = pp.page(1).page_range
|
bgneal@167
|
69 else:
|
bgneal@167
|
70 topic.page_range = None
|
bgneal@167
|
71
|
bgneal@90
|
72 #######################################################################
|
bgneal@81
|
73
|
bgneal@81
|
74 def index(request):
|
bgneal@82
|
75 """
|
bgneal@82
|
76 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
77 """
|
bgneal@167
|
78 # check for special forum queries
|
bgneal@167
|
79 query = request.GET.get("query")
|
bgneal@167
|
80 if query == "unread":
|
bgneal@169
|
81 return redirect('forums-unread_topics')
|
bgneal@168
|
82 elif query == "unanswered":
|
bgneal@169
|
83 return redirect('forums-unanswered_topics')
|
bgneal@169
|
84 elif query == "mine":
|
bgneal@169
|
85 return redirect('forums-my_posts')
|
bgneal@167
|
86
|
bgneal@170
|
87 public_forums = Forum.objects.public_forums()
|
bgneal@170
|
88 feeds = [{'name': 'All Forums', 'feed': '/feeds/forums/'}]
|
bgneal@170
|
89
|
bgneal@100
|
90 forums = Forum.objects.forums_for_user(request.user)
|
bgneal@113
|
91 get_forum_unread_status(forums, request.user)
|
bgneal@81
|
92 cats = {}
|
bgneal@81
|
93 for forum in forums:
|
bgneal@170
|
94 forum.has_feed = forum in public_forums
|
bgneal@170
|
95 if forum.has_feed:
|
bgneal@170
|
96 feeds.append({
|
bgneal@170
|
97 'name': '%s Forum' % forum.name,
|
bgneal@170
|
98 'feed': '/feeds/forums/%s/' % forum.slug,
|
bgneal@170
|
99 })
|
bgneal@170
|
100
|
bgneal@81
|
101 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
102 'cat': forum.category,
|
bgneal@81
|
103 'forums': [],
|
bgneal@81
|
104 })
|
bgneal@81
|
105 cat['forums'].append(forum)
|
bgneal@81
|
106
|
bgneal@81
|
107 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
108 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
109
|
bgneal@81
|
110 return render_to_response('forums/index.html', {
|
bgneal@81
|
111 'cats': cats,
|
bgneal@170
|
112 'feeds': feeds,
|
bgneal@81
|
113 },
|
bgneal@81
|
114 context_instance=RequestContext(request))
|
bgneal@81
|
115
|
bgneal@82
|
116
|
bgneal@81
|
117 def forum_index(request, slug):
|
bgneal@82
|
118 """
|
bgneal@82
|
119 Displays all the topics in a forum.
|
bgneal@82
|
120 """
|
bgneal@101
|
121 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
122
|
bgneal@100
|
123 if not forum.category.can_access(request.user):
|
bgneal@100
|
124 return HttpResponseForbidden()
|
bgneal@100
|
125
|
bgneal@107
|
126 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@114
|
127 get_topic_unread_status(forum, topics, request.user)
|
bgneal@114
|
128
|
bgneal@93
|
129 paginator = create_topic_paginator(topics)
|
bgneal@167
|
130 page_num = get_page_num(request)
|
bgneal@93
|
131 try:
|
bgneal@93
|
132 page = paginator.page(page_num)
|
bgneal@93
|
133 except InvalidPage:
|
bgneal@93
|
134 raise Http404
|
bgneal@97
|
135
|
bgneal@167
|
136 attach_topic_page_ranges(page.object_list)
|
bgneal@161
|
137
|
bgneal@97
|
138 # we do this for the template since it is rendered twice
|
bgneal@97
|
139 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
140
|
bgneal@111
|
141 can_moderate = _can_moderate(forum, request.user)
|
bgneal@82
|
142
|
bgneal@82
|
143 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
144 'forum': forum,
|
bgneal@93
|
145 'page': page,
|
bgneal@97
|
146 'page_nav': page_nav,
|
bgneal@111
|
147 'can_moderate': can_moderate,
|
bgneal@82
|
148 },
|
bgneal@82
|
149 context_instance=RequestContext(request))
|
bgneal@82
|
150
|
bgneal@82
|
151
|
bgneal@82
|
152 def topic_index(request, id):
|
bgneal@82
|
153 """
|
bgneal@82
|
154 Displays all the posts in a topic.
|
bgneal@82
|
155 """
|
bgneal@189
|
156 topic = get_object_or_404(Topic.objects.select_related(
|
bgneal@189
|
157 'forum', 'forum__category', 'last_post'), pk=id)
|
bgneal@100
|
158
|
bgneal@100
|
159 if not topic.forum.category.can_access(request.user):
|
bgneal@100
|
160 return HttpResponseForbidden()
|
bgneal@100
|
161
|
bgneal@189
|
162 topic.view_count = F('view_count') + 1
|
bgneal@189
|
163 topic.save(force_update=True)
|
bgneal@86
|
164
|
bgneal@86
|
165 posts = topic.posts.select_related()
|
bgneal@114
|
166
|
bgneal@93
|
167 paginator = create_post_paginator(posts)
|
bgneal@167
|
168 page_num = get_page_num(request)
|
bgneal@90
|
169 try:
|
bgneal@90
|
170 page = paginator.page(page_num)
|
bgneal@90
|
171 except InvalidPage:
|
bgneal@90
|
172 raise Http404
|
bgneal@117
|
173 get_post_unread_status(topic, page.object_list, request.user)
|
bgneal@117
|
174
|
bgneal@117
|
175 # Attach user profiles to each post to avoid using get_user_profile() in
|
bgneal@117
|
176 # the template.
|
bgneal@117
|
177 users = set(post.user.id for post in page.object_list)
|
bgneal@117
|
178
|
bgneal@117
|
179 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
|
bgneal@117
|
180 user_profiles = dict((profile.user.id, profile) for profile in profiles)
|
bgneal@117
|
181
|
bgneal@117
|
182 for post in page.object_list:
|
bgneal@117
|
183 post.user_profile = user_profiles[post.user.id]
|
bgneal@90
|
184
|
bgneal@90
|
185 last_page = page_num == paginator.num_pages
|
bgneal@86
|
186
|
bgneal@113
|
187 if request.user.is_authenticated() and last_page:
|
bgneal@113
|
188 _update_last_visit(request.user, topic)
|
bgneal@113
|
189
|
bgneal@97
|
190 # we do this for the template since it is rendered twice
|
bgneal@97
|
191 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@97
|
192
|
bgneal@109
|
193 can_moderate = _can_moderate(topic.forum, request.user)
|
bgneal@104
|
194
|
bgneal@104
|
195 can_reply = request.user.is_authenticated() and (
|
bgneal@104
|
196 not topic.locked or can_moderate)
|
bgneal@104
|
197
|
bgneal@181
|
198 is_subscribed = request.user.is_authenticated() and (
|
bgneal@181
|
199 topic in request.user.subscriptions.all())
|
bgneal@181
|
200
|
bgneal@86
|
201 return render_to_response('forums/topic.html', {
|
bgneal@86
|
202 'forum': topic.forum,
|
bgneal@86
|
203 'topic': topic,
|
bgneal@90
|
204 'page': page,
|
bgneal@97
|
205 'page_nav': page_nav,
|
bgneal@87
|
206 'last_page': last_page,
|
bgneal@104
|
207 'can_moderate': can_moderate,
|
bgneal@104
|
208 'can_reply': can_reply,
|
bgneal@106
|
209 'form': NewPostForm(initial={'topic_id': topic.id}),
|
bgneal@181
|
210 'is_subscribed': is_subscribed,
|
bgneal@86
|
211 },
|
bgneal@86
|
212 context_instance=RequestContext(request))
|
bgneal@83
|
213
|
bgneal@83
|
214
|
bgneal@83
|
215 @login_required
|
bgneal@83
|
216 def new_topic(request, slug):
|
bgneal@83
|
217 """
|
bgneal@83
|
218 This view handles the creation of new topics.
|
bgneal@83
|
219 """
|
bgneal@101
|
220 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
221
|
bgneal@100
|
222 if not forum.category.can_access(request.user):
|
bgneal@100
|
223 return HttpResponseForbidden()
|
bgneal@100
|
224
|
bgneal@83
|
225 if request.method == 'POST':
|
bgneal@102
|
226 form = NewTopicForm(request.user, forum, request.POST)
|
bgneal@83
|
227 if form.is_valid():
|
bgneal@102
|
228 topic = form.save(request.META.get("REMOTE_ADDR"))
|
bgneal@108
|
229 _bump_post_count(request.user)
|
bgneal@83
|
230 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
231 kwargs={'tid': topic.pk}))
|
bgneal@83
|
232 else:
|
bgneal@102
|
233 form = NewTopicForm(request.user, forum)
|
bgneal@83
|
234
|
bgneal@83
|
235 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
236 'forum': forum,
|
bgneal@83
|
237 'form': form,
|
bgneal@83
|
238 },
|
bgneal@83
|
239 context_instance=RequestContext(request))
|
bgneal@83
|
240
|
bgneal@83
|
241
|
bgneal@83
|
242 @login_required
|
bgneal@83
|
243 def new_topic_thanks(request, tid):
|
bgneal@83
|
244 """
|
bgneal@83
|
245 This view displays the success page for a newly created topic.
|
bgneal@83
|
246 """
|
bgneal@101
|
247 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
|
bgneal@83
|
248 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
249 'forum': topic.forum,
|
bgneal@83
|
250 'topic': topic,
|
bgneal@83
|
251 },
|
bgneal@83
|
252 context_instance=RequestContext(request))
|
bgneal@89
|
253
|
bgneal@89
|
254
|
bgneal@89
|
255 @require_POST
|
bgneal@89
|
256 def quick_reply_ajax(request):
|
bgneal@89
|
257 """
|
bgneal@89
|
258 This function handles the quick reply to a thread function. This
|
bgneal@89
|
259 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
260 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
261 to the document.
|
bgneal@89
|
262 """
|
bgneal@90
|
263 if not request.user.is_authenticated():
|
bgneal@108
|
264 return HttpResponseForbidden('Please login or register to post.')
|
bgneal@90
|
265
|
bgneal@106
|
266 form = NewPostForm(request.POST)
|
bgneal@89
|
267 if form.is_valid():
|
bgneal@108
|
268 if not _can_post_in_topic(form.topic, request.user):
|
bgneal@108
|
269 return HttpResponseForbidden("You don't have permission to post in this topic.")
|
bgneal@100
|
270
|
bgneal@108
|
271 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
|
bgneal@114
|
272 post.unread = True
|
bgneal@122
|
273 post.user_profile = request.user.get_profile()
|
bgneal@108
|
274 _bump_post_count(request.user)
|
bgneal@113
|
275 _update_last_visit(request.user, form.topic)
|
bgneal@89
|
276 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
277 'post': post,
|
bgneal@113
|
278 'can_moderate': _can_moderate(form.topic.forum, request.user),
|
bgneal@120
|
279 'can_reply': True,
|
bgneal@89
|
280 },
|
bgneal@89
|
281 context_instance=RequestContext(request))
|
bgneal@89
|
282
|
bgneal@108
|
283 return HttpResponseBadRequest("Invalid post.");
|
bgneal@89
|
284
|
bgneal@91
|
285
|
bgneal@91
|
286 def goto_post(request, post_id):
|
bgneal@91
|
287 """
|
bgneal@91
|
288 This function calculates what page a given post is on, then redirects
|
bgneal@91
|
289 to that URL. This function is the target of get_absolute_url() for
|
bgneal@91
|
290 Post objects.
|
bgneal@91
|
291 """
|
bgneal@101
|
292 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@91
|
293 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@91
|
294 page = count / POSTS_PER_PAGE + 1
|
bgneal@91
|
295 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
|
bgneal@91
|
296 '?page=%s#p%s' % (page, post.id)
|
bgneal@91
|
297 return HttpResponseRedirect(url)
|
bgneal@91
|
298
|
bgneal@98
|
299
|
bgneal@98
|
300 @require_POST
|
bgneal@98
|
301 def flag_post(request):
|
bgneal@98
|
302 """
|
bgneal@98
|
303 This function handles the flagging of posts by users. This function should
|
bgneal@98
|
304 be the target of an AJAX post.
|
bgneal@98
|
305 """
|
bgneal@98
|
306 if not request.user.is_authenticated():
|
bgneal@99
|
307 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@98
|
308
|
bgneal@98
|
309 id = request.POST.get('id')
|
bgneal@98
|
310 if id is None:
|
bgneal@98
|
311 return HttpResponseBadRequest('No post id')
|
bgneal@98
|
312
|
bgneal@98
|
313 try:
|
bgneal@98
|
314 post = Post.objects.get(pk=id)
|
bgneal@98
|
315 except Post.DoesNotExist:
|
bgneal@98
|
316 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@98
|
317
|
bgneal@98
|
318 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@98
|
319 flag.save()
|
bgneal@98
|
320 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@98
|
321
|
bgneal@98
|
322 A user has flagged a forum post for review.
|
bgneal@98
|
323 """)
|
bgneal@98
|
324 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@98
|
325 'Thanks for helping to improve the discussions on this site.')
|
bgneal@106
|
326
|
bgneal@106
|
327
|
bgneal@106
|
328 @login_required
|
bgneal@106
|
329 def edit_post(request, id):
|
bgneal@106
|
330 """
|
bgneal@106
|
331 This view function allows authorized users to edit posts.
|
bgneal@106
|
332 The superuser, forum moderators, and original author can edit posts.
|
bgneal@106
|
333 """
|
bgneal@106
|
334 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@108
|
335
|
bgneal@109
|
336 can_moderate = _can_moderate(post.topic.forum, request.user)
|
bgneal@108
|
337 can_edit = can_moderate or request.user == post.user
|
bgneal@106
|
338
|
bgneal@106
|
339 if not can_edit:
|
bgneal@106
|
340 return HttpResponseForbidden("You don't have permission to edit that post.")
|
bgneal@106
|
341
|
bgneal@106
|
342 if request.method == "POST":
|
bgneal@106
|
343 form = PostForm(request.POST, instance=post)
|
bgneal@106
|
344 if form.is_valid():
|
bgneal@115
|
345 post = form.save(commit=False)
|
bgneal@115
|
346 post.touch()
|
bgneal@115
|
347 post.save()
|
bgneal@106
|
348 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@106
|
349 else:
|
bgneal@106
|
350 form = PostForm(instance=post)
|
bgneal@106
|
351
|
bgneal@123
|
352 post.user_profile = request.user.get_profile()
|
bgneal@123
|
353
|
bgneal@106
|
354 return render_to_response('forums/edit_post.html', {
|
bgneal@106
|
355 'forum': post.topic.forum,
|
bgneal@106
|
356 'topic': post.topic,
|
bgneal@106
|
357 'post': post,
|
bgneal@106
|
358 'form': form,
|
bgneal@108
|
359 'can_moderate': can_moderate,
|
bgneal@106
|
360 },
|
bgneal@106
|
361 context_instance=RequestContext(request))
|
bgneal@107
|
362
|
bgneal@107
|
363
|
bgneal@107
|
364 @require_POST
|
bgneal@107
|
365 def delete_post(request):
|
bgneal@107
|
366 """
|
bgneal@107
|
367 This view function allows superusers and forum moderators to delete posts.
|
bgneal@107
|
368 This function is the target of AJAX calls from the client.
|
bgneal@107
|
369 """
|
bgneal@107
|
370 if not request.user.is_authenticated():
|
bgneal@107
|
371 return HttpResponseForbidden('Please login to delete a post.')
|
bgneal@107
|
372
|
bgneal@107
|
373 id = request.POST.get('id')
|
bgneal@107
|
374 if id is None:
|
bgneal@107
|
375 return HttpResponseBadRequest('No post id')
|
bgneal@107
|
376
|
bgneal@107
|
377 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@107
|
378
|
bgneal@107
|
379 can_delete = request.user.is_superuser or \
|
bgneal@107
|
380 request.user in post.topic.forum.moderators.all()
|
bgneal@107
|
381
|
bgneal@107
|
382 if not can_delete:
|
bgneal@107
|
383 return HttpResponseForbidden("You don't have permission to delete that post.")
|
bgneal@107
|
384
|
bgneal@147
|
385 delete_single_post(post)
|
bgneal@147
|
386 return HttpResponse("The post has been deleted.")
|
bgneal@147
|
387
|
bgneal@147
|
388
|
bgneal@147
|
389 def delete_single_post(post):
|
bgneal@147
|
390 """
|
bgneal@147
|
391 This function deletes a single post. It handles the case of where
|
bgneal@147
|
392 a post is the sole post in a topic by deleting the topic also. It
|
bgneal@147
|
393 adjusts any foreign keys in Topic or Forum objects that might be pointing
|
bgneal@147
|
394 to this post before deleting the post to avoid a cascading delete.
|
bgneal@147
|
395 """
|
bgneal@107
|
396 if post.topic.post_count == 1 and post == post.topic.last_post:
|
bgneal@107
|
397 _delete_topic(post.topic)
|
bgneal@107
|
398 else:
|
bgneal@107
|
399 _delete_post(post)
|
bgneal@107
|
400
|
bgneal@107
|
401
|
bgneal@107
|
402 def _delete_post(post):
|
bgneal@107
|
403 """
|
bgneal@107
|
404 Internal function to delete a single post object.
|
bgneal@107
|
405 Decrements the post author's post count.
|
bgneal@107
|
406 Adjusts the parent topic and forum's last_post as needed.
|
bgneal@107
|
407 """
|
bgneal@107
|
408 # Adjust post creator's post count
|
bgneal@107
|
409 profile = post.user.get_profile()
|
bgneal@107
|
410 if profile.forum_post_count > 0:
|
bgneal@107
|
411 profile.forum_post_count -= 1
|
bgneal@107
|
412 profile.save()
|
bgneal@107
|
413
|
bgneal@107
|
414 # If this post is the last_post in a topic, we need to update
|
bgneal@107
|
415 # both the topic and parent forum's last post fields. If we don't
|
bgneal@107
|
416 # the cascading delete will delete them also!
|
bgneal@107
|
417
|
bgneal@107
|
418 topic = post.topic
|
bgneal@107
|
419 if topic.last_post == post:
|
bgneal@107
|
420 topic.last_post_pre_delete()
|
bgneal@107
|
421 topic.save()
|
bgneal@107
|
422
|
bgneal@107
|
423 forum = topic.forum
|
bgneal@107
|
424 if forum.last_post == post:
|
bgneal@107
|
425 forum.last_post_pre_delete()
|
bgneal@107
|
426 forum.save()
|
bgneal@107
|
427
|
bgneal@107
|
428 # Should be safe to delete the post now:
|
bgneal@107
|
429 post.delete()
|
bgneal@107
|
430
|
bgneal@107
|
431
|
bgneal@107
|
432 def _delete_topic(topic):
|
bgneal@107
|
433 """
|
bgneal@107
|
434 Internal function to delete an entire topic.
|
bgneal@107
|
435 Deletes the topic and all posts contained within.
|
bgneal@107
|
436 Adjusts the parent forum's last_post as needed.
|
bgneal@107
|
437 Note that we don't bother adjusting all the users'
|
bgneal@107
|
438 post counts as that doesn't seem to be worth the effort.
|
bgneal@107
|
439 """
|
bgneal@147
|
440 if topic.forum.last_post and topic.forum.last_post.topic == topic:
|
bgneal@107
|
441 topic.forum.last_post_pre_delete()
|
bgneal@107
|
442 topic.forum.save()
|
bgneal@107
|
443
|
bgneal@181
|
444 # delete subscriptions to this topic
|
bgneal@181
|
445 topic.subscribers.clear()
|
bgneal@181
|
446
|
bgneal@107
|
447 # It should be safe to just delete the topic now. This will
|
bgneal@107
|
448 # automatically delete all posts in the topic.
|
bgneal@107
|
449 topic.delete()
|
bgneal@108
|
450
|
bgneal@108
|
451
|
bgneal@108
|
452 @login_required
|
bgneal@108
|
453 def new_post(request, topic_id):
|
bgneal@108
|
454 """
|
bgneal@108
|
455 This function is the view for creating a normal, non-quick reply
|
bgneal@108
|
456 to a topic.
|
bgneal@108
|
457 """
|
bgneal@108
|
458 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
|
bgneal@108
|
459 can_post = _can_post_in_topic(topic, request.user)
|
bgneal@108
|
460
|
bgneal@108
|
461 if can_post:
|
bgneal@108
|
462 if request.method == 'POST':
|
bgneal@108
|
463 form = PostForm(request.POST)
|
bgneal@108
|
464 if form.is_valid():
|
bgneal@108
|
465 post = form.save(commit=False)
|
bgneal@108
|
466 post.topic = topic
|
bgneal@108
|
467 post.user = request.user
|
bgneal@108
|
468 post.user_ip = request.META.get("REMOTE_ADDR", "")
|
bgneal@108
|
469 post.save()
|
bgneal@108
|
470 _bump_post_count(request.user)
|
bgneal@113
|
471 _update_last_visit(request.user, topic)
|
bgneal@108
|
472 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@108
|
473 else:
|
bgneal@108
|
474 quote_id = request.GET.get('quote')
|
bgneal@108
|
475 if quote_id:
|
bgneal@108
|
476 quote_post = get_object_or_404(Post.objects.select_related(),
|
bgneal@108
|
477 pk=quote_id)
|
bgneal@108
|
478 form = PostForm(initial={'body': _quote_message(quote_post.user.username,
|
bgneal@108
|
479 quote_post.body)})
|
bgneal@108
|
480 else:
|
bgneal@108
|
481 form = PostForm()
|
bgneal@108
|
482 else:
|
bgneal@108
|
483 form = None
|
bgneal@108
|
484
|
bgneal@108
|
485 return render_to_response('forums/new_post.html', {
|
bgneal@108
|
486 'forum': topic.forum,
|
bgneal@108
|
487 'topic': topic,
|
bgneal@108
|
488 'form': form,
|
bgneal@108
|
489 'can_post': can_post,
|
bgneal@108
|
490 },
|
bgneal@108
|
491 context_instance=RequestContext(request))
|
bgneal@108
|
492
|
bgneal@108
|
493
|
bgneal@109
|
494 @login_required
|
bgneal@109
|
495 def mod_topic_stick(request, id):
|
bgneal@109
|
496 """
|
bgneal@109
|
497 This view function is for moderators to toggle the sticky status of a topic.
|
bgneal@109
|
498 """
|
bgneal@109
|
499 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
500 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
501 topic.sticky = not topic.sticky
|
bgneal@109
|
502 topic.save()
|
bgneal@109
|
503 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
504
|
bgneal@110
|
505 return HttpResponseForbidden()
|
bgneal@109
|
506
|
bgneal@109
|
507
|
bgneal@109
|
508 @login_required
|
bgneal@109
|
509 def mod_topic_lock(request, id):
|
bgneal@109
|
510 """
|
bgneal@109
|
511 This view function is for moderators to toggle the locked status of a topic.
|
bgneal@109
|
512 """
|
bgneal@109
|
513 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
514 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
515 topic.locked = not topic.locked
|
bgneal@109
|
516 topic.save()
|
bgneal@109
|
517 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
518
|
bgneal@110
|
519 return HttpResponseForbidden()
|
bgneal@109
|
520
|
bgneal@109
|
521
|
bgneal@109
|
522 @login_required
|
bgneal@109
|
523 def mod_topic_delete(request, id):
|
bgneal@109
|
524 """
|
bgneal@109
|
525 This view function is for moderators to delete an entire topic.
|
bgneal@109
|
526 """
|
bgneal@109
|
527 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
528 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
529 forum_url = topic.forum.get_absolute_url()
|
bgneal@109
|
530 _delete_topic(topic)
|
bgneal@109
|
531 return HttpResponseRedirect(forum_url)
|
bgneal@109
|
532
|
bgneal@110
|
533 return HttpResponseForbidden()
|
bgneal@110
|
534
|
bgneal@110
|
535
|
bgneal@110
|
536 @login_required
|
bgneal@110
|
537 def mod_topic_move(request, id):
|
bgneal@110
|
538 """
|
bgneal@110
|
539 This view function is for moderators to move a topic to a different forum.
|
bgneal@110
|
540 """
|
bgneal@110
|
541 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@110
|
542 if not _can_moderate(topic.forum, request.user):
|
bgneal@110
|
543 return HttpResponseForbidden()
|
bgneal@110
|
544
|
bgneal@110
|
545 if request.method == 'POST':
|
bgneal@110
|
546 form = MoveTopicForm(request.user, request.POST)
|
bgneal@110
|
547 if form.is_valid():
|
bgneal@110
|
548 new_forum = form.cleaned_data['forums']
|
bgneal@110
|
549 old_forum = topic.forum
|
bgneal@111
|
550 _move_topic(topic, old_forum, new_forum)
|
bgneal@110
|
551 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@110
|
552 else:
|
bgneal@110
|
553 form = MoveTopicForm(request.user)
|
bgneal@110
|
554
|
bgneal@110
|
555 return render_to_response('forums/move_topic.html', {
|
bgneal@110
|
556 'forum': topic.forum,
|
bgneal@110
|
557 'topic': topic,
|
bgneal@110
|
558 'form': form,
|
bgneal@110
|
559 },
|
bgneal@110
|
560 context_instance=RequestContext(request))
|
bgneal@109
|
561
|
bgneal@109
|
562
|
bgneal@111
|
563 @login_required
|
bgneal@111
|
564 def mod_forum(request, slug):
|
bgneal@111
|
565 """
|
bgneal@111
|
566 Displays a view to allow moderators to perform various operations
|
bgneal@111
|
567 on topics in a forum in bulk. We currently support mass locking/unlocking,
|
bgneal@111
|
568 stickying and unstickying, moving, and deleting topics.
|
bgneal@111
|
569 """
|
bgneal@111
|
570 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@111
|
571 if not _can_moderate(forum, request.user):
|
bgneal@111
|
572 return HttpResponseForbidden()
|
bgneal@111
|
573
|
bgneal@111
|
574 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@111
|
575 paginator = create_topic_paginator(topics)
|
bgneal@167
|
576 page_num = get_page_num(request)
|
bgneal@111
|
577 try:
|
bgneal@111
|
578 page = paginator.page(page_num)
|
bgneal@111
|
579 except InvalidPage:
|
bgneal@111
|
580 raise Http404
|
bgneal@111
|
581
|
bgneal@111
|
582 # we do this for the template since it is rendered twice
|
bgneal@111
|
583 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
584 form = None
|
bgneal@111
|
585
|
bgneal@111
|
586 if request.method == 'POST':
|
bgneal@111
|
587 topic_ids = request.POST.getlist('topic_ids')
|
bgneal@111
|
588 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
|
bgneal@111
|
589 url += '?page=%s' % page_num
|
bgneal@111
|
590
|
bgneal@111
|
591 if len(topic_ids):
|
bgneal@111
|
592 if request.POST.get('sticky'):
|
bgneal@111
|
593 _bulk_sticky(forum, topic_ids)
|
bgneal@111
|
594 return HttpResponseRedirect(url)
|
bgneal@111
|
595 elif request.POST.get('lock'):
|
bgneal@111
|
596 _bulk_lock(forum, topic_ids)
|
bgneal@111
|
597 return HttpResponseRedirect(url)
|
bgneal@111
|
598 elif request.POST.get('delete'):
|
bgneal@111
|
599 _bulk_delete(forum, topic_ids)
|
bgneal@111
|
600 return HttpResponseRedirect(url)
|
bgneal@111
|
601 elif request.POST.get('move'):
|
bgneal@111
|
602 form = MoveTopicForm(request.user, request.POST, hide_label=True)
|
bgneal@111
|
603 if form.is_valid():
|
bgneal@111
|
604 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
|
bgneal@111
|
605 return HttpResponseRedirect(url)
|
bgneal@111
|
606
|
bgneal@111
|
607 if form is None:
|
bgneal@111
|
608 form = MoveTopicForm(request.user, hide_label=True)
|
bgneal@111
|
609
|
bgneal@111
|
610 return render_to_response('forums/mod_forum.html', {
|
bgneal@111
|
611 'forum': forum,
|
bgneal@111
|
612 'page': page,
|
bgneal@111
|
613 'page_nav': page_nav,
|
bgneal@111
|
614 'form': form,
|
bgneal@111
|
615 },
|
bgneal@111
|
616 context_instance=RequestContext(request))
|
bgneal@111
|
617
|
bgneal@111
|
618
|
bgneal@113
|
619 @login_required
|
bgneal@113
|
620 @require_POST
|
bgneal@113
|
621 def forum_catchup(request, slug):
|
bgneal@113
|
622 """
|
bgneal@113
|
623 This view marks all the topics in the forum as being read.
|
bgneal@113
|
624 """
|
bgneal@113
|
625 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@113
|
626
|
bgneal@113
|
627 if not forum.category.can_access(request.user):
|
bgneal@113
|
628 return HttpResponseForbidden()
|
bgneal@113
|
629
|
bgneal@113
|
630 forum.catchup(request.user)
|
bgneal@113
|
631 return HttpResponseRedirect(forum.get_absolute_url())
|
bgneal@113
|
632
|
bgneal@113
|
633
|
bgneal@115
|
634 @login_required
|
bgneal@115
|
635 def mod_topic_split(request, id):
|
bgneal@115
|
636 """
|
bgneal@115
|
637 This view function allows moderators to split posts off to a new topic.
|
bgneal@115
|
638 """
|
bgneal@115
|
639 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@115
|
640 if not _can_moderate(topic.forum, request.user):
|
bgneal@115
|
641 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
642
|
bgneal@115
|
643 if request.method == "POST":
|
bgneal@115
|
644 form = SplitTopicForm(request.user, request.POST)
|
bgneal@115
|
645 if form.is_valid():
|
bgneal@115
|
646 if form.split_at:
|
bgneal@115
|
647 _split_topic_at(topic, form.post_ids[0],
|
bgneal@115
|
648 form.cleaned_data['forums'],
|
bgneal@115
|
649 form.cleaned_data['name'])
|
bgneal@115
|
650 else:
|
bgneal@115
|
651 _split_topic(topic, form.post_ids,
|
bgneal@115
|
652 form.cleaned_data['forums'],
|
bgneal@115
|
653 form.cleaned_data['name'])
|
bgneal@115
|
654
|
bgneal@115
|
655 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
656 else:
|
bgneal@115
|
657 form = SplitTopicForm(request.user)
|
bgneal@115
|
658
|
bgneal@115
|
659 posts = topic.posts.select_related()
|
bgneal@115
|
660
|
bgneal@115
|
661 return render_to_response('forums/mod_split_topic.html', {
|
bgneal@115
|
662 'forum': topic.forum,
|
bgneal@115
|
663 'topic': topic,
|
bgneal@115
|
664 'posts': posts,
|
bgneal@115
|
665 'form': form,
|
bgneal@115
|
666 },
|
bgneal@115
|
667 context_instance=RequestContext(request))
|
bgneal@115
|
668
|
bgneal@115
|
669
|
bgneal@167
|
670 @login_required
|
bgneal@167
|
671 def unread_topics(request):
|
bgneal@168
|
672 """Displays the topics with unread posts for a given user."""
|
bgneal@168
|
673
|
bgneal@167
|
674 topics = get_unread_topics(request.user)
|
bgneal@167
|
675
|
bgneal@167
|
676 paginator = create_topic_paginator(topics)
|
bgneal@167
|
677 page_num = get_page_num(request)
|
bgneal@167
|
678 try:
|
bgneal@167
|
679 page = paginator.page(page_num)
|
bgneal@167
|
680 except InvalidPage:
|
bgneal@167
|
681 raise Http404
|
bgneal@167
|
682
|
bgneal@167
|
683 attach_topic_page_ranges(page.object_list)
|
bgneal@167
|
684
|
bgneal@167
|
685 # we do this for the template since it is rendered twice
|
bgneal@167
|
686 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@167
|
687
|
bgneal@167
|
688 return render_to_response('forums/topic_list.html', {
|
bgneal@167
|
689 'title': 'Topics With Unread Posts',
|
bgneal@167
|
690 'page': page,
|
bgneal@167
|
691 'page_nav': page_nav,
|
bgneal@167
|
692 },
|
bgneal@167
|
693 context_instance=RequestContext(request))
|
bgneal@167
|
694
|
bgneal@167
|
695
|
bgneal@168
|
696 def unanswered_topics(request):
|
bgneal@168
|
697 """Displays the topics with no replies."""
|
bgneal@168
|
698
|
bgneal@168
|
699 forum_ids = Forum.objects.forum_ids_for_user(request.user)
|
bgneal@168
|
700 topics = Topic.objects.filter(forum__id__in=forum_ids,
|
bgneal@168
|
701 post_count=1).select_related(
|
bgneal@168
|
702 'forum', 'user', 'last_post', 'last_post__user')
|
bgneal@168
|
703
|
bgneal@168
|
704 paginator = create_topic_paginator(topics)
|
bgneal@168
|
705 page_num = get_page_num(request)
|
bgneal@168
|
706 try:
|
bgneal@168
|
707 page = paginator.page(page_num)
|
bgneal@168
|
708 except InvalidPage:
|
bgneal@168
|
709 raise Http404
|
bgneal@168
|
710
|
bgneal@168
|
711 attach_topic_page_ranges(page.object_list)
|
bgneal@168
|
712
|
bgneal@168
|
713 # we do this for the template since it is rendered twice
|
bgneal@168
|
714 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@168
|
715
|
bgneal@168
|
716 return render_to_response('forums/topic_list.html', {
|
bgneal@168
|
717 'title': 'Unanswered Topics',
|
bgneal@168
|
718 'page': page,
|
bgneal@168
|
719 'page_nav': page_nav,
|
bgneal@168
|
720 },
|
bgneal@168
|
721 context_instance=RequestContext(request))
|
bgneal@168
|
722
|
bgneal@168
|
723
|
bgneal@169
|
724 @login_required
|
bgneal@169
|
725 def my_posts(request):
|
bgneal@169
|
726 """Displays a list of posts the requesting user made."""
|
bgneal@172
|
727 return _user_posts(request, request.user, request.user, 'My Posts')
|
bgneal@169
|
728
|
bgneal@172
|
729
|
bgneal@172
|
730 @login_required
|
bgneal@172
|
731 def posts_for_user(request, username):
|
bgneal@172
|
732 """Displays a list of posts by the given user.
|
bgneal@172
|
733 Only the forums that the requesting user can see are examined.
|
bgneal@172
|
734 """
|
bgneal@172
|
735 target_user = get_object_or_404(User, username=username)
|
bgneal@172
|
736 return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
|
bgneal@172
|
737
|
bgneal@172
|
738
|
bgneal@172
|
739 def _user_posts(request, target_user, req_user, page_title):
|
bgneal@172
|
740 """Displays a list of posts made by the target user.
|
bgneal@172
|
741 req_user is the user trying to view the posts. Only the forums
|
bgneal@172
|
742 req_user can see are searched.
|
bgneal@172
|
743 """
|
bgneal@172
|
744 forum_ids = Forum.objects.forum_ids_for_user(req_user)
|
bgneal@172
|
745 posts = Post.objects.filter(user=target_user,
|
bgneal@169
|
746 topic__forum__id__in=forum_ids).order_by(
|
bgneal@169
|
747 '-creation_date').select_related()
|
bgneal@169
|
748
|
bgneal@169
|
749 paginator = create_post_paginator(posts)
|
bgneal@169
|
750 page_num = get_page_num(request)
|
bgneal@169
|
751 try:
|
bgneal@169
|
752 page = paginator.page(page_num)
|
bgneal@169
|
753 except InvalidPage:
|
bgneal@169
|
754 raise Http404
|
bgneal@169
|
755
|
bgneal@169
|
756 # we do this for the template since it is rendered twice
|
bgneal@169
|
757 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@169
|
758
|
bgneal@169
|
759 return render_to_response('forums/post_list.html', {
|
bgneal@172
|
760 'title': page_title,
|
bgneal@169
|
761 'page': page,
|
bgneal@169
|
762 'page_nav': page_nav,
|
bgneal@169
|
763 },
|
bgneal@169
|
764 context_instance=RequestContext(request))
|
bgneal@169
|
765
|
bgneal@169
|
766
|
bgneal@109
|
767 def _can_moderate(forum, user):
|
bgneal@109
|
768 """
|
bgneal@109
|
769 Determines if a user has permission to moderate a given forum.
|
bgneal@109
|
770 """
|
bgneal@109
|
771 return user.is_authenticated() and (
|
bgneal@109
|
772 user.is_superuser or user in forum.moderators.all())
|
bgneal@109
|
773
|
bgneal@109
|
774
|
bgneal@108
|
775 def _can_post_in_topic(topic, user):
|
bgneal@108
|
776 """
|
bgneal@108
|
777 This function returns true if the given user can post in the given topic
|
bgneal@108
|
778 and false otherwise.
|
bgneal@108
|
779 """
|
bgneal@108
|
780 return (not topic.locked and topic.forum.category.can_access(user)) or \
|
bgneal@108
|
781 (user.is_superuser or user in topic.forum.moderators.all())
|
bgneal@108
|
782
|
bgneal@108
|
783
|
bgneal@108
|
784 def _bump_post_count(user):
|
bgneal@108
|
785 """
|
bgneal@108
|
786 Increments the forum_post_count for the given user.
|
bgneal@108
|
787 """
|
bgneal@108
|
788 profile = user.get_profile()
|
bgneal@108
|
789 profile.forum_post_count += 1
|
bgneal@108
|
790 profile.save()
|
bgneal@108
|
791
|
bgneal@108
|
792
|
bgneal@108
|
793 def _quote_message(who, message):
|
bgneal@111
|
794 """
|
bgneal@111
|
795 Builds a message reply by quoting the existing message in a
|
bgneal@111
|
796 typical email-like fashion. The quoting is compatible with Markdown.
|
bgneal@111
|
797 """
|
bgneal@111
|
798 header = '*%s wrote:*\n\n' % (who, )
|
bgneal@111
|
799 lines = wrap(message, 55).split('\n')
|
bgneal@111
|
800 for i, line in enumerate(lines):
|
bgneal@111
|
801 lines[i] = '> ' + line
|
bgneal@111
|
802 return header + '\n'.join(lines)
|
bgneal@111
|
803
|
bgneal@111
|
804
|
bgneal@111
|
805 def _move_topic(topic, old_forum, new_forum):
|
bgneal@111
|
806 if new_forum != old_forum:
|
bgneal@111
|
807 topic.forum = new_forum
|
bgneal@111
|
808 topic.save()
|
bgneal@111
|
809 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
|
bgneal@112
|
810 old_forum.sync()
|
bgneal@111
|
811 old_forum.save()
|
bgneal@112
|
812 new_forum.sync()
|
bgneal@111
|
813 new_forum.save()
|
bgneal@111
|
814
|
bgneal@111
|
815
|
bgneal@111
|
816 def _bulk_sticky(forum, topic_ids):
|
bgneal@111
|
817 """
|
bgneal@111
|
818 Performs a toggle on the sticky status for a given list of topic ids.
|
bgneal@111
|
819 """
|
bgneal@111
|
820 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
821 for topic in topics:
|
bgneal@111
|
822 if topic.forum == forum:
|
bgneal@111
|
823 topic.sticky = not topic.sticky
|
bgneal@111
|
824 topic.save()
|
bgneal@111
|
825
|
bgneal@111
|
826
|
bgneal@111
|
827 def _bulk_lock(forum, topic_ids):
|
bgneal@111
|
828 """
|
bgneal@111
|
829 Performs a toggle on the locked status for a given list of topic ids.
|
bgneal@111
|
830 """
|
bgneal@111
|
831 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
832 for topic in topics:
|
bgneal@111
|
833 if topic.forum == forum:
|
bgneal@111
|
834 topic.locked = not topic.locked
|
bgneal@111
|
835 topic.save()
|
bgneal@111
|
836
|
bgneal@111
|
837
|
bgneal@111
|
838 def _bulk_delete(forum, topic_ids):
|
bgneal@111
|
839 """
|
bgneal@111
|
840 Deletes the list of topics.
|
bgneal@111
|
841 """
|
bgneal@111
|
842 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
843 for topic in topics:
|
bgneal@111
|
844 if topic.forum == forum:
|
bgneal@111
|
845 _delete_topic(topic)
|
bgneal@111
|
846
|
bgneal@111
|
847
|
bgneal@111
|
848 def _bulk_move(topic_ids, old_forum, new_forum):
|
bgneal@111
|
849 """
|
bgneal@111
|
850 Moves the list of topics to a new forum.
|
bgneal@111
|
851 """
|
bgneal@111
|
852 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
853 for topic in topics:
|
bgneal@111
|
854 if topic.forum == old_forum:
|
bgneal@111
|
855 _move_topic(topic, old_forum, new_forum)
|
bgneal@111
|
856
|
bgneal@113
|
857
|
bgneal@113
|
858 def _update_last_visit(user, topic):
|
bgneal@113
|
859 """
|
bgneal@113
|
860 Does the bookkeeping for the last visit status for the user to the
|
bgneal@113
|
861 topic/forum.
|
bgneal@113
|
862 """
|
bgneal@113
|
863 now = datetime.datetime.now()
|
bgneal@113
|
864 try:
|
bgneal@113
|
865 flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
|
bgneal@113
|
866 except ForumLastVisit.DoesNotExist:
|
bgneal@113
|
867 flv = ForumLastVisit(user=user, forum=topic.forum)
|
bgneal@113
|
868 flv.begin_date = now
|
bgneal@113
|
869
|
bgneal@113
|
870 flv.end_date = now
|
bgneal@113
|
871 flv.save()
|
bgneal@113
|
872
|
bgneal@113
|
873 if topic.update_date > flv.begin_date:
|
bgneal@113
|
874 try:
|
bgneal@113
|
875 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
|
bgneal@113
|
876 except TopicLastVisit.DoesNotExist:
|
bgneal@113
|
877 tlv = TopicLastVisit(user=user, topic=topic)
|
bgneal@113
|
878
|
bgneal@113
|
879 tlv.touch()
|
bgneal@113
|
880 tlv.save()
|
bgneal@113
|
881
|
bgneal@115
|
882
|
bgneal@115
|
883 def _split_topic_at(topic, post_id, new_forum, new_name):
|
bgneal@115
|
884 """
|
bgneal@115
|
885 This function splits the post given by post_id and all posts that come
|
bgneal@115
|
886 after it in the given topic to a new topic in a new forum.
|
bgneal@115
|
887 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
888 """
|
bgneal@115
|
889 post = get_object_or_404(Post, id=post_id)
|
bgneal@115
|
890 if post.topic == topic:
|
bgneal@115
|
891 post_ids = Post.objects.filter(topic=topic,
|
bgneal@115
|
892 creation_date__gte=post.creation_date).values_list('id', flat=True)
|
bgneal@115
|
893 _split_topic(topic, post_ids, new_forum, new_name)
|
bgneal@115
|
894
|
bgneal@115
|
895
|
bgneal@115
|
896 def _split_topic(topic, post_ids, new_forum, new_name):
|
bgneal@115
|
897 """
|
bgneal@115
|
898 This function splits the posts given by the post_ids list in the
|
bgneal@115
|
899 given topic to a new topic in a new forum.
|
bgneal@115
|
900 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
901 """
|
bgneal@115
|
902 posts = Post.objects.filter(topic=topic, id__in=post_ids)
|
bgneal@115
|
903 if len(posts) > 0:
|
bgneal@115
|
904 new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
|
bgneal@115
|
905 new_topic.save()
|
bgneal@115
|
906 for post in posts:
|
bgneal@115
|
907 post.topic = new_topic
|
bgneal@115
|
908 post.save()
|
bgneal@115
|
909
|
bgneal@115
|
910 topic.post_count_update()
|
bgneal@115
|
911 topic.save()
|
bgneal@115
|
912 new_topic.post_count_update()
|
bgneal@115
|
913 new_topic.save()
|
bgneal@115
|
914 topic.forum.sync()
|
bgneal@115
|
915 topic.forum.save()
|
bgneal@115
|
916 new_forum.sync()
|
bgneal@115
|
917 new_forum.save()
|