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