bgneal@81
|
1 """
|
bgneal@81
|
2 Views for the forums application.
|
bgneal@81
|
3 """
|
bgneal@83
|
4 from django.contrib.auth.decorators import login_required
|
bgneal@82
|
5 from django.http import Http404
|
bgneal@98
|
6 from django.http import HttpResponse
|
bgneal@89
|
7 from django.http import HttpResponseBadRequest
|
bgneal@90
|
8 from django.http import HttpResponseForbidden
|
bgneal@83
|
9 from django.http import HttpResponseRedirect
|
bgneal@83
|
10 from django.core.urlresolvers import reverse
|
bgneal@91
|
11 from django.core.paginator import InvalidPage
|
bgneal@82
|
12 from django.shortcuts import get_object_or_404
|
bgneal@81
|
13 from django.shortcuts import render_to_response
|
bgneal@97
|
14 from django.template.loader import render_to_string
|
bgneal@81
|
15 from django.template import RequestContext
|
bgneal@89
|
16 from django.views.decorators.http import require_POST
|
bgneal@81
|
17
|
bgneal@90
|
18 from core.paginator import DiggPaginator
|
bgneal@98
|
19 from core.functions import email_admins
|
bgneal@81
|
20 from forums.models import Forum
|
bgneal@83
|
21 from forums.models import Topic
|
bgneal@91
|
22 from forums.models import Post
|
bgneal@98
|
23 from forums.models import FlaggedPost
|
bgneal@83
|
24 from forums.forms import NewTopicForm
|
bgneal@86
|
25 from forums.forms import PostForm
|
bgneal@81
|
26
|
bgneal@90
|
27 #######################################################################
|
bgneal@90
|
28
|
bgneal@93
|
29 TOPICS_PER_PAGE = 50
|
bgneal@90
|
30 POSTS_PER_PAGE = 2
|
bgneal@90
|
31
|
bgneal@93
|
32 def create_topic_paginator(topics):
|
bgneal@93
|
33 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@93
|
34
|
bgneal@93
|
35 def create_post_paginator(posts):
|
bgneal@93
|
36 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
37
|
bgneal@90
|
38 #######################################################################
|
bgneal@81
|
39
|
bgneal@81
|
40 def index(request):
|
bgneal@82
|
41 """
|
bgneal@82
|
42 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
43 """
|
bgneal@81
|
44 forums = Forum.objects.all().select_related()
|
bgneal@81
|
45 cats = {}
|
bgneal@81
|
46 for forum in forums:
|
bgneal@81
|
47 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
48 'cat': forum.category,
|
bgneal@81
|
49 'forums': [],
|
bgneal@81
|
50 })
|
bgneal@81
|
51 cat['forums'].append(forum)
|
bgneal@81
|
52
|
bgneal@81
|
53 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
54 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
55
|
bgneal@81
|
56 return render_to_response('forums/index.html', {
|
bgneal@81
|
57 'cats': cats,
|
bgneal@81
|
58 },
|
bgneal@81
|
59 context_instance=RequestContext(request))
|
bgneal@81
|
60
|
bgneal@82
|
61
|
bgneal@81
|
62 def forum_index(request, slug):
|
bgneal@82
|
63 """
|
bgneal@82
|
64 Displays all the topics in a forum.
|
bgneal@82
|
65 """
|
bgneal@82
|
66 forum = get_object_or_404(Forum, slug=slug)
|
bgneal@82
|
67 topics = forum.topics.select_related()
|
bgneal@93
|
68 paginator = create_topic_paginator(topics)
|
bgneal@93
|
69 page_num = int(request.GET.get('page', 1))
|
bgneal@93
|
70 try:
|
bgneal@93
|
71 page = paginator.page(page_num)
|
bgneal@93
|
72 except InvalidPage:
|
bgneal@93
|
73 raise Http404
|
bgneal@97
|
74
|
bgneal@97
|
75 # we do this for the template since it is rendered twice
|
bgneal@97
|
76 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@82
|
77
|
bgneal@82
|
78 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
79 'forum': forum,
|
bgneal@93
|
80 'page': page,
|
bgneal@97
|
81 'page_nav': page_nav,
|
bgneal@82
|
82 },
|
bgneal@82
|
83 context_instance=RequestContext(request))
|
bgneal@82
|
84
|
bgneal@82
|
85
|
bgneal@82
|
86 def topic_index(request, id):
|
bgneal@82
|
87 """
|
bgneal@82
|
88 Displays all the posts in a topic.
|
bgneal@82
|
89 """
|
bgneal@86
|
90 topic = get_object_or_404(Topic, pk=id)
|
bgneal@86
|
91 topic.view_count += 1
|
bgneal@86
|
92 topic.save()
|
bgneal@86
|
93
|
bgneal@86
|
94 posts = topic.posts.select_related()
|
bgneal@93
|
95 paginator = create_post_paginator(posts)
|
bgneal@93
|
96 page_num = int(request.GET.get('page', 1))
|
bgneal@90
|
97 try:
|
bgneal@90
|
98 page = paginator.page(page_num)
|
bgneal@90
|
99 except InvalidPage:
|
bgneal@90
|
100 raise Http404
|
bgneal@90
|
101
|
bgneal@90
|
102 last_page = page_num == paginator.num_pages
|
bgneal@86
|
103
|
bgneal@97
|
104 # we do this for the template since it is rendered twice
|
bgneal@97
|
105 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@97
|
106
|
bgneal@86
|
107 return render_to_response('forums/topic.html', {
|
bgneal@86
|
108 'forum': topic.forum,
|
bgneal@86
|
109 'topic': topic,
|
bgneal@90
|
110 'page': page,
|
bgneal@97
|
111 'page_nav': page_nav,
|
bgneal@87
|
112 'last_page': last_page,
|
bgneal@89
|
113 'form': PostForm(initial={'topic_id': topic.id}),
|
bgneal@86
|
114 },
|
bgneal@86
|
115 context_instance=RequestContext(request))
|
bgneal@83
|
116
|
bgneal@83
|
117
|
bgneal@83
|
118 @login_required
|
bgneal@83
|
119 def new_topic(request, slug):
|
bgneal@83
|
120 """
|
bgneal@83
|
121 This view handles the creation of new topics.
|
bgneal@83
|
122 """
|
bgneal@83
|
123 forum = get_object_or_404(Forum, slug=slug)
|
bgneal@83
|
124 if request.method == 'POST':
|
bgneal@83
|
125 form = NewTopicForm(request.POST)
|
bgneal@83
|
126 if form.is_valid():
|
bgneal@83
|
127 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
|
bgneal@83
|
128 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
129 kwargs={'tid': topic.pk}))
|
bgneal@83
|
130 else:
|
bgneal@83
|
131 form = NewTopicForm()
|
bgneal@83
|
132
|
bgneal@83
|
133 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
134 'forum': forum,
|
bgneal@83
|
135 'form': form,
|
bgneal@83
|
136 },
|
bgneal@83
|
137 context_instance=RequestContext(request))
|
bgneal@83
|
138
|
bgneal@83
|
139
|
bgneal@83
|
140 @login_required
|
bgneal@83
|
141 def new_topic_thanks(request, tid):
|
bgneal@83
|
142 """
|
bgneal@83
|
143 This view displays the success page for a newly created topic.
|
bgneal@83
|
144 """
|
bgneal@83
|
145 topic = get_object_or_404(Topic, pk=tid)
|
bgneal@83
|
146 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
147 'forum': topic.forum,
|
bgneal@83
|
148 'topic': topic,
|
bgneal@83
|
149 },
|
bgneal@83
|
150 context_instance=RequestContext(request))
|
bgneal@89
|
151
|
bgneal@89
|
152
|
bgneal@89
|
153 @require_POST
|
bgneal@89
|
154 def quick_reply_ajax(request):
|
bgneal@89
|
155 """
|
bgneal@89
|
156 This function handles the quick reply to a thread function. This
|
bgneal@89
|
157 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
158 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
159 to the document.
|
bgneal@89
|
160 """
|
bgneal@90
|
161 if not request.user.is_authenticated():
|
bgneal@90
|
162 return HttpResponseForbidden()
|
bgneal@90
|
163
|
bgneal@89
|
164 form = PostForm(request.POST)
|
bgneal@89
|
165 if form.is_valid():
|
bgneal@89
|
166 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
|
bgneal@89
|
167 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
168 'post': post,
|
bgneal@89
|
169 },
|
bgneal@89
|
170 context_instance=RequestContext(request))
|
bgneal@89
|
171
|
bgneal@89
|
172 return HttpResponseBadRequest();
|
bgneal@89
|
173
|
bgneal@91
|
174
|
bgneal@91
|
175 def goto_post(request, post_id):
|
bgneal@91
|
176 """
|
bgneal@91
|
177 This function calculates what page a given post is on, then redirects
|
bgneal@91
|
178 to that URL. This function is the target of get_absolute_url() for
|
bgneal@91
|
179 Post objects.
|
bgneal@91
|
180 """
|
bgneal@91
|
181 post = get_object_or_404(Post, pk=post_id)
|
bgneal@91
|
182 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@91
|
183 page = count / POSTS_PER_PAGE + 1
|
bgneal@91
|
184 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
|
bgneal@91
|
185 '?page=%s#p%s' % (page, post.id)
|
bgneal@91
|
186 return HttpResponseRedirect(url)
|
bgneal@91
|
187
|
bgneal@98
|
188
|
bgneal@98
|
189 @require_POST
|
bgneal@98
|
190 def flag_post(request):
|
bgneal@98
|
191 """
|
bgneal@98
|
192 This function handles the flagging of posts by users. This function should
|
bgneal@98
|
193 be the target of an AJAX post.
|
bgneal@98
|
194 """
|
bgneal@98
|
195 if not request.user.is_authenticated():
|
bgneal@99
|
196 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@98
|
197
|
bgneal@98
|
198 id = request.POST.get('id')
|
bgneal@98
|
199 if id is None:
|
bgneal@98
|
200 return HttpResponseBadRequest('No post id')
|
bgneal@98
|
201
|
bgneal@98
|
202 try:
|
bgneal@98
|
203 post = Post.objects.get(pk=id)
|
bgneal@98
|
204 except Post.DoesNotExist:
|
bgneal@98
|
205 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@98
|
206
|
bgneal@98
|
207 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@98
|
208 flag.save()
|
bgneal@98
|
209 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@98
|
210
|
bgneal@98
|
211 A user has flagged a forum post for review.
|
bgneal@98
|
212 """)
|
bgneal@98
|
213 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@98
|
214 'Thanks for helping to improve the discussions on this site.')
|