Python rest_framework.mixins.ListModelMixin() Examples

The following are 4 code examples of rest_framework.mixins.ListModelMixin(). 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.mixins , or try the search function .
Example #1
Source File: views.py    From CTF_AWD_Platform with MIT License 6 votes vote down vote up
def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        mobile = serializer.validated_data['mobile']
        list = []
        list.append(mobile)
        code = self.generate_code()
        SendCode.SendMail.delay(code, list)

        code_record = VerifyCode(code=code, mobile=mobile, type='email')
        code_record.save()
        return Response({
            "mobile": mobile
        }, status=status.HTTP_201_CREATED)


# class UserLogViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet): 
Example #2
Source File: initialize.py    From cmdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
def add_viewset(table):
    data_index = table.name
    record_data_index = "{}.".format(table.name)
    deleted_data_index = "{}..".format(table.name)

    def list(self, request, *args, **kwargs):
        page = int(request.query_params.get("page", 1))
        page_size = int(request.query_params.get("page_size", 10))
        try:
            res = es.search(index=deleted_data_index, doc_type="deleted-data", size=page_size, from_=(page-1)*page_size)
        except Exception as exc:
            raise exceptions.APIException("内部错误,错误类型: {}".format(type(exc)))
        return Response(res["hits"])

    def retrieve(self, request, *args, **kwargs):
        try:
            res = es.get(index=deleted_data_index, doc_type="data", id=kwargs["pk"])
        except NotFoundError as exc:
            raise exceptions.NotFound("Document {} was not found in Type {} of Index {}".format(kwargs["pk"], "data", table.name))
    viewset = type(table.name, (mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet),
                   dict(permission_classes=(c_permissions.TableLevelPermission, ), list=list, retrieve=retrieve))
    setattr(views, table.name, viewset)
    return viewset 
Example #3
Source File: tests.py    From scirius with GNU General Public License v3.0 5 votes vote down vote up
def test_001_default_order(self):
        # Ordering must be set to prevent:
        # /usr/share/python/scirius-pro/local/lib/python2.7/site-packages/rest_framework/pagination.py:208: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'rules.models.RuleTransformation'> QuerySet
        for url, viewset, view_name in self.router.registry:
            # Need to instanciate request and user because of FilterSetViewSet::get_queryset override that uses self.request.user
            v = viewset()
            v.request = HttpRequest()
            v.request.user = None

            if v.get_queryset().ordered or not issubclass(viewset, mixins.ListModelMixin):
                continue
            ERR = 'Viewset "%s" must set an "ordering" attribute or have an ordered queryset' % viewset.__name__
            self.assertTrue(hasattr(viewset, 'ordering'), ERR)
            self.assertNotEqual(len(viewset.ordering), 0, ERR) 
Example #4
Source File: tests.py    From scirius with GNU General Public License v3.0 5 votes vote down vote up
def test_002_list(self):
        for url, viewset, view_name in self.router.registry:
            if issubclass(viewset, mixins.ListModelMixin):
                self.http_get(reverse(view_name + '-list'))