Mercurial > public > sg101
comparison gpp/news/views.py @ 241:27bee3ac85e6
For #93: fix url scheme for downloads.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 01:01:40 +0000 |
parents | 1246a4f1ab4f |
children | 75ea1a8be7f2 |
comparison
equal
deleted
inserted
replaced
240:1246a4f1ab4f | 241:27bee3ac85e6 |
---|---|
20 from tagging.models import TaggedItem | 20 from tagging.models import TaggedItem |
21 | 21 |
22 from core.html import clean_html | 22 from core.html import clean_html |
23 from core.functions import send_mail | 23 from core.functions import send_mail |
24 from core.functions import get_full_name | 24 from core.functions import get_full_name |
25 from core.functions import get_page | |
25 from core.paginator import DiggPaginator | 26 from core.paginator import DiggPaginator |
26 from news.models import Category | 27 from news.models import Category |
27 from news.models import PendingStory | 28 from news.models import PendingStory |
28 from news.models import Story | 29 from news.models import Story |
29 from news.forms import AddNewsForm | 30 from news.forms import AddNewsForm |
36 def create_paginator(stories): | 37 def create_paginator(stories): |
37 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) | 38 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) |
38 | 39 |
39 ####################################################################### | 40 ####################################################################### |
40 | 41 |
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): | 42 def index(request): |
56 stories = Story.objects.all().select_related() | 43 stories = Story.objects.all().select_related() |
57 paginator = create_paginator(stories) | 44 paginator = create_paginator(stories) |
58 | 45 |
59 page = _get_page(request.GET) | 46 page = get_page(request.GET) |
60 try: | 47 try: |
61 the_page = paginator.page(page) | 48 the_page = paginator.page(page) |
62 except InvalidPage: | 49 except InvalidPage: |
63 raise Http404 | 50 raise Http404 |
64 | 51 |
81 ####################################################################### | 68 ####################################################################### |
82 | 69 |
83 def archive(request, year, month): | 70 def archive(request, year, month): |
84 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) | 71 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) |
85 paginator = create_paginator(stories) | 72 paginator = create_paginator(stories) |
86 page = _get_page(request.GET) | 73 page = get_page(request.GET) |
87 try: | 74 try: |
88 the_page = paginator.page(page) | 75 the_page = paginator.page(page) |
89 except InvalidPage: | 76 except InvalidPage: |
90 raise Http404 | 77 raise Http404 |
91 | 78 |
114 | 101 |
115 def category(request, slug): | 102 def category(request, slug): |
116 category = get_object_or_404(Category, slug=slug) | 103 category = get_object_or_404(Category, slug=slug) |
117 stories = Story.objects.filter(category=category) | 104 stories = Story.objects.filter(category=category) |
118 paginator = create_paginator(stories) | 105 paginator = create_paginator(stories) |
119 page = _get_page(request.GET) | 106 page = get_page(request.GET) |
120 try: | 107 try: |
121 the_page = paginator.page(page) | 108 the_page = paginator.page(page) |
122 except InvalidPage: | 109 except InvalidPage: |
123 raise Http404 | 110 raise Http404 |
124 | 111 |
179 | 166 |
180 def tag(request, tag_name): | 167 def tag(request, tag_name): |
181 tag = get_object_or_404(Tag, name=tag_name) | 168 tag = get_object_or_404(Tag, name=tag_name) |
182 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) | 169 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) |
183 paginator = create_paginator(stories) | 170 paginator = create_paginator(stories) |
184 page = _get_page(request.GET) | 171 page = get_page(request.GET) |
185 try: | 172 try: |
186 the_page = paginator.page(page) | 173 the_page = paginator.page(page) |
187 except InvalidPage: | 174 except InvalidPage: |
188 raise Http404 | 175 raise Http404 |
189 | 176 |