comparison gpp/forums/views.py @ 100:eb9f99382476

Forums: groups support. Some experimentation with select_related() to reduce queries. There are more opportunities for this, see the TODO comments in views.py.
author Brian Neal <bgneal@gmail.com>
date Tue, 15 Sep 2009 03:15:20 +0000
parents 10d6182b9f6e
children 4bbb6a9aa317
comparison
equal deleted inserted replaced
99:10d6182b9f6e 100:eb9f99382476
39 39
40 def index(request): 40 def index(request):
41 """ 41 """
42 This view displays all the forums available, ordered in each category. 42 This view displays all the forums available, ordered in each category.
43 """ 43 """
44 forums = Forum.objects.all().select_related() 44 forums = Forum.objects.forums_for_user(request.user)
45 cats = {} 45 cats = {}
46 for forum in forums: 46 for forum in forums:
47 cat = cats.setdefault(forum.category.id, { 47 cat = cats.setdefault(forum.category.id, {
48 'cat': forum.category, 48 'cat': forum.category,
49 'forums': [], 49 'forums': [],
61 61
62 def forum_index(request, slug): 62 def forum_index(request, slug):
63 """ 63 """
64 Displays all the topics in a forum. 64 Displays all the topics in a forum.
65 """ 65 """
66 # TODO: use select_related to save queries
66 forum = get_object_or_404(Forum, slug=slug) 67 forum = get_object_or_404(Forum, slug=slug)
67 topics = forum.topics.select_related() 68
69 if not forum.category.can_access(request.user):
70 return HttpResponseForbidden()
71
72 topics = forum.topics.select_related('last_post', 'last_post__user')
68 paginator = create_topic_paginator(topics) 73 paginator = create_topic_paginator(topics)
69 page_num = int(request.GET.get('page', 1)) 74 page_num = int(request.GET.get('page', 1))
70 try: 75 try:
71 page = paginator.page(page_num) 76 page = paginator.page(page_num)
72 except InvalidPage: 77 except InvalidPage:
85 90
86 def topic_index(request, id): 91 def topic_index(request, id):
87 """ 92 """
88 Displays all the posts in a topic. 93 Displays all the posts in a topic.
89 """ 94 """
90 topic = get_object_or_404(Topic, pk=id) 95 #topic = get_object_or_404(Topic, pk=id)
96 #TODO: optimize this or package it up somehow
97 # this saves 2 queries vs the get_object_or_404
98 qs = Topic.objects.filter(pk=id).select_related()
99 try:
100 topic = qs[0]
101 except Topic.DoesNotExist:
102 raise Http404
103
104 if not topic.forum.category.can_access(request.user):
105 return HttpResponseForbidden()
106
91 topic.view_count += 1 107 topic.view_count += 1
92 topic.save() 108 topic.save()
93 109
94 posts = topic.posts.select_related() 110 posts = topic.posts.select_related()
95 paginator = create_post_paginator(posts) 111 paginator = create_post_paginator(posts)
119 def new_topic(request, slug): 135 def new_topic(request, slug):
120 """ 136 """
121 This view handles the creation of new topics. 137 This view handles the creation of new topics.
122 """ 138 """
123 forum = get_object_or_404(Forum, slug=slug) 139 forum = get_object_or_404(Forum, slug=slug)
140
141 if not forum.category.can_access(request.user):
142 return HttpResponseForbidden()
143
124 if request.method == 'POST': 144 if request.method == 'POST':
125 form = NewTopicForm(request.POST) 145 form = NewTopicForm(request.POST)
126 if form.is_valid(): 146 if form.is_valid():
127 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR")) 147 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
128 return HttpResponseRedirect(reverse('forums-new_topic_thanks', 148 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
161 if not request.user.is_authenticated(): 181 if not request.user.is_authenticated():
162 return HttpResponseForbidden() 182 return HttpResponseForbidden()
163 183
164 form = PostForm(request.POST) 184 form = PostForm(request.POST)
165 if form.is_valid(): 185 if form.is_valid():
186 if not form.topic.forum.category.can_access(request.user):
187 return HttpResponseForbidden()
188
166 post = form.save(request.user, request.META.get("REMOTE_ADDR")) 189 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
167 return render_to_response('forums/display_post.html', { 190 return render_to_response('forums/display_post.html', {
168 'post': post, 191 'post': post,
169 }, 192 },
170 context_instance=RequestContext(request)) 193 context_instance=RequestContext(request))