comparison news/views.py @ 1028:5ba2508939f7

Django 1.8 changes; first batch.
author Brian Neal <bgneal@gmail.com>
date Tue, 15 Dec 2015 21:01:07 -0600
parents c6c3ba5cf6eb
children e932f2ecd4a7
comparison
equal deleted inserted replaced
1027:cd4db27c90e3 1028:5ba2508939f7
2 Views for the News application. 2 Views for the News application.
3 """ 3 """
4 4
5 import datetime 5 import datetime
6 from django.conf import settings 6 from django.conf import settings
7 from django.shortcuts import render_to_response
8 from django.template import RequestContext 7 from django.template import RequestContext
9 from django.template.loader import render_to_string 8 from django.template.loader import render_to_string
10 from django.http import HttpResponseRedirect 9 from django.http import HttpResponseRedirect
11 from django.contrib.auth.decorators import login_required 10 from django.contrib.auth.decorators import login_required
12 from django.shortcuts import get_object_or_404 11 from django.shortcuts import get_object_or_404
12 from django.shortcuts import render
13 from django.core.paginator import InvalidPage 13 from django.core.paginator import InvalidPage
14 from django.core.urlresolvers import reverse 14 from django.core.urlresolvers import reverse
15 from django.contrib.sites.models import Site 15 from django.contrib.sites.models import Site
16 from django.http import Http404 16 from django.http import Http404
17 17
18 from tagging.models import Tag 18 from tagging.models import Tag
19 from tagging.models import TaggedItem 19 from tagging.models import TaggedItem
20 20
21 from core.html import clean_html
22 from core.functions import send_mail 21 from core.functions import send_mail
23 from core.functions import get_full_name 22 from core.functions import get_full_name
24 from core.functions import get_page 23 from core.functions import get_page
25 from core.paginator import DiggPaginator 24 from core.paginator import DiggPaginator
26 from news.models import Category 25 from news.models import Category
54 53
55 # Go get the tags and comment counts for all these stories in bulk rather 54 # Go get the tags and comment counts for all these stories in bulk rather
56 # than one at a time in the template; this saves database queries 55 # than one at a time in the template; this saves database queries
57 attach_extra_attrs(the_page.object_list) 56 attach_extra_attrs(the_page.object_list)
58 57
59 return render_to_response('news/index.html', { 58 return render(request, 'news/index.html', {
60 'title': 'Main Index', 59 'title': 'Main Index',
61 'page': the_page, 60 'page': the_page,
62 }, 61 })
63 context_instance = RequestContext(request))
64 62
65 ####################################################################### 63 #######################################################################
66 64
67 def archive_index(request): 65 def archive_index(request):
68 dates = Story.objects.dates('date_submitted', 'month', order='DESC') 66 dates = Story.objects.dates('date_submitted', 'month', order='DESC')
69 return render_to_response('news/archive_index.html', { 67 return render(request, 'news/archive_index.html', {
70 'title': 'News Archive', 68 'title': 'News Archive',
71 'dates': dates, 69 'dates': dates,
72 }, 70 })
73 context_instance = RequestContext(request))
74 71
75 ####################################################################### 72 #######################################################################
76 73
77 def archive(request, year, month): 74 def archive(request, year, month):
78 stories = Story.objects.defer('tags').filter(date_submitted__year=year, 75 stories = Story.objects.defer('tags').filter(date_submitted__year=year,
87 84
88 attach_extra_attrs(the_page.object_list) 85 attach_extra_attrs(the_page.object_list)
89 86
90 month_name = datetime.date(int(year), int(month), 1).strftime('%B') 87 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
91 88
92 return render_to_response('news/index.html', { 89 return render(request, 'news/index.html', {
93 'title': 'Archive for %s, %s' % (month_name, year), 90 'title': 'Archive for %s, %s' % (month_name, year),
94 'page': the_page, 91 'page': the_page,
95 }, 92 })
96 context_instance = RequestContext(request))
97 93
98 ####################################################################### 94 #######################################################################
99 95
100 def category_index(request): 96 def category_index(request):
101 categories = Category.objects.all().select_related() 97 categories = Category.objects.all().select_related()
102 cat_list = [] 98 cat_list = []
103 for cat in categories: 99 for cat in categories:
104 cat_list.append((cat, cat.story_set.defer('tags')[:10])) 100 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
105 101
106 return render_to_response('news/category_index.html', { 102 return render(request, 'news/category_index.html', {
107 'cat_list': cat_list, 103 'cat_list': cat_list,
108 }, 104 })
109 context_instance = RequestContext(request))
110 105
111 ####################################################################### 106 #######################################################################
112 107
113 def category(request, slug): 108 def category(request, slug):
114 category = get_object_or_404(Category, slug=slug) 109 category = get_object_or_404(Category, slug=slug)
121 except InvalidPage: 116 except InvalidPage:
122 raise Http404 117 raise Http404
123 118
124 attach_extra_attrs(the_page.object_list) 119 attach_extra_attrs(the_page.object_list)
125 120
126 return render_to_response('news/index.html', { 121 return render(request, 'news/index.html', {
127 'title': 'Category: ' + category.title, 122 'title': 'Category: ' + category.title,
128 'page': the_page, 123 'page': the_page,
129 }, 124 })
130 context_instance = RequestContext(request))
131 125
132 ####################################################################### 126 #######################################################################
133 127
134 def story(request, story_id): 128 def story(request, story_id):
135 story = get_object_or_404(Story.objects.select_related( 129 story = get_object_or_404(Story.objects.select_related(
136 'submitter', 'category', 'forums_topic'), pk=story_id) 130 'submitter', 'category', 'forums_topic'), pk=story_id)
137 return render_to_response('news/story.html', { 131 return render(request, 'news/story.html', {
138 'story': story, 132 'story': story,
139 }, 133 })
140 context_instance=RequestContext(request))
141 134
142 ####################################################################### 135 #######################################################################
143 136
144 @login_required 137 @login_required
145 def submit(request): 138 def submit(request):
149 add_form.save(request.user) 142 add_form.save(request.user)
150 return HttpResponseRedirect(reverse('news-submit_thanks')) 143 return HttpResponseRedirect(reverse('news-submit_thanks'))
151 else: 144 else:
152 add_form = AddNewsForm() 145 add_form = AddNewsForm()
153 146
154 return render_to_response('news/submit_news.html', { 147 return render(request, 'news/submit_news.html', {
155 'add_form': add_form, 148 'add_form': add_form,
156 }, 149 })
157 context_instance = RequestContext(request))
158 150
159 ####################################################################### 151 #######################################################################
160 152
161 @login_required 153 @login_required
162 def submit_thanks(request): 154 def submit_thanks(request):
163 return render_to_response('news/submit_news.html', { 155 return render(request, 'news/submit_news.html')
164 },
165 context_instance = RequestContext(request))
166 156
167 ####################################################################### 157 #######################################################################
168 158
169 def tags(request): 159 def tags(request):
170 tags = Tag.objects.cloud_for_model(Story) 160 tags = Tag.objects.cloud_for_model(Story)
171 return render_to_response('news/tag_index.html', { 161 return render(request, 'news/tag_index.html', {
172 'tags': tags, 162 'tags': tags,
173 }, 163 })
174 context_instance = RequestContext(request))
175 164
176 ####################################################################### 165 #######################################################################
177 166
178 def tag(request, tag_name): 167 def tag(request, tag_name):
179 tag = get_object_or_404(Tag, name=tag_name) 168 tag = get_object_or_404(Tag, name=tag_name)
187 except InvalidPage: 176 except InvalidPage:
188 raise Http404 177 raise Http404
189 178
190 attach_extra_attrs(the_page.object_list) 179 attach_extra_attrs(the_page.object_list)
191 180
192 return render_to_response('news/index.html', { 181 return render(request, 'news/index.html', {
193 'title': 'Stories with tag: "%s"' % tag_name, 182 'title': 'Stories with tag: "%s"' % tag_name,
194 'page': the_page, 183 'page': the_page,
195 }, 184 })
196 context_instance=RequestContext(request))
197 185
198 ####################################################################### 186 #######################################################################
199 187
200 @login_required 188 @login_required
201 def email_story(request, story_id): 189 def email_story(request, story_id):
223 send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email) 211 send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email)
224 return HttpResponseRedirect(reverse('news.views.email_thanks')) 212 return HttpResponseRedirect(reverse('news.views.email_thanks'))
225 else: 213 else:
226 send_form = SendStoryForm() 214 send_form = SendStoryForm()
227 215
228 return render_to_response('news/send_story.html', { 216 return render(request, 'news/send_story.html', {
229 'send_form': send_form, 217 'send_form': send_form,
230 'story': story, 218 'story': story,
231 }, 219 })
232 context_instance = RequestContext(request))
233 220
234 ####################################################################### 221 #######################################################################
235 222
236 @login_required 223 @login_required
237 def email_thanks(request): 224 def email_thanks(request):
238 return render_to_response('news/send_story.html', { 225 return render(request, 'news/send_story.html')
239 },
240 context_instance = RequestContext(request))
241
242 #######################################################################
243
244 def _clean_html(s):
245 return clean_html(s, profile='news')