aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/mixins.py
diff options
context:
space:
mode:
authorJamie Matthews2012-09-26 13:05:21 +0100
committerJamie Matthews2012-09-26 13:05:21 +0100
commit01770c53cd9045e6ea054f32b1e40b5d2ff7fe44 (patch)
tree657cb66f92d78add3b2f587754387832043168e6 /rest_framework/mixins.py
parentf6488cb0589d3b11fb8d831e00d1389f3fff74b6 (diff)
parent09a445b257532be69ffab69a3f62b84bfa90463d (diff)
downloaddjango-rest-framework-01770c53cd9045e6ea054f32b1e40b5d2ff7fe44.tar.bz2
Merge branch 'restframework2' of git://github.com/tomchristie/django-rest-framework into improved-view-decorators
* 'restframework2' of git://github.com/tomchristie/django-rest-framework: (56 commits) Bits of cleanup Add request.QUERY_PARAMS Add readonly 'id' field Tweak browseable API Don't display readonly fields Fix some bits of serialization Add csrf note Fix incorrect bit of tutorial Added tox.ini Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing Clean up bits of templates etc Hack out bunch of unneccesary private methods on View class Clean up template tags Remove dumbass __all__ variables Remove old 'djangorestframework directories Change package name: djangorestframework -> rest_framework Dont strip final '/' Use get_script_prefix to play nicely if not installed at the root. ... Conflicts: rest_framework/decorators.py
Diffstat (limited to 'rest_framework/mixins.py')
-rw-r--r--rest_framework/mixins.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
new file mode 100644
index 00000000..33a363d3
--- /dev/null
+++ b/rest_framework/mixins.py
@@ -0,0 +1,97 @@
+"""
+Basic building blocks for generic class based views.
+
+We don't bind behaviour to http method handlers yet,
+which allows mixin classes to be composed in interesting ways.
+
+Eg. Use mixins to build a Resource class, and have a Router class
+ perform the binding of http methods to actions for us.
+"""
+from rest_framework import status
+from rest_framework.response import Response
+
+
+class CreateModelMixin(object):
+ """
+ Create a model instance.
+ Should be mixed in with any `BaseView`.
+ """
+ def create(self, request, *args, **kwargs):
+ serializer = self.get_serializer(data=request.DATA)
+ if serializer.is_valid():
+ self.object = serializer.object
+ self.object.save()
+ return Response(serializer.data, status=status.HTTP_201_CREATED)
+ return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
+
+
+class ListModelMixin(object):
+ """
+ List a queryset.
+ Should be mixed in with `MultipleObjectBaseView`.
+ """
+ def list(self, request, *args, **kwargs):
+ self.object_list = self.get_queryset()
+ serializer = self.get_serializer(instance=self.object_list)
+ return Response(serializer.data)
+
+
+class RetrieveModelMixin(object):
+ """
+ Retrieve a model instance.
+ Should be mixed in with `SingleObjectBaseView`.
+ """
+ def retrieve(self, request, *args, **kwargs):
+ self.object = self.get_object()
+ serializer = self.get_serializer(instance=self.object)
+ return Response(serializer.data)
+
+
+class UpdateModelMixin(object):
+ """
+ Update a model instance.
+ Should be mixed in with `SingleObjectBaseView`.
+ """
+ def update(self, request, *args, **kwargs):
+ self.object = self.get_object()
+ serializer = self.get_serializer(data=request.DATA, instance=self.object)
+ if serializer.is_valid():
+ self.object = serializer.object
+ self.object.save()
+ return Response(serializer.data)
+ return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
+
+
+class DestroyModelMixin(object):
+ """
+ Destroy a model instance.
+ Should be mixed in with `SingleObjectBaseView`.
+ """
+ def destroy(self, request, *args, **kwargs):
+ self.object = self.get_object()
+ self.object.delete()
+ return Response(status=status.HTTP_204_NO_CONTENT)
+
+
+class MetadataMixin(object):
+ """
+ Return a dicitonary of view metadata.
+ Should be mixed in with any `BaseView`.
+
+ This mixin is typically used for the HTTP 'OPTIONS' method.
+ """
+ def metadata(self, request, *args, **kwargs):
+ content = {
+ 'name': self.get_name(),
+ 'description': self.get_description(),
+ 'renders': [renderer.media_type for renderer in self.renderer_classes],
+ 'parses': [parser.media_type for parser in self.parser_classes],
+ }
+ # TODO: Add 'fields', from serializer info.
+ # form = self.get_bound_form()
+ # if form is not None:
+ # field_name_types = {}
+ # for name, field in form.fields.iteritems():
+ # field_name_types[name] = field.__class__.__name__
+ # content['fields'] = field_name_types
+ return Response(content, status=status.HTTP_200_OK)