small improvements to tag cloud
[mygpo.git] / mygpo / web / views / public.py
blob6c16c8c50ec064b7f27e5fd04941c31fb69340e4
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, ToplistEntry, Subscription
22 from mygpo.data.models import PodcastTag
23 from mygpo import settings
24 from django.shortcuts import get_object_or_404
25 from django.db.models import Sum
26 from django.contrib.sites.models import Site
27 from django.core.paginator import Paginator, InvalidPage, EmptyPage
30 def browse(request, num_categories=10, num_tags_cloud=90, podcasts_per_category=10):
31 total = int(num_categories) + int(num_tags_cloud)
32 top_tags = PodcastTag.objects.raw("select *, count(id) as entries from podcast_tags group by tag order by entries desc limit %d" % total)
34 top_tags = filter(lambda x: not x.tag.startswith('http://'), top_tags)
36 excluded_tags = getattr(settings, 'DIRECTORY_EXCLUDED_TAGS', [])
37 top_tags = filter(lambda x: not x.tag in excluded_tags, top_tags)
39 categories = []
40 for tag in top_tags[:num_categories]:
41 entries = ToplistEntry.objects.filter(podcast__podcasttag__tag=tag.tag).order_by('-subscriptions').distinct()[:podcasts_per_category]
42 categories.append({
43 'tag': tag.tag,
44 'entries': entries
47 tag_cloud = top_tags[num_categories:]
49 tag_cloud.sort(key = lambda x: x.tag.lower())
50 max_entries = max([t.entries for t in tag_cloud])
52 return render_to_response('directory.html', {
53 'categories': categories,
54 'tag_cloud': tag_cloud,
55 'max_entries': max_entries,
56 }, context_instance=RequestContext(request))
59 def category(request, category, page_size=20):
60 entries = ToplistEntry.objects.filter(podcast__podcasttag__tag=category).order_by('-subscriptions')
62 paginator = Paginator(entries, page_size)
64 # Make sure page request is an int. If not, deliver first page.
65 try:
66 page = int(request.GET.get('page', '1'))
67 except ValueError:
68 page = 1
70 # If page request (9999) is out of range, deliver last page of results.
71 try:
72 podcasts = paginator.page(page)
73 except (EmptyPage, InvalidPage):
74 podcasts = paginator.page(paginator.num_pages)
76 return render_to_response('category.html', {
77 'entries': podcasts,
78 'category': category,
79 }, context_instance=RequestContext(request))