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