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