Mercurial > public > sg101
comparison gpp/weblinks/views.py @ 242:7e8d2dda99e3
For #93: fix url scheme for weblinks.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 03:24:01 +0000 |
parents | 2022c0409296 |
children | 75ea1a8be7f2 |
comparison
equal
deleted
inserted
replaced
241:27bee3ac85e6 | 242:7e8d2dda99e3 |
---|---|
18 from django.http import Http404 | 18 from django.http import Http404 |
19 from django.views.decorators.http import require_POST | 19 from django.views.decorators.http import require_POST |
20 | 20 |
21 from core.paginator import DiggPaginator | 21 from core.paginator import DiggPaginator |
22 from core.functions import email_admins | 22 from core.functions import email_admins |
23 from core.functions import get_page | |
23 from weblinks.models import Category | 24 from weblinks.models import Category |
24 from weblinks.models import Link | 25 from weblinks.models import Link |
25 from weblinks.models import FlaggedLink | 26 from weblinks.models import FlaggedLink |
26 from weblinks.forms import SearchForm | |
27 from weblinks.forms import AddLinkForm | 27 from weblinks.forms import AddLinkForm |
28 | 28 |
29 ####################################################################### | 29 ####################################################################### |
30 | 30 |
31 LINKS_PER_PAGE = 10 | 31 LINKS_PER_PAGE = 10 |
45 context_instance = RequestContext(request)) | 45 context_instance = RequestContext(request)) |
46 | 46 |
47 ####################################################################### | 47 ####################################################################### |
48 | 48 |
49 def new_links(request): | 49 def new_links(request): |
50 links = Link.public_objects.order_by('-date_added')[:LINKS_PER_PAGE] | 50 links = Link.public_objects.order_by('-date_added') |
51 paginator = create_paginator(links) | |
52 page = get_page(request.GET) | |
53 try: | |
54 the_page = paginator.page(page) | |
55 except InvalidPage: | |
56 raise Http404 | |
57 | |
51 return render_to_response('weblinks/link_summary.html', { | 58 return render_to_response('weblinks/link_summary.html', { |
52 'links': links, | 59 'page': the_page, |
53 'title': 'Newest Links', | 60 'title': 'Newest Links', |
54 }, | 61 }, |
55 context_instance = RequestContext(request)) | 62 context_instance = RequestContext(request)) |
56 | 63 |
57 ####################################################################### | 64 ####################################################################### |
58 | 65 |
59 def popular_links(request): | 66 def popular_links(request): |
60 links = Link.public_objects.order_by('-hits')[:LINKS_PER_PAGE] | 67 links = Link.public_objects.order_by('-hits') |
68 paginator = create_paginator(links) | |
69 page = get_page(request.GET) | |
70 try: | |
71 the_page = paginator.page(page) | |
72 except InvalidPage: | |
73 raise Http404 | |
61 return render_to_response('weblinks/link_summary.html', { | 74 return render_to_response('weblinks/link_summary.html', { |
62 'links': links, | 75 'page': the_page, |
63 'title': 'Popular Links', | 76 'title': 'Popular Links', |
64 }, | 77 }, |
65 context_instance = RequestContext(request)) | 78 context_instance = RequestContext(request)) |
66 | 79 |
67 ####################################################################### | 80 ####################################################################### |
76 new_link.save() | 89 new_link.save() |
77 email_admins('New link for approval', """Hello, | 90 email_admins('New link for approval', """Hello, |
78 | 91 |
79 A user has added a new link for your approval. | 92 A user has added a new link for your approval. |
80 """) | 93 """) |
81 return HttpResponseRedirect(reverse('weblinks.views.add_thanks')) | 94 return HttpResponseRedirect(reverse('weblinks-add_thanks')) |
82 else: | 95 else: |
83 add_form = AddLinkForm() | 96 add_form = AddLinkForm() |
84 | 97 |
85 return render_to_response('weblinks/add_link.html', { | 98 return render_to_response('weblinks/add_link.html', { |
86 'add_form': add_form, | 99 'add_form': add_form, |
103 'title': 'title', | 116 'title': 'title', |
104 'date': '-date_added', | 117 'date': '-date_added', |
105 'hits': '-hits' | 118 'hits': '-hits' |
106 } | 119 } |
107 | 120 |
108 def view_links(request, category, sort='title', page='1'): | 121 def view_links(request, slug, sort='title'): |
109 try: | 122 try: |
110 cat = Category.objects.get(pk=category) | 123 cat = Category.objects.get(slug=slug) |
111 except Category.DoesNotExist: | 124 except Category.DoesNotExist: |
112 raise Http404 | 125 raise Http404 |
113 | 126 |
114 if sort in LINK_FIELD_MAP: | 127 if sort in LINK_FIELD_MAP: |
115 order_by = LINK_FIELD_MAP[sort] | 128 order_by = LINK_FIELD_MAP[sort] |
116 else: | 129 else: |
117 sort = 'title' | 130 sort = 'title' |
118 order_by = LINK_FIELD_MAP['title'] | 131 order_by = LINK_FIELD_MAP['title'] |
119 | 132 |
120 links = Link.public_objects.filter(category = category).order_by(order_by) | 133 links = Link.public_objects.filter(category=cat).order_by(order_by) |
121 paginator = create_paginator(links) | 134 paginator = create_paginator(links) |
122 try: | 135 page = get_page(request.GET) |
123 the_page = paginator.page(int(page)) | 136 try: |
137 the_page = paginator.page(page) | |
124 except InvalidPage: | 138 except InvalidPage: |
125 raise Http404 | 139 raise Http404 |
126 | 140 |
127 return render_to_response('weblinks/view_links.html', { | 141 return render_to_response('weblinks/view_links.html', { |
128 's' : sort, | 142 's' : sort, |
176 "link shortly. Thanks for helping to improve the content on " \ | 190 "link shortly. Thanks for helping to improve the content on " \ |
177 "this site.") | 191 "this site.") |
178 | 192 |
179 ####################################################################### | 193 ####################################################################### |
180 | 194 |
181 def search_links(request, page=1): | |
182 if request.method == 'POST': | |
183 form = SearchForm(request.POST) | |
184 if form.is_valid(): | |
185 query_text = form.query() | |
186 page = 1 | |
187 else: | |
188 return HttpResponseRedirect(reverse('weblinks.views.link_index')) | |
189 else: | |
190 if 'query' in request.GET: | |
191 query_text = request.GET['query'] | |
192 else: | |
193 return HttpResponseRedirect(reverse('weblinks.views.link_index')) | |
194 | |
195 links = Link.public_objects.filter( | |
196 Q(title__icontains = query_text) | | |
197 Q(description__icontains = query_text)).order_by('title').select_related() | |
198 paginator = create_paginator(links) | |
199 try: | |
200 the_page = paginator.page(int(page)) | |
201 except EmptyPage: | |
202 links = Link.public_objects.none() | |
203 except InvalidPage: | |
204 raise Http404 | |
205 | |
206 return render_to_response('weblinks/search_results.html', { | |
207 'query': query_text, | |
208 'page': the_page, | |
209 }, | |
210 context_instance = RequestContext(request)) | |
211 | |
212 ####################################################################### | |
213 | |
214 def link_detail(request, id): | 195 def link_detail(request, id): |
215 link = get_object_or_404(Link, pk=id) | 196 link = get_object_or_404(Link, pk=id) |
216 return render_to_response('weblinks/link_detail.html', { | 197 return render_to_response('weblinks/link_detail.html', { |
217 'link': link, | 198 'link': link, |
218 }, | 199 }, |