gremmie@1
|
1 """
|
gremmie@1
|
2 Views for the News application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4
|
gremmie@1
|
5 import datetime
|
gremmie@1
|
6 from django.shortcuts import render_to_response
|
gremmie@1
|
7 from django.template import RequestContext
|
gremmie@1
|
8 from django.template.loader import render_to_string
|
gremmie@1
|
9 from django.contrib import auth
|
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
|
gremmie@1
|
13 from django.core.urlresolvers import reverse
|
gremmie@1
|
14 from django.db.models import Q
|
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
|
gremmie@1
|
24 from core.paginator import DiggPaginator
|
gremmie@1
|
25 from news.models import Category
|
gremmie@1
|
26 from news.models import PendingStory
|
gremmie@1
|
27 from news.models import Story
|
gremmie@1
|
28 from news.forms import AddNewsForm
|
gremmie@1
|
29 from news.forms import SearchNewsForm
|
gremmie@1
|
30 from news.forms import SendStoryForm
|
gremmie@1
|
31
|
gremmie@1
|
32 NEWS_PER_PAGE = 2
|
gremmie@1
|
33
|
gremmie@1
|
34 #######################################################################
|
gremmie@1
|
35
|
gremmie@1
|
36 def create_paginator(stories):
|
gremmie@1
|
37 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
|
gremmie@1
|
38
|
gremmie@1
|
39 #######################################################################
|
gremmie@1
|
40
|
gremmie@1
|
41 def index(request, page=1):
|
gremmie@1
|
42 stories = Story.objects.all()
|
gremmie@1
|
43 paginator = create_paginator(stories)
|
gremmie@1
|
44 try:
|
gremmie@1
|
45 the_page = paginator.page(int(page))
|
gremmie@1
|
46 except InvalidPage:
|
gremmie@1
|
47 raise Http404
|
gremmie@1
|
48
|
gremmie@1
|
49 return render_to_response('news/index.html', {
|
gremmie@1
|
50 'title': 'Main Index',
|
gremmie@1
|
51 'page': the_page,
|
gremmie@1
|
52 'search_form': SearchNewsForm(),
|
gremmie@1
|
53 },
|
gremmie@1
|
54 context_instance = RequestContext(request))
|
gremmie@1
|
55
|
gremmie@1
|
56 #######################################################################
|
gremmie@1
|
57
|
gremmie@1
|
58 def archive_index(request):
|
gremmie@1
|
59 dates = Story.objects.dates('date_published', 'month', order='DESC')
|
gremmie@1
|
60 return render_to_response('news/archive_index.html', {
|
gremmie@1
|
61 'title': 'News Archive',
|
gremmie@1
|
62 'dates': dates,
|
gremmie@1
|
63 'search_form': SearchNewsForm(),
|
gremmie@1
|
64 },
|
gremmie@1
|
65 context_instance = RequestContext(request))
|
gremmie@1
|
66
|
gremmie@1
|
67 #######################################################################
|
gremmie@1
|
68
|
gremmie@1
|
69 def archive(request, year, month, page=1):
|
gremmie@1
|
70 stories = Story.objects.filter(date_published__year=year, date_published__month=month)
|
gremmie@1
|
71 paginator = create_paginator(stories)
|
gremmie@1
|
72 try:
|
gremmie@1
|
73 the_page = paginator.page(int(page))
|
gremmie@1
|
74 except InvalidPage:
|
gremmie@1
|
75 raise Http404
|
gremmie@1
|
76
|
gremmie@1
|
77 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
|
gremmie@1
|
78
|
gremmie@1
|
79 return render_to_response('news/index.html', {
|
gremmie@1
|
80 'title': 'Archive for %s, %s' % (month_name, year),
|
gremmie@1
|
81 'page': the_page,
|
gremmie@1
|
82 'search_form': SearchNewsForm(),
|
gremmie@1
|
83 },
|
gremmie@1
|
84 context_instance = RequestContext(request))
|
gremmie@1
|
85
|
gremmie@1
|
86 #######################################################################
|
gremmie@1
|
87
|
gremmie@1
|
88 def category_index(request):
|
gremmie@1
|
89 categories = Category.objects.all()
|
gremmie@1
|
90 cat_list = []
|
gremmie@1
|
91 for cat in categories:
|
gremmie@1
|
92 cat_list.append((cat, cat.story_set.all()[:10]))
|
gremmie@1
|
93
|
gremmie@1
|
94 return render_to_response('news/category_index.html', {
|
gremmie@1
|
95 'cat_list': cat_list,
|
gremmie@1
|
96 'search_form': SearchNewsForm(),
|
gremmie@1
|
97 },
|
gremmie@1
|
98 context_instance = RequestContext(request))
|
gremmie@1
|
99
|
gremmie@1
|
100 #######################################################################
|
gremmie@1
|
101
|
gremmie@1
|
102 def category(request, category, page=1):
|
gremmie@1
|
103 category = get_object_or_404(Category, pk=category)
|
gremmie@1
|
104 stories = Story.objects.filter(category=category)
|
gremmie@1
|
105 paginator = create_paginator(stories)
|
gremmie@1
|
106 try:
|
gremmie@1
|
107 the_page = paginator.page(int(page))
|
gremmie@1
|
108 except InvalidPage:
|
gremmie@1
|
109 raise Http404
|
gremmie@1
|
110
|
gremmie@1
|
111 return render_to_response('news/index.html', {
|
gremmie@1
|
112 'title': 'Category: ' + category.title,
|
gremmie@1
|
113 'page': the_page,
|
gremmie@1
|
114 'search_form': SearchNewsForm(),
|
gremmie@1
|
115 },
|
gremmie@1
|
116 context_instance = RequestContext(request))
|
gremmie@1
|
117
|
gremmie@1
|
118 #######################################################################
|
gremmie@1
|
119
|
gremmie@1
|
120 def search(request, page=1):
|
gremmie@1
|
121 if request.method == 'POST':
|
gremmie@1
|
122 form = SearchNewsForm(request.POST)
|
gremmie@1
|
123 if form.is_valid():
|
gremmie@1
|
124 query_text = form.get_query()
|
gremmie@1
|
125 category = form.get_category()
|
gremmie@1
|
126 page = 1
|
gremmie@1
|
127 else:
|
gremmie@1
|
128 return HttpResponseRedirect(reverse('news.views.index'))
|
gremmie@1
|
129 else:
|
gremmie@1
|
130 if 'query' in request.GET:
|
gremmie@1
|
131 query_text = request.GET['query']
|
gremmie@1
|
132 category = request.GET.get('category', None)
|
gremmie@1
|
133 else:
|
gremmie@1
|
134 return HttpResponseRedirect(reverse('news.views.index'))
|
gremmie@1
|
135
|
gremmie@1
|
136 if category is not None:
|
gremmie@1
|
137 stories = Story.objects.filter(category=category)
|
gremmie@1
|
138 cat_qual = ' in category "%s"' % category.title
|
gremmie@1
|
139 else:
|
gremmie@1
|
140 stories = Story.objects.all()
|
gremmie@1
|
141 cat_qual = ''
|
gremmie@1
|
142
|
gremmie@1
|
143 stories = stories.filter(
|
gremmie@1
|
144 Q(title__icontains=query_text) |
|
gremmie@1
|
145 Q(short_text__icontains=query_text) |
|
gremmie@1
|
146 Q(long_text__icontains=query_text)).order_by('-date_published')
|
gremmie@1
|
147
|
gremmie@1
|
148 paginator = create_paginator(stories)
|
gremmie@1
|
149 try:
|
gremmie@1
|
150 the_page = paginator.page(int(page))
|
gremmie@1
|
151 except InvalidPage:
|
gremmie@1
|
152 raise Http404
|
gremmie@1
|
153
|
gremmie@1
|
154 return render_to_response('news/index.html', {
|
gremmie@1
|
155 'title': 'Search Results for "%s"%s' % (query_text, cat_qual),
|
gremmie@1
|
156 'query': query_text,
|
gremmie@1
|
157 'page': the_page,
|
gremmie@1
|
158 'search_form': SearchNewsForm(),
|
gremmie@1
|
159 },
|
gremmie@1
|
160 context_instance = RequestContext(request))
|
gremmie@1
|
161
|
gremmie@1
|
162 #######################################################################
|
gremmie@1
|
163
|
gremmie@1
|
164 def story(request, story_id):
|
gremmie@1
|
165 story = get_object_or_404(Story, pk=story_id)
|
gremmie@1
|
166 return render_to_response('news/story.html', {
|
gremmie@1
|
167 'story': story,
|
gremmie@1
|
168 'search_form': SearchNewsForm(),
|
gremmie@1
|
169 },
|
gremmie@1
|
170 context_instance=RequestContext(request))
|
gremmie@1
|
171
|
gremmie@1
|
172 #######################################################################
|
gremmie@1
|
173
|
gremmie@1
|
174 @login_required
|
gremmie@1
|
175 def submit(request):
|
gremmie@1
|
176 if request.method == "POST":
|
gremmie@1
|
177 add_form = AddNewsForm(request.POST)
|
gremmie@1
|
178 if add_form.is_valid():
|
gremmie@1
|
179 pending_story = add_form.save(commit=False)
|
gremmie@1
|
180 pending_story.submitter = request.user
|
gremmie@1
|
181 pending_story.short_text = clean_html(pending_story.short_text)
|
gremmie@1
|
182 pending_story.long_text = clean_html(pending_story.long_text)
|
gremmie@1
|
183 pending_story.save()
|
gremmie@1
|
184 return HttpResponseRedirect(reverse('news.views.submit_thanks'))
|
gremmie@1
|
185 else:
|
gremmie@1
|
186 add_form = AddNewsForm()
|
gremmie@1
|
187
|
gremmie@1
|
188 return render_to_response('news/submit_news.html', {
|
gremmie@1
|
189 'add_form': add_form,
|
gremmie@1
|
190 'search_form': SearchNewsForm(),
|
gremmie@1
|
191 },
|
gremmie@1
|
192 context_instance = RequestContext(request))
|
gremmie@1
|
193
|
gremmie@1
|
194 #######################################################################
|
gremmie@1
|
195
|
gremmie@1
|
196 @login_required
|
gremmie@1
|
197 def submit_thanks(request):
|
gremmie@1
|
198 return render_to_response('news/submit_news.html', {
|
gremmie@1
|
199 'search_form': SearchNewsForm(),
|
gremmie@1
|
200 },
|
gremmie@1
|
201 context_instance = RequestContext(request))
|
gremmie@1
|
202
|
gremmie@1
|
203 #######################################################################
|
gremmie@1
|
204
|
gremmie@1
|
205 def tags(request):
|
gremmie@1
|
206 tags = Tag.objects.cloud_for_model(Story)
|
gremmie@1
|
207 return render_to_response('news/tag_index.html', {
|
gremmie@1
|
208 'tags': tags,
|
gremmie@1
|
209 'search_form': SearchNewsForm(),
|
gremmie@1
|
210 },
|
gremmie@1
|
211 context_instance = RequestContext(request))
|
gremmie@1
|
212
|
gremmie@1
|
213 #######################################################################
|
gremmie@1
|
214
|
gremmie@1
|
215 def tag(request, tag_name, page=1):
|
gremmie@1
|
216 tag = get_object_or_404(Tag, name=tag_name)
|
gremmie@1
|
217 stories = TaggedItem.objects.get_by_model(Story, tag)
|
gremmie@1
|
218 paginator = create_paginator(stories)
|
gremmie@1
|
219 try:
|
gremmie@1
|
220 the_page = paginator.page(int(page))
|
gremmie@1
|
221 except InvalidPage:
|
gremmie@1
|
222 raise Http404
|
gremmie@1
|
223
|
gremmie@1
|
224 return render_to_response('news/index.html', {
|
gremmie@1
|
225 'title': 'Stories with tag: "%s"' % tag_name,
|
gremmie@1
|
226 'page': the_page,
|
gremmie@1
|
227 'search_form': SearchNewsForm(),
|
gremmie@1
|
228 },
|
gremmie@1
|
229 context_instance = RequestContext(request))
|
gremmie@1
|
230
|
gremmie@1
|
231 #######################################################################
|
gremmie@1
|
232
|
gremmie@1
|
233 @login_required
|
gremmie@1
|
234 def email_story(request, story_id):
|
gremmie@1
|
235 story = get_object_or_404(Story, pk=story_id)
|
gremmie@1
|
236 if request.method == 'POST':
|
gremmie@1
|
237 send_form = SendStoryForm(request.POST)
|
gremmie@1
|
238 if send_form.is_valid():
|
gremmie@1
|
239 to_name = send_form.name()
|
gremmie@1
|
240 to_email = send_form.email()
|
gremmie@1
|
241 from_name = get_full_name(request.user)
|
gremmie@1
|
242 from_email = request.user.email
|
gremmie@1
|
243 site = Site.objects.get_current()
|
gremmie@1
|
244
|
gremmie@1
|
245 msg = render_to_string('news/send_story_email.txt',
|
gremmie@1
|
246 {
|
gremmie@1
|
247 'to_name': to_name,
|
gremmie@1
|
248 'sender_name': from_name,
|
gremmie@1
|
249 'site_name' : site.name,
|
gremmie@1
|
250 'site_url' : site.domain,
|
gremmie@1
|
251 'story_title': story.title,
|
gremmie@1
|
252 'story_link': story.get_absolute_url(),
|
gremmie@1
|
253 })
|
gremmie@1
|
254
|
gremmie@1
|
255 subject = 'Interesting Story at ' + site.name
|
gremmie@1
|
256 send_mail(subject, msg, from_email, [to_email])
|
gremmie@1
|
257 return HttpResponseRedirect(reverse('news.views.email_thanks'))
|
gremmie@1
|
258 else:
|
gremmie@1
|
259 send_form = SendStoryForm()
|
gremmie@1
|
260
|
gremmie@1
|
261 return render_to_response('news/send_story.html', {
|
gremmie@1
|
262 'send_form': send_form,
|
gremmie@1
|
263 'story': story,
|
gremmie@1
|
264 },
|
gremmie@1
|
265 context_instance = RequestContext(request))
|
gremmie@1
|
266
|
gremmie@1
|
267 #######################################################################
|
gremmie@1
|
268
|
gremmie@1
|
269 @login_required
|
gremmie@1
|
270 def email_thanks(request):
|
gremmie@1
|
271 return render_to_response('news/send_story.html', {
|
gremmie@1
|
272 },
|
gremmie@1
|
273 context_instance = RequestContext(request))
|
gremmie@1
|
274
|