mirror of
https://github.com/kennethreitz-archive/chishop.git
synced 2026-06-05 23:40:18 +00:00
Added decorators for testing if a user owns/maintains a package and working towards a public management view for packages
This commit is contained in:
@@ -4,7 +4,7 @@ except ImportError:
|
||||
from django.utils.functional import update_wrapper, wraps
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth import login, REDIRECT_FIELD_NAME
|
||||
from django.utils.decorators import available_attrs
|
||||
|
||||
from djangopypi.http import HttpResponseUnauthorized, login_basic_auth
|
||||
@@ -12,10 +12,8 @@ from djangopypi.http import HttpResponseUnauthorized, login_basic_auth
|
||||
|
||||
|
||||
def basic_auth(view_func):
|
||||
"""
|
||||
Decorator for views that need to handle basic authentication such as
|
||||
distutils views.
|
||||
"""
|
||||
""" Decorator for views that need to handle basic authentication such as
|
||||
distutils views. """
|
||||
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated():
|
||||
@@ -31,3 +29,55 @@ def basic_auth(view_func):
|
||||
"password.")
|
||||
return view_func(request, *args, **kwargs)
|
||||
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
|
||||
|
||||
try:
|
||||
from functools import update_wrapper, wraps
|
||||
except ImportError:
|
||||
from django.utils.functional import update_wrapper, wraps # Python 2.4 fallback.
|
||||
|
||||
from django.contrib.auth import REDIRECT_FIELD_NAME
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.utils.decorators import available_attrs
|
||||
from django.utils.http import urlquote
|
||||
|
||||
|
||||
def user_owns_package(login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
||||
"""
|
||||
Decorator for views that checks whether the user owns the currently requested
|
||||
package.
|
||||
"""
|
||||
if not login_url:
|
||||
from django.conf import settings
|
||||
login_url = settings.LOGIN_URL
|
||||
|
||||
def decorator(view_func):
|
||||
def _wrapped_view(request, package, *args, **kwargs):
|
||||
if request.user.packages_owned.filter(name=package).count() > 0:
|
||||
return view_func(request, package=package, *args, **kwargs)
|
||||
|
||||
path = urlquote(request.get_full_path())
|
||||
tup = login_url, redirect_field_name, path
|
||||
return HttpResponseRedirect('%s?%s=%s' % tup)
|
||||
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
|
||||
return decorator
|
||||
|
||||
def user_maintains_package(login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
||||
"""
|
||||
Decorator for views that checks whether the user maintains (or owns) the
|
||||
currently requested package.
|
||||
"""
|
||||
if not login_url:
|
||||
from django.conf import settings
|
||||
login_url = settings.LOGIN_URL
|
||||
|
||||
def decorator(view_func):
|
||||
def _wrapped_view(request, package, *args, **kwargs):
|
||||
if request.user.packages_owned.filter(name=package).count() > 0 or \
|
||||
request.user.packages_maintained.filter(name=package).count() > 0:
|
||||
return view_func(request, package=package, *args, **kwargs)
|
||||
|
||||
path = urlquote(request.get_full_path())
|
||||
tup = login_url, redirect_field_name, path
|
||||
return HttpResponseRedirect('%s?%s=%s' % tup)
|
||||
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
|
||||
return decorator
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.views.generic import list_detail
|
||||
from django.db.models.query import Q
|
||||
|
||||
from djangopypi.decorators import user_owns_package, user_maintains_package
|
||||
from djangopypi.models import Package
|
||||
from djangopypi.forms import SimplePackageSearchForm
|
||||
|
||||
@@ -31,4 +32,8 @@ def search(request, **kwargs):
|
||||
q = form.cleaned_data['query']
|
||||
kwargs['queryset'] = Package.objects.filter(Q(name__contains=q) |
|
||||
Q(releases__package_info__contains=q)).distinct()
|
||||
return index(request, **kwargs)
|
||||
return index(request, **kwargs)
|
||||
|
||||
@user_owns_package()
|
||||
def manage(request, package, **kwargs):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user