bugfixing, refactoring directory
[mygpo.git] / mygpo / web / views / public.py
blob9bdfe1b3a2423854f0b882c6db963ee5c86bf349
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.shortcuts import render_to_response
19 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, Http404, HttpResponseForbidden
20 from django.template import RequestContext
21 from mygpo.api.models import Podcast, Episode, Subscription
22 from mygpo.data.models import PodcastTag, DirectoryEntry
23 from mygpo.decorators import manual_gc
24 from mygpo.web import utils
25 from mygpo import settings
26 from django.shortcuts import get_object_or_404
27 from django.db.models import Sum
28 from django.contrib.sites.models import Site
29 from django.core.paginator import Paginator, InvalidPage, EmptyPage
32 @manual_gc
33 def browse(request, num_categories=10, num_tags_cloud=90, podcasts_per_category=10):
34 total = int(num_categories) + int(num_tags_cloud)
35 top_tags = DirectoryEntry.objects.top_tags(total)
37 categories = []
38 for tag in top_tags[:num_categories]:
39 entries = DirectoryEntry.objects.podcasts_for_tag(tag.tag)[:podcasts_per_category]
40 categories.append({
41 'tag': tag.tag,
42 'entries': entries
45 tag_cloud = top_tags[num_categories:]
47 tag_cloud.sort(key = lambda x: x.tag.lower())
48 max_entries = max([t.entries for t in tag_cloud])
50 return render_to_response('directory.html', {
51 'categories': categories,
52 'tag_cloud': tag_cloud,
53 'max_entries': max_entries,
54 }, context_instance=RequestContext(request))
57 @manual_gc
58 def category(request, category, page_size=20):
59 entries = DirectoryEntry.objects.podcasts_for_tag(category)
61 paginator = Paginator(entries, page_size)
63 # Make sure page request is an int. If not, deliver first page.
64 try:
65 page = int(request.GET.get('page', '1'))
66 except ValueError:
67 page = 1
69 # If page request (9999) is out of range, deliver last page of results.
70 try:
71 podcasts = paginator.page(page)
72 except (EmptyPage, InvalidPage):
73 podcasts = paginator.page(paginator.num_pages)
75 page_list = utils.get_page_list(1, podcasts.paginator.num_pages, podcasts.number, 15)
77 return render_to_response('category.html', {
78 'entries': podcasts,
79 'category': category,
80 'page_list': page_list,
81 }, context_instance=RequestContext(request))