remove unnecessary imports
[mygpo.git] / mygpo / web / views / public.py
blobf33f772f436cdd2af6c9e418faf7c33427b192cb
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
20 from django.template import RequestContext
21 from mygpo.api import backend
22 from mygpo.data.models import DirectoryEntry
23 from mygpo.data.mimetype import CONTENT_TYPES
24 from mygpo.decorators import manual_gc
25 from mygpo.web import utils
26 from django.contrib.sites.models import Site
27 from django.core.paginator import Paginator, InvalidPage, EmptyPage
30 @manual_gc
31 def browse(request, num_categories=10, num_tags_cloud=90, podcasts_per_category=10):
32 total = int(num_categories) + int(num_tags_cloud)
33 top_tags = DirectoryEntry.objects.top_tags(total)
35 categories = []
36 for tag in top_tags[:num_categories]:
37 entries = DirectoryEntry.objects.podcasts_for_tag(tag.tag)[:podcasts_per_category]
38 categories.append({
39 'tag': tag.tag,
40 'entries': entries
43 tag_cloud = top_tags[num_categories:]
45 tag_cloud.sort(key = lambda x: x.tag.lower())
46 max_entries = max([t.entries for t in tag_cloud])
48 return render_to_response('directory.html', {
49 'categories': categories,
50 'tag_cloud': tag_cloud,
51 'max_entries': max_entries,
52 }, context_instance=RequestContext(request))
55 @manual_gc
56 def category(request, category, page_size=20):
57 entries = DirectoryEntry.objects.podcasts_for_tag(category)
59 paginator = Paginator(entries, page_size)
61 # Make sure page request is an int. If not, deliver first page.
62 try:
63 page = int(request.GET.get('page', '1'))
64 except ValueError:
65 page = 1
67 # If page request (9999) is out of range, deliver last page of results.
68 try:
69 podcasts = paginator.page(page)
70 except (EmptyPage, InvalidPage):
71 podcasts = paginator.page(paginator.num_pages)
73 page_list = utils.get_page_list(1, podcasts.paginator.num_pages, podcasts.number, 15)
75 return render_to_response('category.html', {
76 'entries': podcasts,
77 'category': category,
78 'page_list': page_list,
79 }, context_instance=RequestContext(request))
82 @manual_gc
83 def toplist(request, num=100, lang=None):
85 try:
86 lang = utils.process_lang_params(request, '/toplist/')
87 except utils.UpdatedException, updated:
88 return HttpResponseRedirect('/toplist/?lang=%s' % ','.join(updated.data))
90 type_str = request.GET.get('types', '')
91 set_types = [t for t in type_str.split(',') if t]
92 if set_types:
93 media_types = dict([(t, t in set_types) for t in CONTENT_TYPES])
94 else:
95 media_types = dict([(t, True) for t in CONTENT_TYPES])
97 entries = backend.get_toplist(num, lang, set_types)
99 max_subscribers = max([e.subscriptions for e in entries]) if entries else 0
100 current_site = Site.objects.get_current()
101 all_langs = utils.get_language_names(utils.get_podcast_languages())
103 return render_to_response('toplist.html', {
104 'entries': entries,
105 'max_subscribers': max_subscribers,
106 'url': current_site,
107 'languages': lang,
108 'all_languages': all_langs,
109 'types': media_types,
110 }, context_instance=RequestContext(request))
113 @manual_gc
114 def episode_toplist(request, num=100):
116 try:
117 lang = utils.process_lang_params(request, '/toplist/episodes')
118 except utils.UpdatedException, updated:
119 return HttpResponseRedirect('/toplist/episodes?lang=%s' % ','.join(updated.data))
121 type_str = request.GET.get('types', '')
122 set_types = [t for t in type_str.split(',') if t]
123 if set_types:
124 media_types = dict([(t, t in set_types) for t in CONTENT_TYPES])
125 else:
126 media_types = dict([(t, True) for t in CONTENT_TYPES])
128 entries = backend.get_episode_toplist(num, lang, set_types)
130 current_site = Site.objects.get_current()
132 # Determine maximum listener amount (or 0 if no entries exist)
133 max_listeners = max([0]+[e.listeners for e in entries])
134 all_langs = utils.get_language_names(utils.get_podcast_languages())
135 return render_to_response('episode_toplist.html', {
136 'entries': entries,
137 'max_listeners': max_listeners,
138 'url': current_site,
139 'languages': lang,
140 'all_languages': all_langs,
141 'types': media_types,
142 }, context_instance=RequestContext(request))
145 def gpodder_example_podcasts(request):
146 sponsored_podcast = utils.get_sponsored_podcast()
147 return render_to_response('gpodder_examples.opml', {
148 'sponsored_podcast': sponsored_podcast
149 }, context_instance=RequestContext(request))