diff options
| author | Tom Christie | 2011-05-27 14:40:19 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-05-27 14:40:19 +0100 |
| commit | 21d2dcc2942a8e4adb3a79b0983841e5f0ab5878 (patch) | |
| tree | 82acf04d4f092a392e6db51a7467c63406929a3c /djangorestframework/resources.py | |
| parent | 9e9ae6094943d663a677b3bbe8e8af6a43744daf (diff) | |
| download | django-rest-framework-21d2dcc2942a8e4adb3a79b0983841e5f0ab5878.tar.bz2 | |
Allow .form specified on view. Allow get_form, put_form, post_form. Add .PARAMS.
Diffstat (limited to 'djangorestframework/resources.py')
| -rw-r--r-- | djangorestframework/resources.py | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index de8e1351..1ac6eeac 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -122,7 +122,7 @@ class BaseResource(object): def __init__(self, view): self.view = view - def validate_request(self, data, files): + def validate_request(self, data, files=None): """ Given the request content return the cleaned, validated content. Typically raises a :exc:`response.ErrorResponse` with status code 400 (Bad Request) on failure. @@ -168,22 +168,12 @@ class FormResource(Resource): """ The :class:`Form` class that should be used for request validation. - This can be overridden by a :attr:`form` attribute on the :class:`.View`. + This can be overridden by a :attr:`form` attribute on the :class:`views.View`. """ form = None - def __init__(self, view): - """ - Allow a :attr:`form` attributes set on the :class:`View` to override - the :attr:`form` attribute set on the :class:`Resource`. - """ - super(FormResource, self).__init__(view) - - if getattr(view, 'form', None): - self.form = view.form - - def validate_request(self, data, files): + def validate_request(self, data, files=None): """ Given some content as input return some cleaned, validated content. Raises a :exc:`response.ErrorResponse` with status code 400 (Bad Request) on failure. @@ -285,18 +275,34 @@ class FormResource(Resource): raise ErrorResponse(400, detail) - def get_bound_form(self, data=None, files=None): + def get_bound_form(self, data=None, files=None, method=None): """ Given some content return a Django form bound to that content. If form validation is turned off (:attr:`form` class attribute is :const:`None`) then returns :const:`None`. """ - if not self.form: + + # A form on the view overrides a form on the resource. + form = getattr(self.view, 'form', self.form) + + # Use the requested method or determine the request method + if method is None and hasattr(self.view, 'request') and hasattr(self.view, 'method'): + method = self.view.method + elif method is None and hasattr(self.view, 'request'): + method = self.view.request.method + + # A method form on the view or resource overrides the general case. + # Method forms are attributes like `get_form` `post_form` `put_form`. + if method: + form = getattr(self, '%s_form' % method.lower(), form) + form = getattr(self.view, '%s_form' % method.lower(), form) + + if not form: return None if data is not None: - return self.form(data, files) + return form(data, files) - return self.form() + return form() @@ -326,14 +332,14 @@ class ModelResource(FormResource): The form class that should be used for request validation. If set to :const:`None` then the default model form validation will be used. - This can be overridden by a :attr:`form` attribute on the :class:`.View`. + This can be overridden by a :attr:`form` attribute on the :class:`views.View`. """ form = None """ The model class which this resource maps to. - This can be overridden by a :attr:`model` attribute on the :class:`.View`. + This can be overridden by a :attr:`model` attribute on the :class:`views.View`. """ model = None @@ -372,7 +378,7 @@ class ModelResource(FormResource): if getattr(view, 'model', None): self.model = view.model - def validate_request(self, data, files): + def validate_request(self, data, files=None): """ Given some content as input return some cleaned, validated content. Raises a :exc:`response.ErrorResponse` with status code 400 (Bad Request) on failure. @@ -389,7 +395,7 @@ class ModelResource(FormResource): return self._validate(data, files, allowed_extra_fields=self._property_fields_set) - def get_bound_form(self, data=None, files=None): + def get_bound_form(self, data=None, files=None, method=None): """ Given some content return a ``Form`` instance bound to that content. @@ -397,9 +403,11 @@ class ModelResource(FormResource): to create the Form, otherwise the model will be used to create a ModelForm. """ - if self.form: - # Use explict Form - return super(ModelResource, self).get_bound_form(data, files) + form = super(ModelResource, self).get_bound_form(data, files, method=method) + + # Use an explict Form if it exists + if form: + return form elif self.model: # Fall back to ModelForm which we create on the fly |
