Allow test failures on unsupported Python versions
[mygpo.git] / mygpo / publisher / auth.py
blobc077e2eb0e4fafa7a7f4ca4fa805e8d22dc5d088
1 from functools import wraps
3 from django.http import HttpResponseRedirect
5 from mygpo.publisher.models import PublishedPodcast
8 def require_publisher(protected_view):
9 @wraps(protected_view)
10 def wrapper(request, *args, **kwargs):
12 if not request.user.is_authenticated:
13 return HttpResponseRedirect('/login/')
15 if is_publisher(request.user):
16 return protected_view(request, *args, **kwargs)
18 return HttpResponseRedirect('/')
20 return wrapper
23 def is_publisher(user):
24 """
25 checks if the given user has publisher rights,
26 ie he is either set as the publisher of at least one podcast,
27 or he has the staff flag set
28 """
30 if not user.is_authenticated:
31 return False
33 if user.is_staff:
34 return True
36 if PublishedPodcast.objects.filter(publisher=user).exists():
37 return True
39 return False