Created
April 10, 2013 20:22
-
-
Save gavinwahl/5358110 to your computer and use it in GitHub Desktop.
`Resource` class to make django-rest-framework less tedious.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Resource(object): | |
class Base(object): | |
pass | |
class List(generics.ListCreateAPIView): | |
pass | |
class Detail(generics.RetrieveUpdateDestroyAPIView): | |
pass | |
@property | |
def list_class(self): | |
return type('%sList' % self.model.__name__, (self.Base, self.List), { | |
'__doc__': self.docstring(self.List, self, self.Base) | |
}) | |
@property | |
def detail_class(self): | |
return type('%sDetail' % self.model.__name__, (self.Base, self.Detail), { | |
'__doc__': self.docstring(self.Detail, self, self.Base) | |
}) | |
@staticmethod | |
def docstring(*classes): | |
""" | |
Returns the docstring of the first class in classes that has one. | |
""" | |
for cls in classes: | |
if cls.__doc__: | |
return cls.__doc__ | |
return None | |
def as_list_view(self): | |
return self.list_class.as_view(**self.get_view_kwargs()) | |
def as_detail_view(self): | |
return self.detail_class.as_view(**self.get_view_kwargs()) | |
def get_view_kwargs(self): | |
return dict( | |
model=self.model, | |
serializer_class=self.serializer_class | |
) | |
@property | |
def urls(self): | |
return patterns('', | |
url('^$', self.as_list_view(), name=self.model_name + '-list'), | |
url('^(?P<pk>[^/]+)/$', self.as_detail_view(), name=self.model_name + '-detail'), | |
) | |
@property | |
def model_name(self): | |
return self.model._meta.module_name | |
@property | |
def model(self): | |
return self.serializer_class.Meta.model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlpatterns = patterns('api.views', | |
url(r'^users/', include(UserResource().urls)), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserResource(Resource): | |
serializer_class = UserSerializer | |
# all the rest is optional... | |
# mix this in to both the List and Detail views | |
class Base(object): | |
def get_queryset(self): | |
qs = super(UserResource.Base, self).get_queryset() | |
if not self.request.user.is_authenticated(): | |
return qs.none() | |
else: | |
return qs.filter(pk=self.request.user.pk) | |
# list view is unauthenticated | |
class List(generics.ListCreateAPIView): | |
permission_classes = () | |
# can't delete users | |
Detail = generics.RetrieveUpdateAPIView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment