diff options
| author | Tom Christie | 2012-02-23 09:21:01 +0000 |
|---|---|---|
| committer | Tom Christie | 2012-02-23 09:21:01 +0000 |
| commit | e15494a172f19d9388c0888fb566907aad95de0c (patch) | |
| tree | d7c8f83fe93a3bade46ac5533f5bc8121a3cf332 /djangorestframework | |
| parent | 2b59df004a5bb7449aa4c07277ac846c330a79f7 (diff) | |
| download | django-rest-framework-e15494a172f19d9388c0888fb566907aad95de0c.tar.bz2 | |
Remove InstanceMixin auto-url magicks.
Diffstat (limited to 'djangorestframework')
| -rw-r--r-- | djangorestframework/mixins.py | 31 | ||||
| -rw-r--r-- | djangorestframework/resources.py | 55 | ||||
| -rw-r--r-- | djangorestframework/reverse.py | 2 | ||||
| -rw-r--r-- | djangorestframework/tests/modelviews.py | 5 | ||||
| -rw-r--r-- | djangorestframework/views.py | 3 |
5 files changed, 12 insertions, 84 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index f38277eb..6c8f8179 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -25,14 +25,13 @@ __all__ = ( 'ResponseMixin', 'AuthMixin', 'ResourceMixin', - # Reverse URL lookup behavior - 'InstanceMixin', # Model behavior mixins 'ReadModelMixin', 'CreateModelMixin', 'UpdateModelMixin', 'DeleteModelMixin', - 'ListModelMixin' + 'ListModelMixin', + 'PaginatorMixin' ) @@ -444,30 +443,6 @@ class ResourceMixin(object): else: return None -########## - - -class InstanceMixin(object): - """ - `Mixin` class that is used to identify a `View` class as being the canonical identifier - for the resources it is mapped to. - """ - - @classmethod - def as_view(cls, **initkwargs): - """ - Store the callable object on the resource class that has been associated with this view. - """ - view = super(InstanceMixin, cls).as_view(**initkwargs) - resource = getattr(cls(**initkwargs), 'resource', None) - if resource: - # We do a little dance when we store the view callable... - # we need to store it wrapped in a 1-tuple, so that inspect will treat it - # as a function when we later look it up (rather than turning it into a method). - # This makes sure our URL reversing works ok. - resource.view_callable = (view,) - return view - ########## Model Mixins ########## @@ -599,7 +574,7 @@ class CreateModelMixin(ModelMixin): manager.through(**data).save() headers = {} - if hasattr(instance, 'get_absolute_url'): + if hasattr(self.resource, 'url'): headers['Location'] = self.resource(self).url(instance) return Response(status.HTTP_201_CREATED, instance, headers) diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index 71c4d2b9..f170eb45 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -1,10 +1,7 @@ from django import forms -from django.core.urlresolvers import get_urlconf, get_resolver, NoReverseMatch -from django.db import models from djangorestframework.response import ErrorResponse -from djangorestframework.reverse import reverse -from djangorestframework.serializer import Serializer, _SkipField +from djangorestframework.serializer import Serializer from djangorestframework.utils import as_tuple @@ -20,7 +17,7 @@ class BaseResource(Serializer): def __init__(self, view=None, depth=None, stack=[], **kwargs): super(BaseResource, self).__init__(depth, stack, **kwargs) self.view = view - self.request = view.request + self.request = getattr(view, 'request', None) def validate_request(self, data, files=None): """ @@ -224,9 +221,6 @@ class ModelResource(FormResource): Also provides a :meth:`get_bound_form` method which may be used by some renderers. """ - # Auto-register new ModelResource classes into _model_to_resource - #__metaclass__ = _RegisterModelResource - form = None """ The form class that should be used for request validation. @@ -260,7 +254,7 @@ class ModelResource(FormResource): The list of fields to exclude. This is only used if :attr:`fields` is not set. """ - include = ('url',) + include = () """ The list of extra fields to include. This is only used if :attr:`fields` is not set. """ @@ -323,49 +317,6 @@ class ModelResource(FormResource): return form() - def url(self, instance): - """ - Attempts to reverse resolve the url of the given model *instance* for this resource. - - Requires a ``View`` with :class:`mixins.InstanceMixin` to have been created for this resource. - - This method can be overridden if you need to set the resource url reversing explicitly. - """ - - if not hasattr(self, 'view_callable'): - raise _SkipField - - # dis does teh magicks... - urlconf = get_urlconf() - resolver = get_resolver(urlconf) - - possibilities = resolver.reverse_dict.getlist(self.view_callable[0]) - for tuple_item in possibilities: - possibility = tuple_item[0] - # pattern = tuple_item[1] - # Note: defaults = tuple_item[2] for django >= 1.3 - for result, params in possibility: - - #instance_attrs = dict([ (param, getattr(instance, param)) for param in params if hasattr(instance, param) ]) - - instance_attrs = {} - for param in params: - if not hasattr(instance, param): - continue - attr = getattr(instance, param) - if isinstance(attr, models.Model): - instance_attrs[param] = attr.pk - else: - instance_attrs[param] = attr - - try: - return reverse(self.view_callable[0], - kwargs=instance_attrs, - request=self.view.request) - except NoReverseMatch: - pass - raise _SkipField - @property def _model_fields_set(self): """ diff --git a/djangorestframework/reverse.py b/djangorestframework/reverse.py index 05643157..ba663f98 100644 --- a/djangorestframework/reverse.py +++ b/djangorestframework/reverse.py @@ -5,7 +5,7 @@ from django.core.urlresolvers import reverse as django_reverse from django.utils.functional import lazy -def reverse(viewname, request, *args, **kwargs): +def reverse(viewname, *args, **kwargs): """ Same as `django.core.urlresolvers.reverse`, but optionally takes a request and returns a fully qualified URL, using the request to get the base URL. diff --git a/djangorestframework/tests/modelviews.py b/djangorestframework/tests/modelviews.py index 031e65c5..ccd8513f 100644 --- a/djangorestframework/tests/modelviews.py +++ b/djangorestframework/tests/modelviews.py @@ -1,5 +1,4 @@ from django.conf.urls.defaults import patterns, url -from django.test import TestCase from django.forms import ModelForm from django.contrib.auth.models import Group, User from djangorestframework.resources import ModelResource @@ -7,18 +6,22 @@ from djangorestframework.views import ListOrCreateModelView, InstanceModelView from djangorestframework.tests.models import CustomUser from djangorestframework.tests.testcases import TestModelsTestCase + class GroupResource(ModelResource): model = Group + class UserForm(ModelForm): class Meta: model = User exclude = ('last_login', 'date_joined') + class UserResource(ModelResource): model = User form = UserForm + class CustomUserResource(ModelResource): model = CustomUser diff --git a/djangorestframework/views.py b/djangorestframework/views.py index bcdb5039..3657fd64 100644 --- a/djangorestframework/views.py +++ b/djangorestframework/views.py @@ -6,7 +6,6 @@ By setting or modifying class attributes on your view, you change it's predefine """ import re -from django.core.urlresolvers import set_script_prefix, get_script_prefix from django.http import HttpResponse from django.utils.html import escape from django.utils.safestring import mark_safe @@ -269,7 +268,7 @@ class ModelView(View): resource = resources.ModelResource -class InstanceModelView(InstanceMixin, ReadModelMixin, UpdateModelMixin, DeleteModelMixin, ModelView): +class InstanceModelView(ReadModelMixin, UpdateModelMixin, DeleteModelMixin, ModelView): """ A view which provides default operations for read/update/delete against a model instance. """ |
