--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
| Filipe Ximenes +55 (81) 8245-9204 Vinta Software Studio http://www.vinta.com.br |
class UserProfileViewSet(viewsets.ModelViewSet):
permission_classes =(CustomPermissionClass, )
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
class ProjectViewSet(viewset.ModelViewSet):
permission_classes = (AnotherCustomPermissionClass,)
queryset =Project.objects.all()
serializer_class = ProjectSerializer
@list_route(methods=['get'])
def forprofile(request, *args, **kwargs):
user_id = request.query_params.get('user_id', None)
#This is where I would like to be able to use the retrieve functionality that django rest framework provides for free to lookup the user and save it to a "user" variable
projects = self.get_queryset().filter(user=user)
serializer = self.get_serializer(projects, many=True)
return Response(serializer.data)
Hi David, pardon if I din't understand the question but wouldn't creating a mixin to be shared by the 2 ViewSets solve this? This way you would have a separate class with the code that can be used by the ViewSets and extend it in each one of then.
On Mon, Jun 29, 2015 at 10:12 PM, David Kaplan <dave.b...@gmail.com> wrote:
I would like to call an existing ViewSet that I have created from a new ViewSet. The existing viewset I want to call handles all of my User data updates and retrievals. The new viewset takes a userid as a parameter in order to look up related objects. I would like to reuse the existing viewset because it already encapsulates my permission logic, filter logic, and custom retrieval logic for users and I want to use this same validation as a first step for this new call. I have been researching, but have not seen a solution posted.
The 2 problems I see is that viewset functions like retrieve take in a Request and return a Response. I could create and manually pack a request and manually unpack a response within my new viewset, but I feel icky just writing that.
Is there an easier more standard way to reuse viewsets as internal APIs?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
from rest_framework import permissions
class CustomViewPermission(permissions.BasePermission):
def has_permission(self, request, view):
if hasattr(view, 'custom_permission'):
return view.custom_permission(request)
return True
def has_object_permission(self, request, view, obj):
if hasattr(view, 'custom_object_permission'):
return view.custom_object_permission(request, obj)
return True
class UserProcessMixin(object):
def custom_object_permission(request, obj):
# do your permission checking here
# using self.user NOT obj
return True # or False depending on your checking
class UserProfileViewSet(UserProcessMixin, viewsets.ModelViewSet):
permission_classes =(CustomViewPermission, )
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
def get_object(self):
self.user = super(UserProfileViewSet, self).get_object()
return self.user
class ProjectViewSet(UserProcessMixin, viewset.ModelViewSet):
permission_classes = (CustomViewPermission,)
queryset =Project.objects.all()
serializer_class = ProjectSerializer
def get_user(self, user_id):
self.user = User.objects.get(id=user_id)
return self.user
@list_route(methods=['get'])
def forprofile(request, *args, **kwargs):
user_id = request.query_params.get('user_id', None)
self.get_user(user_id)
self.check_object_permissions(self.request, None)
projects = self.get_queryset().filter(user=user)
serializer = self.get_serializer(projects, many=True)
return Response(serializer.data)
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
def another_view(request, *args, **kwargs):
viewfn = MyViewSet.as_view({'get': 'retrieve'})
response = viewfn(request, *args, **kwargs)
# directly use response.data