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