Remove unused license preamble
[mygpo.git] / mygpo / web / views.py
blobb7a6f7a81925eafcdc2c2f8d2459f81ce213d587
1 import sys
2 from collections import defaultdict
3 from datetime import datetime, timedelta
5 from django.core.urlresolvers import reverse
6 from django.http import HttpResponseRedirect
7 from django.contrib import messages
8 from django.utils.translation import ugettext as _
9 from django.contrib.auth.decorators import login_required
10 from django.shortcuts import render
11 from django.contrib.sites.requests import RequestSite
12 from django.views.generic.base import View
13 from django.views.decorators.vary import vary_on_cookie
14 from django.views.decorators.cache import never_cache, cache_control
16 from mygpo.podcasts.models import Podcast, Episode, Tag
17 from mygpo.users.models import HistoryEntry, Client
18 from mygpo.subscriptions import get_subscribed_podcasts
19 from mygpo.web.utils import process_lang_params
20 from mygpo.utils import parse_range
21 from mygpo.podcastlists.models import PodcastList
22 from mygpo.favorites.models import FavoriteEpisode
23 #from mygpo.web.views.podcast import slug_id_decorator
24 from mygpo.users.settings import FLATTR_AUTO, FLATTR_TOKEN
25 from mygpo.publisher.models import PublishedPodcast
28 @vary_on_cookie
29 @cache_control(private=True)
30 def home(request):
31 if request.user.is_authenticated():
32 return dashboard(request)
33 else:
34 return welcome(request)
37 @vary_on_cookie
38 @cache_control(private=True)
39 def welcome(request):
40 current_site = RequestSite(request)
42 lang = process_lang_params(request)
44 toplist = Podcast.objects.all().toplist(lang)
46 return render(request, 'home.html', {
47 'url': current_site,
48 'toplist': toplist,
52 @vary_on_cookie
53 @cache_control(private=True)
54 @login_required
55 def dashboard(request, episode_count=10):
57 subscribed_podcasts = get_subscribed_podcasts(request.user)
58 subscribed_podcasts = [sp.podcast for sp in subscribed_podcasts]
60 podcast_ad = Podcast.objects.get_advertised_podcast()
62 site = RequestSite(request)
64 checklist = []
66 if request.user.client_set.count():
67 checklist.append('devices')
69 if subscribed_podcasts:
70 checklist.append('subscriptions')
72 if FavoriteEpisode.objects.filter(user=request.user).exists():
73 checklist.append('favorites')
75 if not request.user.profile.get_token('subscriptions_token'):
76 checklist.append('share')
78 if not request.user.profile.get_token('favorite_feeds_token'):
79 checklist.append('share-favorites')
81 if not request.user.profile.get_token('userpage_token'):
82 checklist.append('userpage')
84 if Tag.objects.filter(user=request.user).exists():
85 checklist.append('tags')
87 if PodcastList.objects.filter(user=request.user).exists():
88 checklist.append('lists')
90 if PublishedPodcast.objects.filter(publisher=request.user).exists():
91 checklist.append('publish')
93 if request.user.profile.settings.get_wksetting(FLATTR_TOKEN):
94 checklist.append('flattr')
96 if request.user.profile.settings.get_wksetting(FLATTR_AUTO):
97 checklist.append('auto-flattr')
99 tomorrow = datetime.today() + timedelta(days=1)
101 newest_episodes = Episode.objects.filter(podcast__in=subscribed_podcasts,
102 released__lt=tomorrow).\
103 select_related('podcast').\
104 prefetch_related('slugs',
105 'podcast__slugs').\
106 order_by('-released')[:episode_count]
109 # we only show the "install reader" link in firefox, because we don't know
110 # yet how/if this works in other browsers.
111 # hints appreciated at https://bugs.gpodder.org/show_bug.cgi?id=58
112 show_install_reader = \
113 'firefox' in request.META.get('HTTP_USER_AGENT', '').lower()
115 random_podcast = Podcast.objects.all().random().prefetch_related('slugs').first()
117 return render(request, 'dashboard.html', {
118 'user': request.user,
119 'subscribed_podcasts': subscribed_podcasts,
120 'newest_episodes': list(newest_episodes),
121 'random_podcast': random_podcast,
122 'checklist': checklist,
123 'site': site,
124 'show_install_reader': show_install_reader,
125 'podcast_ad': podcast_ad,
129 @vary_on_cookie
130 @cache_control(private=True)
131 @login_required
132 def mytags(request):
133 tags_tag = defaultdict(list)
135 user = request.user
137 tags = Tag.objects.filter(source=Tag.USER, user=user).order_by('tag')
138 for tag in tags:
139 tags_tag[tag.tag].append(tag.content_object)
141 return render(request, 'mytags.html', {
142 'tags_tag': dict(tags_tag.items()),
146 @never_cache
147 def csrf_failure(request, reason=""):
148 site = RequestSite(request)
149 return render(request, 'csrf.html', {
150 'site': site,
151 'method': request.method,
152 'referer': request.META.get('HTTP_REFERER', _('another site')),
153 'path': request.path,
154 'get': request.GET,
155 'post': request.POST,