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@89
|
6 from django.http import HttpResponseBadRequest
|
bgneal@90
|
7 from django.http import HttpResponseForbidden
|
bgneal@83
|
8 from django.http import HttpResponseRedirect
|
bgneal@83
|
9 from django.core.urlresolvers import reverse
|
bgneal@82
|
10 from django.shortcuts import get_object_or_404
|
bgneal@81
|
11 from django.shortcuts import render_to_response
|
bgneal@81
|
12 from django.template import RequestContext
|
bgneal@89
|
13 from django.views.decorators.http import require_POST
|
bgneal@81
|
14
|
bgneal@90
|
15 from core.paginator import DiggPaginator
|
bgneal@81
|
16 from forums.models import Forum
|
bgneal@83
|
17 from forums.models import Topic
|
bgneal@83
|
18 from forums.forms import NewTopicForm
|
bgneal@86
|
19 from forums.forms import PostForm
|
bgneal@81
|
20
|
bgneal@90
|
21 #######################################################################
|
bgneal@90
|
22
|
bgneal@90
|
23 POSTS_PER_PAGE = 2
|
bgneal@90
|
24
|
bgneal@90
|
25 def create_paginator(links):
|
bgneal@90
|
26 return DiggPaginator(links, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
27
|
bgneal@90
|
28 #######################################################################
|
bgneal@81
|
29
|
bgneal@81
|
30 def index(request):
|
bgneal@82
|
31 """
|
bgneal@82
|
32 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
33 """
|
bgneal@81
|
34 forums = Forum.objects.all().select_related()
|
bgneal@81
|
35 cats = {}
|
bgneal@81
|
36 for forum in forums:
|
bgneal@81
|
37 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
38 'cat': forum.category,
|
bgneal@81
|
39 'forums': [],
|
bgneal@81
|
40 })
|
bgneal@81
|
41 cat['forums'].append(forum)
|
bgneal@81
|
42
|
bgneal@81
|
43 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
44 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
45
|
bgneal@81
|
46 return render_to_response('forums/index.html', {
|
bgneal@81
|
47 'cats': cats,
|
bgneal@81
|
48 },
|
bgneal@81
|
49 context_instance=RequestContext(request))
|
bgneal@81
|
50
|
bgneal@82
|
51
|
bgneal@81
|
52 def forum_index(request, slug):
|
bgneal@82
|
53 """
|
bgneal@82
|
54 Displays all the topics in a forum.
|
bgneal@82
|
55 """
|
bgneal@82
|
56 forum = get_object_or_404(Forum, slug=slug)
|
bgneal@82
|
57 topics = forum.topics.select_related()
|
bgneal@82
|
58
|
bgneal@82
|
59 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
60 'forum': forum,
|
bgneal@82
|
61 'topics': topics,
|
bgneal@82
|
62 },
|
bgneal@82
|
63 context_instance=RequestContext(request))
|
bgneal@82
|
64
|
bgneal@82
|
65
|
bgneal@82
|
66 def topic_index(request, id):
|
bgneal@82
|
67 """
|
bgneal@82
|
68 Displays all the posts in a topic.
|
bgneal@82
|
69 """
|
bgneal@86
|
70 topic = get_object_or_404(Topic, pk=id)
|
bgneal@86
|
71 topic.view_count += 1
|
bgneal@86
|
72 topic.save()
|
bgneal@86
|
73
|
bgneal@86
|
74 posts = topic.posts.select_related()
|
bgneal@90
|
75 paginator = create_paginator(posts)
|
bgneal@90
|
76 page_num = int(request.GET.get('page', '1'))
|
bgneal@90
|
77 try:
|
bgneal@90
|
78 page = paginator.page(page_num)
|
bgneal@90
|
79 except InvalidPage:
|
bgneal@90
|
80 raise Http404
|
bgneal@90
|
81
|
bgneal@90
|
82 last_page = page_num == paginator.num_pages
|
bgneal@86
|
83
|
bgneal@86
|
84 return render_to_response('forums/topic.html', {
|
bgneal@86
|
85 'forum': topic.forum,
|
bgneal@86
|
86 'topic': topic,
|
bgneal@90
|
87 'page': page,
|
bgneal@87
|
88 'last_page': last_page,
|
bgneal@89
|
89 'form': PostForm(initial={'topic_id': topic.id}),
|
bgneal@86
|
90 },
|
bgneal@86
|
91 context_instance=RequestContext(request))
|
bgneal@83
|
92
|
bgneal@83
|
93
|
bgneal@83
|
94 @login_required
|
bgneal@83
|
95 def new_topic(request, slug):
|
bgneal@83
|
96 """
|
bgneal@83
|
97 This view handles the creation of new topics.
|
bgneal@83
|
98 """
|
bgneal@83
|
99 forum = get_object_or_404(Forum, slug=slug)
|
bgneal@83
|
100 if request.method == 'POST':
|
bgneal@83
|
101 form = NewTopicForm(request.POST)
|
bgneal@83
|
102 if form.is_valid():
|
bgneal@83
|
103 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
|
bgneal@83
|
104 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
105 kwargs={'tid': topic.pk}))
|
bgneal@83
|
106 else:
|
bgneal@83
|
107 form = NewTopicForm()
|
bgneal@83
|
108
|
bgneal@83
|
109 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
110 'forum': forum,
|
bgneal@83
|
111 'form': form,
|
bgneal@83
|
112 },
|
bgneal@83
|
113 context_instance=RequestContext(request))
|
bgneal@83
|
114
|
bgneal@83
|
115
|
bgneal@83
|
116 @login_required
|
bgneal@83
|
117 def new_topic_thanks(request, tid):
|
bgneal@83
|
118 """
|
bgneal@83
|
119 This view displays the success page for a newly created topic.
|
bgneal@83
|
120 """
|
bgneal@83
|
121 topic = get_object_or_404(Topic, pk=tid)
|
bgneal@83
|
122 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
123 'forum': topic.forum,
|
bgneal@83
|
124 'topic': topic,
|
bgneal@83
|
125 },
|
bgneal@83
|
126 context_instance=RequestContext(request))
|
bgneal@89
|
127
|
bgneal@89
|
128
|
bgneal@89
|
129 @require_POST
|
bgneal@89
|
130 def quick_reply_ajax(request):
|
bgneal@89
|
131 """
|
bgneal@89
|
132 This function handles the quick reply to a thread function. This
|
bgneal@89
|
133 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
134 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
135 to the document.
|
bgneal@89
|
136 """
|
bgneal@90
|
137 if not request.user.is_authenticated():
|
bgneal@90
|
138 return HttpResponseForbidden()
|
bgneal@90
|
139
|
bgneal@89
|
140 form = PostForm(request.POST)
|
bgneal@89
|
141 if form.is_valid():
|
bgneal@89
|
142 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
|
bgneal@89
|
143 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
144 'post': post,
|
bgneal@89
|
145 },
|
bgneal@89
|
146 context_instance=RequestContext(request))
|
bgneal@89
|
147
|
bgneal@89
|
148 return HttpResponseBadRequest();
|
bgneal@89
|
149
|