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