Python rest_framework.viewsets.ViewSet() Examples

The following are 6 code examples of rest_framework.viewsets.ViewSet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.viewsets , or try the search function .
Example #1
Source File: test_viewsets.py    From djangorest-alchemy with MIT License 6 votes vote down vote up
def test_action_methods_manager_mixin(self):
        '''
        Test if action methods specified on managers using action_methods
        class field are registered with viewset
        '''

        class MockManager(SessionMixin, AlchemyModelManager):
            model_class = mock.Mock()
            action_methods = {'method_name': ['POST', 'DELETE']}

            def method_name(self, data, pk=None, **kwargs):
                return {'status': 'created'}

        class MockViewSet(viewsets.ViewSet, ManagerMixin):
            manager_class = MockManager

        viewset = MockViewSet()
        self.assertTrue(hasattr(viewset, 'method_name'))

        method = getattr(viewset, 'method_name')
        # DRF looks for methods with this attr
        self.assertTrue(hasattr(method, 'bind_to_methods')) 
Example #2
Source File: views.py    From figures with MIT License 5 votes vote down vote up
def get_queryset(self):
        """
        Stub method because ViewSet requires one, even though we are not
        retrieving querysets directly (we use the query in figures.sites)
        """
        pass 
Example #3
Source File: views.py    From figures with MIT License 5 votes vote down vote up
def get_queryset(self):
        """
        Stub method because ViewSet requires one, even though we are not
        retrieving querysets directly (we use the query in figures.sites)
        """
        pass 
Example #4
Source File: views.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def create(self, request):
        """
        Validate the given form input, and return any errors as json.

        Not really a list view, but we have to use `list` to fit into ViewSet
        semantics so this can be part of the browsable api.
        """
        form = BetaTestApplicationForm(request.data,
                                       instance=self.get_object())
        return Response(form.errors) 
Example #5
Source File: fermentables.py    From BrewCenterAPI with GNU General Public License v3.0 4 votes vote down vote up
def get_permissions(self):
        """Define custom permissions for different methods"""

        # at minimum require users to be authenticated
        self.permission_classes = [IsAuthenticated]
        # for PUT requests require users to be admins
        if self.request.method == 'PUT':
            self.permission_classes.append(IsAdminUser)

        return super(viewsets.ViewSet, self).get_permissions() 
Example #6
Source File: fermentables.py    From BrewCenterAPI with GNU General Public License v3.0 4 votes vote down vote up
def get_permissions(self):
        """Define custom permissions for different methods"""

        # at minimum require users to be authenticated
        self.permission_classes = [IsAuthenticated]
        # for PUT requests require users to be admins
        if self.request.method == 'PUT':
            self.permission_classes.append(IsAdminUser)

        return super(viewsets.ViewSet, self).get_permissions()