Mercurial > public > sg101
comparison gpp/news/views.py @ 399:24f1230f3ee3
Fixing #193; reduce news query counts by grabbing tags and comment counts in bulk. Increased news items per page from 5 to 10.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 Mar 2011 03:08:05 +0000 |
parents | 75ea1a8be7f2 |
children |
comparison
equal
deleted
inserted
replaced
398:701730b2fcda | 399:24f1230f3ee3 |
---|---|
26 from news.models import Category | 26 from news.models import Category |
27 from news.models import PendingStory | 27 from news.models import PendingStory |
28 from news.models import Story | 28 from news.models import Story |
29 from news.forms import AddNewsForm | 29 from news.forms import AddNewsForm |
30 from news.forms import SendStoryForm | 30 from news.forms import SendStoryForm |
31 | 31 from news.utils import attach_extra_attrs |
32 NEWS_PER_PAGE = 5 | 32 |
33 | |
34 NEWS_PER_PAGE = 10 | |
33 | 35 |
34 ####################################################################### | 36 ####################################################################### |
35 | 37 |
36 def create_paginator(stories): | 38 def create_paginator(stories): |
37 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) | 39 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) |
38 | 40 |
39 ####################################################################### | 41 ####################################################################### |
40 | 42 |
41 def index(request): | 43 def index(request): |
42 stories = Story.objects.all().select_related() | 44 # Defer the tags field because we are going to get all the |
43 paginator = create_paginator(stories) | 45 # tags out in 1 query later... |
44 | 46 stories = Story.objects.all().defer('tags').select_related() |
45 page = get_page(request.GET) | 47 paginator = create_paginator(stories) |
46 try: | 48 |
47 the_page = paginator.page(page) | 49 page = get_page(request.GET) |
48 except InvalidPage: | 50 try: |
49 raise Http404 | 51 the_page = paginator.page(page) |
50 | 52 except InvalidPage: |
51 return render_to_response('news/index.html', { | 53 raise Http404 |
52 'title': 'Main Index', | 54 |
53 'page': the_page, | 55 # Go get the tags and comment counts for all these stories in bulk rather |
54 }, | 56 # than one at a time in the template; this saves database queries |
55 context_instance = RequestContext(request)) | 57 attach_extra_attrs(the_page.object_list) |
58 | |
59 return render_to_response('news/index.html', { | |
60 'title': 'Main Index', | |
61 'page': the_page, | |
62 }, | |
63 context_instance = RequestContext(request)) | |
56 | 64 |
57 ####################################################################### | 65 ####################################################################### |
58 | 66 |
59 def archive_index(request): | 67 def archive_index(request): |
60 dates = Story.objects.dates('date_submitted', 'month', order='DESC') | 68 dates = Story.objects.dates('date_submitted', 'month', order='DESC') |
61 return render_to_response('news/archive_index.html', { | 69 return render_to_response('news/archive_index.html', { |
62 'title': 'News Archive', | 70 'title': 'News Archive', |
63 'dates': dates, | 71 'dates': dates, |
64 }, | 72 }, |
65 context_instance = RequestContext(request)) | 73 context_instance = RequestContext(request)) |
66 | 74 |
67 ####################################################################### | 75 ####################################################################### |
68 | 76 |
69 def archive(request, year, month): | 77 def archive(request, year, month): |
70 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) | 78 stories = Story.objects.defer('tags').filter(date_submitted__year=year, |
71 paginator = create_paginator(stories) | 79 date_submitted__month=month).select_related() |
72 page = get_page(request.GET) | 80 paginator = create_paginator(stories) |
73 try: | 81 page = get_page(request.GET) |
74 the_page = paginator.page(page) | 82 try: |
75 except InvalidPage: | 83 the_page = paginator.page(page) |
76 raise Http404 | 84 except InvalidPage: |
77 | 85 raise Http404 |
78 month_name = datetime.date(int(year), int(month), 1).strftime('%B') | 86 |
79 | 87 attach_extra_attrs(the_page.object_list) |
80 return render_to_response('news/index.html', { | 88 |
81 'title': 'Archive for %s, %s' % (month_name, year), | 89 month_name = datetime.date(int(year), int(month), 1).strftime('%B') |
82 'page': the_page, | 90 |
83 }, | 91 return render_to_response('news/index.html', { |
84 context_instance = RequestContext(request)) | 92 'title': 'Archive for %s, %s' % (month_name, year), |
93 'page': the_page, | |
94 }, | |
95 context_instance = RequestContext(request)) | |
85 | 96 |
86 ####################################################################### | 97 ####################################################################### |
87 | 98 |
88 def category_index(request): | 99 def category_index(request): |
89 categories = Category.objects.all().select_related() | 100 categories = Category.objects.all().select_related() |
90 cat_list = [] | 101 cat_list = [] |
91 for cat in categories: | 102 for cat in categories: |
92 cat_list.append((cat, cat.story_set.defer('tags')[:10])) | 103 cat_list.append((cat, cat.story_set.defer('tags')[:10])) |
93 | 104 |
94 return render_to_response('news/category_index.html', { | 105 return render_to_response('news/category_index.html', { |
95 'cat_list': cat_list, | 106 'cat_list': cat_list, |
96 }, | 107 }, |
97 context_instance = RequestContext(request)) | 108 context_instance = RequestContext(request)) |
98 | 109 |
99 ####################################################################### | 110 ####################################################################### |
100 | 111 |
101 def category(request, slug): | 112 def category(request, slug): |
102 category = get_object_or_404(Category, slug=slug) | 113 category = get_object_or_404(Category, slug=slug) |
103 stories = Story.objects.filter(category=category) | 114 stories = Story.objects.defer('tags').filter(category=category).select_related() |
104 paginator = create_paginator(stories) | 115 paginator = create_paginator(stories) |
105 page = get_page(request.GET) | 116 page = get_page(request.GET) |
106 try: | 117 try: |
107 the_page = paginator.page(page) | 118 the_page = paginator.page(page) |
108 except InvalidPage: | 119 except InvalidPage: |
109 raise Http404 | 120 raise Http404 |
110 | 121 |
111 return render_to_response('news/index.html', { | 122 attach_extra_attrs(the_page.object_list) |
112 'title': 'Category: ' + category.title, | 123 |
113 'page': the_page, | 124 return render_to_response('news/index.html', { |
114 }, | 125 'title': 'Category: ' + category.title, |
115 context_instance = RequestContext(request)) | 126 'page': the_page, |
127 }, | |
128 context_instance = RequestContext(request)) | |
116 | 129 |
117 ####################################################################### | 130 ####################################################################### |
118 | 131 |
119 def story(request, story_id): | 132 def story(request, story_id): |
120 story = get_object_or_404(Story, pk=story_id) | 133 story = get_object_or_404(Story, pk=story_id) |
121 return render_to_response('news/story.html', { | 134 return render_to_response('news/story.html', { |
122 'story': story, | 135 'story': story, |
123 }, | 136 }, |
124 context_instance=RequestContext(request)) | 137 context_instance=RequestContext(request)) |
125 | 138 |
126 ####################################################################### | 139 ####################################################################### |
127 | 140 |
128 @login_required | 141 @login_required |
129 def submit(request): | 142 def submit(request): |
130 if request.method == "POST": | 143 if request.method == "POST": |
131 add_form = AddNewsForm(request.POST) | 144 add_form = AddNewsForm(request.POST) |
132 if add_form.is_valid(): | 145 if add_form.is_valid(): |
133 pending_story = add_form.save(commit=False) | 146 pending_story = add_form.save(commit=False) |
134 pending_story.submitter = request.user | 147 pending_story.submitter = request.user |
135 pending_story.short_text = clean_html(pending_story.short_text) | 148 pending_story.short_text = clean_html(pending_story.short_text) |
136 pending_story.long_text = clean_html(pending_story.long_text) | 149 pending_story.long_text = clean_html(pending_story.long_text) |
137 pending_story.save() | 150 pending_story.save() |
138 return HttpResponseRedirect(reverse('news.views.submit_thanks')) | 151 return HttpResponseRedirect(reverse('news.views.submit_thanks')) |
139 else: | 152 else: |
140 add_form = AddNewsForm() | 153 add_form = AddNewsForm() |
141 | 154 |
142 return render_to_response('news/submit_news.html', { | 155 return render_to_response('news/submit_news.html', { |
143 'add_form': add_form, | 156 'add_form': add_form, |
144 }, | 157 }, |
145 context_instance = RequestContext(request)) | 158 context_instance = RequestContext(request)) |
146 | 159 |
147 ####################################################################### | 160 ####################################################################### |
148 | 161 |
149 @login_required | 162 @login_required |
150 def submit_thanks(request): | 163 def submit_thanks(request): |
151 return render_to_response('news/submit_news.html', { | 164 return render_to_response('news/submit_news.html', { |
152 }, | 165 }, |
153 context_instance = RequestContext(request)) | 166 context_instance = RequestContext(request)) |
154 | 167 |
155 ####################################################################### | 168 ####################################################################### |
156 | 169 |
157 def tags(request): | 170 def tags(request): |
158 tags = Tag.objects.cloud_for_model(Story) | 171 tags = Tag.objects.cloud_for_model(Story) |
159 return render_to_response('news/tag_index.html', { | 172 return render_to_response('news/tag_index.html', { |
160 'tags': tags, | 173 'tags': tags, |
161 }, | 174 }, |
162 context_instance = RequestContext(request)) | 175 context_instance = RequestContext(request)) |
163 | 176 |
164 ####################################################################### | 177 ####################################################################### |
165 | 178 |
166 def tag(request, tag_name): | 179 def tag(request, tag_name): |
167 tag = get_object_or_404(Tag, name=tag_name) | 180 tag = get_object_or_404(Tag, name=tag_name) |
168 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) | 181 stories = TaggedItem.objects.get_by_model( |
169 paginator = create_paginator(stories) | 182 Story.objects.defer('tags').select_related(), tag) |
170 page = get_page(request.GET) | 183 paginator = create_paginator(stories) |
171 try: | 184 page = get_page(request.GET) |
172 the_page = paginator.page(page) | 185 try: |
173 except InvalidPage: | 186 the_page = paginator.page(page) |
174 raise Http404 | 187 except InvalidPage: |
175 | 188 raise Http404 |
176 return render_to_response('news/index.html', { | 189 |
177 'title': 'Stories with tag: "%s"' % tag_name, | 190 attach_extra_attrs(the_page.object_list) |
178 'page': the_page, | 191 |
179 }, | 192 return render_to_response('news/index.html', { |
180 context_instance=RequestContext(request)) | 193 'title': 'Stories with tag: "%s"' % tag_name, |
194 'page': the_page, | |
195 }, | |
196 context_instance=RequestContext(request)) | |
181 | 197 |
182 ####################################################################### | 198 ####################################################################### |
183 | 199 |
184 @login_required | 200 @login_required |
185 def email_story(request, story_id): | 201 def email_story(request, story_id): |
186 story = get_object_or_404(Story, pk=story_id) | 202 story = get_object_or_404(Story, pk=story_id) |
187 if request.method == 'POST': | 203 if request.method == 'POST': |
188 send_form = SendStoryForm(request.POST) | 204 send_form = SendStoryForm(request.POST) |
189 if send_form.is_valid(): | 205 if send_form.is_valid(): |
190 to_name = send_form.name() | 206 to_name = send_form.name() |
191 to_email = send_form.email() | 207 to_email = send_form.email() |
192 from_name = get_full_name(request.user) | 208 from_name = get_full_name(request.user) |
193 from_email = request.user.email | 209 from_email = request.user.email |
194 site = Site.objects.get_current() | 210 site = Site.objects.get_current() |
195 | 211 |
196 msg = render_to_string('news/send_story_email.txt', | 212 msg = render_to_string('news/send_story_email.txt', |
197 { | 213 { |
198 'to_name': to_name, | 214 'to_name': to_name, |
199 'sender_name': from_name, | 215 'sender_name': from_name, |
200 'site_name' : site.name, | 216 'site_name' : site.name, |
201 'site_url' : site.domain, | 217 'site_url' : site.domain, |
202 'story_title': story.title, | 218 'story_title': story.title, |
203 'story_link': story.get_absolute_url(), | 219 'story_link': story.get_absolute_url(), |
204 }) | 220 }) |
205 | 221 |
206 subject = 'Interesting Story at ' + site.name | 222 subject = 'Interesting Story at ' + site.name |
207 send_mail(subject, msg, from_email, [to_email]) | 223 send_mail(subject, msg, from_email, [to_email]) |
208 return HttpResponseRedirect(reverse('news.views.email_thanks')) | 224 return HttpResponseRedirect(reverse('news.views.email_thanks')) |
209 else: | 225 else: |
210 send_form = SendStoryForm() | 226 send_form = SendStoryForm() |
211 | 227 |
212 return render_to_response('news/send_story.html', { | 228 return render_to_response('news/send_story.html', { |
213 'send_form': send_form, | 229 'send_form': send_form, |
214 'story': story, | 230 'story': story, |
215 }, | 231 }, |
216 context_instance = RequestContext(request)) | 232 context_instance = RequestContext(request)) |
217 | 233 |
218 ####################################################################### | 234 ####################################################################### |
219 | 235 |
220 @login_required | 236 @login_required |
221 def email_thanks(request): | 237 def email_thanks(request): |
222 return render_to_response('news/send_story.html', { | 238 return render_to_response('news/send_story.html', { |
223 }, | 239 }, |
224 context_instance = RequestContext(request)) | 240 context_instance = RequestContext(request)) |
225 | 241 |