mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
depercate auth and content arguments to the request handler methods - yea :)
This commit is contained in:
parent
6096b50dbe
commit
18bbda84b9
|
@ -28,8 +28,9 @@ class BaseAuthenticator(object):
|
||||||
The default permission checking on Resource will use the allowed_methods attribute
|
The default permission checking on Resource will use the allowed_methods attribute
|
||||||
for permissions if the authentication context is not None, and use anon_allowed_methods otherwise.
|
for permissions if the authentication context is not None, and use anon_allowed_methods otherwise.
|
||||||
|
|
||||||
The authentication context is passed to the method calls eg Resource.get(request, auth) in order to
|
The authentication context is available to the method calls eg Resource.get(request)
|
||||||
allow them to apply any more fine grained permission checking at the point the response is being generated.
|
by accessing self.auth in order to allow them to apply any more fine grained permission
|
||||||
|
checking at the point the response is being generated.
|
||||||
|
|
||||||
This function must be overridden to be implemented."""
|
This function must be overridden to be implemented."""
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -341,7 +341,7 @@ class ModelResource(Resource):
|
||||||
return _any(data, self.fields)
|
return _any(data, self.fields)
|
||||||
|
|
||||||
|
|
||||||
def post(self, request, auth, content, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
# TODO: test creation on a non-existing resource url
|
# TODO: test creation on a non-existing resource url
|
||||||
|
|
||||||
# translated related_field into related_field_id
|
# translated related_field into related_field_id
|
||||||
|
@ -350,7 +350,7 @@ class ModelResource(Resource):
|
||||||
kwargs[related_name + '_id'] = kwargs[related_name]
|
kwargs[related_name + '_id'] = kwargs[related_name]
|
||||||
del kwargs[related_name]
|
del kwargs[related_name]
|
||||||
|
|
||||||
all_kw_args = dict(content.items() + kwargs.items())
|
all_kw_args = dict(self.CONTENT.items() + kwargs.items())
|
||||||
if args:
|
if args:
|
||||||
instance = self.model(pk=args[-1], **all_kw_args)
|
instance = self.model(pk=args[-1], **all_kw_args)
|
||||||
else:
|
else:
|
||||||
|
@ -361,7 +361,7 @@ class ModelResource(Resource):
|
||||||
headers['Location'] = instance.get_absolute_url()
|
headers['Location'] = instance.get_absolute_url()
|
||||||
return Response(status.HTTP_201_CREATED, instance, headers)
|
return Response(status.HTTP_201_CREATED, instance, headers)
|
||||||
|
|
||||||
def get(self, request, auth, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
if args:
|
if args:
|
||||||
# If we have any none kwargs then assume the last represents the primrary key
|
# If we have any none kwargs then assume the last represents the primrary key
|
||||||
|
@ -374,7 +374,7 @@ class ModelResource(Resource):
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def put(self, request, auth, content, *args, **kwargs):
|
def put(self, request, *args, **kwargs):
|
||||||
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
|
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
|
||||||
try:
|
try:
|
||||||
if args:
|
if args:
|
||||||
|
@ -384,16 +384,16 @@ class ModelResource(Resource):
|
||||||
# Otherwise assume the kwargs uniquely identify the model
|
# Otherwise assume the kwargs uniquely identify the model
|
||||||
instance = self.model.objects.get(**kwargs)
|
instance = self.model.objects.get(**kwargs)
|
||||||
|
|
||||||
for (key, val) in content.items():
|
for (key, val) in self.CONTENT.items():
|
||||||
setattr(instance, key, val)
|
setattr(instance, key, val)
|
||||||
except self.model.DoesNotExist:
|
except self.model.DoesNotExist:
|
||||||
instance = self.model(**content)
|
instance = self.model(**self.CONTENT)
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
||||||
instance.save()
|
instance.save()
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def delete(self, request, auth, *args, **kwargs):
|
def delete(self, request, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
if args:
|
if args:
|
||||||
# If we have any none kwargs then assume the last represents the primrary key
|
# If we have any none kwargs then assume the last represents the primrary key
|
||||||
|
@ -413,7 +413,7 @@ class RootModelResource(ModelResource):
|
||||||
allowed_methods = ('GET', 'POST')
|
allowed_methods = ('GET', 'POST')
|
||||||
queryset = None
|
queryset = None
|
||||||
|
|
||||||
def get(self, request, auth, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
queryset = self.queryset if self.queryset else self.model.objects.all()
|
queryset = self.queryset if self.queryset else self.model.objects.all()
|
||||||
return queryset.filter(**kwargs)
|
return queryset.filter(**kwargs)
|
||||||
|
|
||||||
|
@ -427,7 +427,7 @@ class QueryModelResource(ModelResource):
|
||||||
def get_form(self, data=None):
|
def get_form(self, data=None):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get(self, request, auth, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
queryset = self.queryset if self.queryset else self.model.objects.all()
|
queryset = self.queryset if self.queryset else self.model.objects.all()
|
||||||
return queryset.filer(**kwargs)
|
return queryset.filer(**kwargs)
|
||||||
|
|
||||||
|
|
|
@ -57,22 +57,22 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
callmap = { 'GET': 'get', 'POST': 'post',
|
callmap = { 'GET': 'get', 'POST': 'post',
|
||||||
'PUT': 'put', 'DELETE': 'delete' }
|
'PUT': 'put', 'DELETE': 'delete' }
|
||||||
|
|
||||||
def get(self, request, auth, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
"""Must be subclassed to be implemented."""
|
"""Must be subclassed to be implemented."""
|
||||||
self.not_implemented('GET')
|
self.not_implemented('GET')
|
||||||
|
|
||||||
|
|
||||||
def post(self, request, auth, content, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
"""Must be subclassed to be implemented."""
|
"""Must be subclassed to be implemented."""
|
||||||
self.not_implemented('POST')
|
self.not_implemented('POST')
|
||||||
|
|
||||||
|
|
||||||
def put(self, request, auth, content, *args, **kwargs):
|
def put(self, request, *args, **kwargs):
|
||||||
"""Must be subclassed to be implemented."""
|
"""Must be subclassed to be implemented."""
|
||||||
self.not_implemented('PUT')
|
self.not_implemented('PUT')
|
||||||
|
|
||||||
|
|
||||||
def delete(self, request, auth, *args, **kwargs):
|
def delete(self, request, *args, **kwargs):
|
||||||
"""Must be subclassed to be implemented."""
|
"""Must be subclassed to be implemented."""
|
||||||
self.not_implemented('DELETE')
|
self.not_implemented('DELETE')
|
||||||
|
|
||||||
|
@ -154,11 +154,7 @@ class Resource(RequestMixin, ResponseMixin, AuthMixin, View):
|
||||||
# Either generate the response data, deserializing and validating any request data
|
# Either generate the response data, deserializing and validating any request data
|
||||||
# TODO: This is going to change to: func(request, *args, **kwargs)
|
# TODO: This is going to change to: func(request, *args, **kwargs)
|
||||||
# That'll work out now that we have the lazily evaluated self.CONTENT property.
|
# That'll work out now that we have the lazily evaluated self.CONTENT property.
|
||||||
if self.method in ('PUT', 'POST'):
|
response_obj = func(request, *args, **kwargs)
|
||||||
response_obj = func(request, auth_context, self.CONTENT, *args, **kwargs)
|
|
||||||
|
|
||||||
else:
|
|
||||||
response_obj = func(request, auth_context, *args, **kwargs)
|
|
||||||
|
|
||||||
# Allow return value to be either Response, or an object, or None
|
# Allow return value to be either Response, or an object, or None
|
||||||
if isinstance(response_obj, Response):
|
if isinstance(response_obj, Response):
|
||||||
|
|
|
@ -20,7 +20,7 @@ class UserAgentMungingTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
class MockResource(Resource):
|
class MockResource(Resource):
|
||||||
anon_allowed_methods = allowed_methods = ('GET',)
|
anon_allowed_methods = allowed_methods = ('GET',)
|
||||||
def get(self, request, auth):
|
def get(self, request):
|
||||||
return {'a':1, 'b':2, 'c':3}
|
return {'a':1, 'b':2, 'c':3}
|
||||||
self.req = RequestFactory()
|
self.req = RequestFactory()
|
||||||
self.MockResource = MockResource
|
self.MockResource = MockResource
|
||||||
|
|
|
@ -15,7 +15,7 @@ except ImportError:
|
||||||
class MockResource(Resource):
|
class MockResource(Resource):
|
||||||
allowed_methods = ('POST',)
|
allowed_methods = ('POST',)
|
||||||
|
|
||||||
def post(self, request, auth, content):
|
def post(self, request):
|
||||||
return {'a':1, 'b':2, 'c':3}
|
return {'a':1, 'b':2, 'c':3}
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
|
|
|
@ -19,10 +19,9 @@ class UploadFilesTests(TestCase):
|
||||||
allowed_methods = anon_allowed_methods = ('POST',)
|
allowed_methods = anon_allowed_methods = ('POST',)
|
||||||
form = FileForm
|
form = FileForm
|
||||||
|
|
||||||
def post(self, request, auth, content, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
#self.uploaded = content.file
|
return {'FILE_NAME': self.CONTENT['file'].name,
|
||||||
return {'FILE_NAME': content['file'].name,
|
'FILE_CONTENT': self.CONTENT['file'].read()}
|
||||||
'FILE_CONTENT': content['file'].read()}
|
|
||||||
|
|
||||||
file = StringIO.StringIO('stuff')
|
file = StringIO.StringIO('stuff')
|
||||||
file.name = 'stuff.txt'
|
file.name = 'stuff.txt'
|
||||||
|
|
|
@ -14,7 +14,7 @@ class MockResource(Resource):
|
||||||
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified"""
|
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified"""
|
||||||
anon_allowed_methods = ('GET',)
|
anon_allowed_methods = ('GET',)
|
||||||
|
|
||||||
def get(self, request, auth):
|
def get(self, request):
|
||||||
return reverse('another')
|
return reverse('another')
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user