Mercurial > public > sg101
comparison gpp/news/views.py @ 240:1246a4f1ab4f
For #93: fix url scheme for the news application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 00:14:54 +0000 |
parents | da46e77cd804 |
children | 27bee3ac85e6 |
comparison
equal
deleted
inserted
replaced
239:dcc929973bba | 240:1246a4f1ab4f |
---|---|
25 from core.paginator import DiggPaginator | 25 from core.paginator import DiggPaginator |
26 from news.models import Category | 26 from news.models import Category |
27 from news.models import PendingStory | 27 from news.models import PendingStory |
28 from news.models import Story | 28 from news.models import Story |
29 from news.forms import AddNewsForm | 29 from news.forms import AddNewsForm |
30 from news.forms import SearchNewsForm | |
31 from news.forms import SendStoryForm | 30 from news.forms import SendStoryForm |
32 | 31 |
33 NEWS_PER_PAGE = 5 | 32 NEWS_PER_PAGE = 5 |
34 | 33 |
35 ####################################################################### | 34 ####################################################################### |
37 def create_paginator(stories): | 36 def create_paginator(stories): |
38 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) | 37 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) |
39 | 38 |
40 ####################################################################### | 39 ####################################################################### |
41 | 40 |
42 def index(request, page=1): | 41 def _get_page(qdict): |
42 """Attempts to retrieve the value for "page" from the given query dict and | |
43 return it as an integer. If the key cannot be found or converted to an | |
44 integer, 1 is returned. | |
45 """ | |
46 n = qdict.get('page', 1) | |
47 try: | |
48 n = int(n) | |
49 except ValueError: | |
50 n = 1 | |
51 return n | |
52 | |
53 ####################################################################### | |
54 | |
55 def index(request): | |
43 stories = Story.objects.all().select_related() | 56 stories = Story.objects.all().select_related() |
44 paginator = create_paginator(stories) | 57 paginator = create_paginator(stories) |
45 try: | 58 |
46 the_page = paginator.page(int(page)) | 59 page = _get_page(request.GET) |
60 try: | |
61 the_page = paginator.page(page) | |
47 except InvalidPage: | 62 except InvalidPage: |
48 raise Http404 | 63 raise Http404 |
49 | 64 |
50 return render_to_response('news/index.html', { | 65 return render_to_response('news/index.html', { |
51 'title': 'Main Index', | 66 'title': 'Main Index', |
52 'page': the_page, | 67 'page': the_page, |
53 'search_form': SearchNewsForm(), | |
54 }, | 68 }, |
55 context_instance = RequestContext(request)) | 69 context_instance = RequestContext(request)) |
56 | 70 |
57 ####################################################################### | 71 ####################################################################### |
58 | 72 |
59 def archive_index(request): | 73 def archive_index(request): |
60 dates = Story.objects.dates('date_submitted', 'month', order='DESC') | 74 dates = Story.objects.dates('date_submitted', 'month', order='DESC') |
61 return render_to_response('news/archive_index.html', { | 75 return render_to_response('news/archive_index.html', { |
62 'title': 'News Archive', | 76 'title': 'News Archive', |
63 'dates': dates, | 77 'dates': dates, |
64 'search_form': SearchNewsForm(), | 78 }, |
65 }, | 79 context_instance = RequestContext(request)) |
66 context_instance = RequestContext(request)) | 80 |
67 | 81 ####################################################################### |
68 ####################################################################### | 82 |
69 | 83 def archive(request, year, month): |
70 def archive(request, year, month, page=1): | |
71 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) | 84 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) |
72 paginator = create_paginator(stories) | 85 paginator = create_paginator(stories) |
73 try: | 86 page = _get_page(request.GET) |
74 the_page = paginator.page(int(page)) | 87 try: |
88 the_page = paginator.page(page) | |
75 except InvalidPage: | 89 except InvalidPage: |
76 raise Http404 | 90 raise Http404 |
77 | 91 |
78 month_name = datetime.date(int(year), int(month), 1).strftime('%B') | 92 month_name = datetime.date(int(year), int(month), 1).strftime('%B') |
79 | 93 |
80 return render_to_response('news/index.html', { | 94 return render_to_response('news/index.html', { |
81 'title': 'Archive for %s, %s' % (month_name, year), | 95 'title': 'Archive for %s, %s' % (month_name, year), |
82 'page': the_page, | 96 'page': the_page, |
83 'search_form': SearchNewsForm(), | |
84 }, | 97 }, |
85 context_instance = RequestContext(request)) | 98 context_instance = RequestContext(request)) |
86 | 99 |
87 ####################################################################### | 100 ####################################################################### |
88 | 101 |
92 for cat in categories: | 105 for cat in categories: |
93 cat_list.append((cat, cat.story_set.defer('tags')[:10])) | 106 cat_list.append((cat, cat.story_set.defer('tags')[:10])) |
94 | 107 |
95 return render_to_response('news/category_index.html', { | 108 return render_to_response('news/category_index.html', { |
96 'cat_list': cat_list, | 109 'cat_list': cat_list, |
97 'search_form': SearchNewsForm(), | 110 }, |
98 }, | 111 context_instance = RequestContext(request)) |
99 context_instance = RequestContext(request)) | 112 |
100 | 113 ####################################################################### |
101 ####################################################################### | 114 |
102 | 115 def category(request, slug): |
103 def category(request, category, page=1): | 116 category = get_object_or_404(Category, slug=slug) |
104 category = get_object_or_404(Category, pk=category) | |
105 stories = Story.objects.filter(category=category) | 117 stories = Story.objects.filter(category=category) |
106 paginator = create_paginator(stories) | 118 paginator = create_paginator(stories) |
107 try: | 119 page = _get_page(request.GET) |
108 the_page = paginator.page(int(page)) | 120 try: |
121 the_page = paginator.page(page) | |
109 except InvalidPage: | 122 except InvalidPage: |
110 raise Http404 | 123 raise Http404 |
111 | 124 |
112 return render_to_response('news/index.html', { | 125 return render_to_response('news/index.html', { |
113 'title': 'Category: ' + category.title, | 126 'title': 'Category: ' + category.title, |
114 'page': the_page, | 127 'page': the_page, |
115 'search_form': SearchNewsForm(), | |
116 }, | |
117 context_instance = RequestContext(request)) | |
118 | |
119 ####################################################################### | |
120 | |
121 def search(request, page=1): | |
122 if request.method == 'POST': | |
123 form = SearchNewsForm(request.POST) | |
124 if form.is_valid(): | |
125 query_text = form.get_query() | |
126 category = form.get_category() | |
127 page = 1 | |
128 else: | |
129 return HttpResponseRedirect(reverse('news.views.index')) | |
130 else: | |
131 if 'query' in request.GET: | |
132 query_text = request.GET['query'] | |
133 category = request.GET.get('category', None) | |
134 else: | |
135 return HttpResponseRedirect(reverse('news.views.index')) | |
136 | |
137 if category is not None: | |
138 stories = Story.objects.filter(category=category) | |
139 cat_qual = ' in category "%s"' % category.title | |
140 else: | |
141 stories = Story.objects.all() | |
142 cat_qual = '' | |
143 | |
144 stories = stories.filter( | |
145 Q(title__icontains=query_text) | | |
146 Q(short_text__icontains=query_text) | | |
147 Q(long_text__icontains=query_text)).order_by('-date_submitted') | |
148 | |
149 paginator = create_paginator(stories) | |
150 try: | |
151 the_page = paginator.page(int(page)) | |
152 except InvalidPage: | |
153 raise Http404 | |
154 | |
155 return render_to_response('news/index.html', { | |
156 'title': 'Search Results for "%s"%s' % (query_text, cat_qual), | |
157 'query': query_text, | |
158 'page': the_page, | |
159 'search_form': SearchNewsForm(), | |
160 }, | 128 }, |
161 context_instance = RequestContext(request)) | 129 context_instance = RequestContext(request)) |
162 | 130 |
163 ####################################################################### | 131 ####################################################################### |
164 | 132 |
165 def story(request, story_id): | 133 def story(request, story_id): |
166 story = get_object_or_404(Story, pk=story_id) | 134 story = get_object_or_404(Story, pk=story_id) |
167 return render_to_response('news/story.html', { | 135 return render_to_response('news/story.html', { |
168 'story': story, | 136 'story': story, |
169 'search_form': SearchNewsForm(), | |
170 }, | 137 }, |
171 context_instance=RequestContext(request)) | 138 context_instance=RequestContext(request)) |
172 | 139 |
173 ####################################################################### | 140 ####################################################################### |
174 | 141 |
186 else: | 153 else: |
187 add_form = AddNewsForm() | 154 add_form = AddNewsForm() |
188 | 155 |
189 return render_to_response('news/submit_news.html', { | 156 return render_to_response('news/submit_news.html', { |
190 'add_form': add_form, | 157 'add_form': add_form, |
191 'search_form': SearchNewsForm(), | |
192 }, | 158 }, |
193 context_instance = RequestContext(request)) | 159 context_instance = RequestContext(request)) |
194 | 160 |
195 ####################################################################### | 161 ####################################################################### |
196 | 162 |
197 @login_required | 163 @login_required |
198 def submit_thanks(request): | 164 def submit_thanks(request): |
199 return render_to_response('news/submit_news.html', { | 165 return render_to_response('news/submit_news.html', { |
200 'search_form': SearchNewsForm(), | |
201 }, | 166 }, |
202 context_instance = RequestContext(request)) | 167 context_instance = RequestContext(request)) |
203 | 168 |
204 ####################################################################### | 169 ####################################################################### |
205 | 170 |
206 def tags(request): | 171 def tags(request): |
207 tags = Tag.objects.cloud_for_model(Story) | 172 tags = Tag.objects.cloud_for_model(Story) |
208 return render_to_response('news/tag_index.html', { | 173 return render_to_response('news/tag_index.html', { |
209 'tags': tags, | 174 'tags': tags, |
210 'search_form': SearchNewsForm(), | 175 }, |
211 }, | 176 context_instance = RequestContext(request)) |
212 context_instance = RequestContext(request)) | 177 |
213 | 178 ####################################################################### |
214 ####################################################################### | 179 |
215 | 180 def tag(request, tag_name): |
216 def tag(request, tag_name, page=1): | |
217 tag = get_object_or_404(Tag, name=tag_name) | 181 tag = get_object_or_404(Tag, name=tag_name) |
218 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) | 182 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) |
219 paginator = create_paginator(stories) | 183 paginator = create_paginator(stories) |
220 try: | 184 page = _get_page(request.GET) |
221 the_page = paginator.page(int(page)) | 185 try: |
186 the_page = paginator.page(page) | |
222 except InvalidPage: | 187 except InvalidPage: |
223 raise Http404 | 188 raise Http404 |
224 | 189 |
225 return render_to_response('news/index.html', { | 190 return render_to_response('news/index.html', { |
226 'title': 'Stories with tag: "%s"' % tag_name, | 191 'title': 'Stories with tag: "%s"' % tag_name, |
227 'page': the_page, | 192 'page': the_page, |
228 'search_form': SearchNewsForm(), | |
229 }, | 193 }, |
230 context_instance=RequestContext(request)) | 194 context_instance=RequestContext(request)) |
231 | 195 |
232 ####################################################################### | 196 ####################################################################### |
233 | 197 |