2 from functools
import wraps
3 from datetime
import datetime
5 from django
.urls
import reverse
6 from django
.http
import Http404
, HttpResponseRedirect
, HttpResponseForbidden
7 from django
.shortcuts
import render
, get_object_or_404
8 from django
.utils
.text
import slugify
9 from django
.contrib
.sites
.requests
import RequestSite
10 from django
.contrib
.auth
.decorators
import login_required
11 from django
.contrib
.auth
import get_user_model
12 from django
.contrib
import messages
13 from django
.contrib
.contenttypes
.models
import ContentType
14 from django
.utils
.translation
import ugettext
as _
15 from django
.views
import View
17 from mygpo
.podcasts
.models
import Podcast
, PodcastGroup
18 from mygpo
.podcastlists
.models
import PodcastList
, PodcastListEntry
19 from mygpo
.api
.simple
import format_podcast_list
20 from mygpo
.votes
.models
import Vote
21 from mygpo
.directory
.views
import search
as directory_search
24 logger
= logging
.getLogger(__name__
)
27 def list_decorator(must_own
=False):
30 def _decorator(request
, username
, slug
, *args
, **kwargs
):
32 User
= get_user_model()
33 user
= get_object_or_404(User
, username
=username
)
35 if must_own
and request
.user
!= user
:
36 return HttpResponseForbidden()
38 plist
= get_object_or_404(PodcastList
, user
=user
, slug
=slug
)
40 return f(request
, plist
, user
, *args
, **kwargs
)
47 @list_decorator(must_own
=False)
48 def search(request
, plist
, owner
):
49 return directory_search(request
, 'list_search.html',
50 {'listname': plist
.slug
})
54 def lists_own(request
):
56 lists
= PodcastList
.objects
.filter(user
=request
.user
)
58 return render(request
, 'lists.html', {
63 def lists_user(request
, username
):
65 User
= get_user_model()
66 user
= get_object_or_404(User
, username
=username
)
67 lists
= PodcastList
.objects
.filter(user
=user
)
69 return render(request
, 'lists_user.html', {
75 @list_decorator(must_own
=False)
76 def list_show(request
, plist
, owner
):
78 is_own
= owner
== request
.user
79 site
= RequestSite(request
)
81 objs
= [entry
.content_object
for entry
in plist
.entries
.all()]
82 max_subscribers
= max([p
.subscriber_count() for p
in objs
] + [0])
84 return render(request
, 'list.html', {
86 'max_subscribers': max_subscribers
,
88 'domain': site
.domain
,
93 @list_decorator(must_own
=False)
94 def list_opml(request
, plist
, owner
):
95 podcasts
= [entry
.content_object
for entry
in plist
.entries
.all()]
96 return format_podcast_list(podcasts
, 'opml', plist
.title
)
100 def create_list(request
):
101 title
= request
.POST
.get('title', None)
104 messages
.error(request
, _('You have to specify a title.'))
105 return HttpResponseRedirect(reverse('lists-overview'))
107 slug
= slugify(title
)
110 messages
.error(request
, _('"{title}" is not a valid title').format(
112 return HttpResponseRedirect(reverse('lists-overview'))
114 plist
, created
= PodcastList
.objects
.get_or_create(
123 list_url
= reverse('list-show', args
=[request
.user
.username
, slug
])
124 return HttpResponseRedirect(list_url
)
128 @list_decorator(must_own
=True)
129 def add_podcast(request
, plist
, owner
, podcast_id
):
132 obj
= Podcast
.objects
.all().get_by_any_id(podcast_id
)
133 except Podcast
.DoesNotExist
:
135 obj
= PodcastGroup
.objects
.get(id=podcast_id
)
136 except PodcastList
.DoesNotExist
:
140 list_url
= reverse('list-show', args
=[owner
.username
, plist
.slug
])
141 return HttpResponseRedirect(list_url
)
145 @list_decorator(must_own
=True)
146 def remove_podcast(request
, plist
, owner
, order
):
147 PodcastListEntry
.objects
.filter(podcastlist
=plist
, order
=order
).delete()
148 list_url
= reverse('list-show', args
=[owner
.username
, plist
.slug
])
149 return HttpResponseRedirect(list_url
)
153 @list_decorator(must_own
=True)
154 def delete_list(request
, plist
, owner
):
156 return HttpResponseRedirect(reverse('lists-overview'))
160 @list_decorator(must_own
=False)
161 def rate_list(request
, plist
, owner
):
162 now
= datetime
.utcnow()
164 vote
, created
= Vote
.objects
.get_or_create(
166 content_type
=ContentType
.objects
.get_for_model(plist
),
169 messages
.success(request
, _('Thanks for rating!'))
171 list_url
= reverse('list-show', args
=[owner
.username
, plist
.slug
])
172 return HttpResponseRedirect(list_url
)