Move mygpo.web.views into single file
[mygpo.git] / mygpo / web / views.py
blobdd6001a443d3ee3bb68693c3cef07a228d8d0cc8
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 import sys
19 from collections import defaultdict
20 from datetime import datetime, timedelta
22 from django.core.urlresolvers import reverse
23 from django.http import HttpResponseRedirect
24 from django.contrib import messages
25 from django.utils.translation import ugettext as _
26 from django.contrib.auth.decorators import login_required
27 from django.shortcuts import render
28 from django.contrib.sites.requests import RequestSite
29 from django.views.generic.base import View
30 from django.views.decorators.vary import vary_on_cookie
31 from django.views.decorators.cache import never_cache, cache_control
33 from mygpo.podcasts.models import Podcast, Episode, Tag
34 from mygpo.users.models import HistoryEntry, Client
35 from mygpo.subscriptions import get_subscribed_podcasts
36 from mygpo.web.utils import process_lang_params
37 from mygpo.utils import parse_range
38 from mygpo.podcastlists.models import PodcastList
39 from mygpo.favorites.models import FavoriteEpisode
40 #from mygpo.web.views.podcast import slug_id_decorator
41 from mygpo.users.settings import FLATTR_AUTO, FLATTR_TOKEN
42 from mygpo.publisher.models import PublishedPodcast
45 @vary_on_cookie
46 @cache_control(private=True)
47 def home(request):
48 if request.user.is_authenticated():
49 return dashboard(request)
50 else:
51 return welcome(request)
54 @vary_on_cookie
55 @cache_control(private=True)
56 def welcome(request):
57 current_site = RequestSite(request)
59 lang = process_lang_params(request)
61 toplist = Podcast.objects.all().toplist(lang)
63 return render(request, 'home.html', {
64 'url': current_site,
65 'toplist': toplist,
69 @vary_on_cookie
70 @cache_control(private=True)
71 @login_required
72 def dashboard(request, episode_count=10):
74 subscribed_podcasts = get_subscribed_podcasts(request.user)
75 subscribed_podcasts = [sp.podcast for sp in subscribed_podcasts]
77 site = RequestSite(request)
79 checklist = []
81 if request.user.client_set.count():
82 checklist.append('devices')
84 if subscribed_podcasts:
85 checklist.append('subscriptions')
87 if FavoriteEpisode.objects.filter(user=request.user).exists():
88 checklist.append('favorites')
90 if not request.user.profile.get_token('subscriptions_token'):
91 checklist.append('share')
93 if not request.user.profile.get_token('favorite_feeds_token'):
94 checklist.append('share-favorites')
96 if not request.user.profile.get_token('userpage_token'):
97 checklist.append('userpage')
99 if Tag.objects.filter(user=request.user).exists():
100 checklist.append('tags')
102 if PodcastList.objects.filter(user=request.user).exists():
103 checklist.append('lists')
105 if PublishedPodcast.objects.filter(publisher=request.user).exists():
106 checklist.append('publish')
108 if request.user.profile.settings.get_wksetting(FLATTR_TOKEN):
109 checklist.append('flattr')
111 if request.user.profile.settings.get_wksetting(FLATTR_AUTO):
112 checklist.append('auto-flattr')
114 tomorrow = datetime.today() + timedelta(days=1)
116 newest_episodes = Episode.objects.filter(podcast__in=subscribed_podcasts,
117 released__lt=tomorrow).\
118 select_related('podcast').\
119 prefetch_related('slugs',
120 'podcast__slugs').\
121 order_by('-released')[:episode_count]
124 # we only show the "install reader" link in firefox, because we don't know
125 # yet how/if this works in other browsers.
126 # hints appreciated at https://bugs.gpodder.org/show_bug.cgi?id=58
127 show_install_reader = \
128 'firefox' in request.META.get('HTTP_USER_AGENT', '').lower()
130 random_podcast = Podcast.objects.all().random().prefetch_related('slugs').first()
132 return render(request, 'dashboard.html', {
133 'user': request.user,
134 'subscribed_podcasts': subscribed_podcasts,
135 'newest_episodes': list(newest_episodes),
136 'random_podcast': random_podcast,
137 'checklist': checklist,
138 'site': site,
139 'show_install_reader': show_install_reader,
143 @vary_on_cookie
144 @cache_control(private=True)
145 @login_required
146 def mytags(request):
147 tags_tag = defaultdict(list)
149 user = request.user
151 tags = Tag.objects.filter(source=Tag.USER, user=user).order_by('tag')
152 for tag in tags:
153 tags_tag[tag.tag].append(tag.content_object)
155 return render(request, 'mytags.html', {
156 'tags_tag': dict(tags_tag.items()),
160 @never_cache
161 def csrf_failure(request, reason=""):
162 site = RequestSite(request)
163 return render(request, 'csrf.html', {
164 'site': site,
165 'method': request.method,
166 'referer': request.META.get('HTTP_REFERER', _('another site')),
167 'path': request.path,
168 'get': request.GET,
169 'post': request.POST,