Exemplo n.º 1
0
 def get_page_num(self, request):
     """
     获取页码
     :param request:
     :return:
     """
     if self.other_page_query_param:
         for param_name in self.other_page_query_param:
             if param_name in request.query_params:
                 return _positive_int(request.query_params[param_name], strict=True)
     page_num = request.query_params.get(self.page_query_param, 1)
     return _positive_int(page_num, strict=True)
Exemplo n.º 2
0
    def _validate_limit(self, request):
        value = _positive_int(request.query_params[self.limit_query_param],
                              strict=True)

        if value > self.max_limit:
            raise ValueError
        return value
Exemplo n.º 3
0
 def get_offset(self, query_params):
     try:
         return pagination._positive_int(
             query_params[self.offset_query_param],
         )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 4
0
 def get_offset(self, request):
     try:
         return _positive_int(
             ((int(request.query_params[self.offset_query_param]) - 1) *
              int(request.query_params[self.limit_query_param])), )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 5
0
 def get_offset(self, *args):
     if self._rql_offset is not None:
         try:
             return _positive_int(self._rql_offset)
         except ValueError:
             pass
     return 0
Exemplo n.º 6
0
    def decode_cursor(self, request):
        """
        Given a request with a cursor, return a `Cursor` instance.
        """
        # Determine if we have a cursor, and if so then decode it.
        encoded = request.query_params.get(self.cursor_query_param)
        if encoded is None:
            return None

        # The offset in the cursor is used in situations where we have a
        # nearly-unique index. (Eg millisecond precision creation timestamps)
        # We guard against malicious users attempting to cause expensive database
        # queries, by having a hard cap on the maximum possible size of the offset.
        OFFSET_CUTOFF = getattr(settings, 'CURSOR_PAGINATION_OFFSET_CUTOFF',
                                1000)

        try:
            querystring = b64decode(encoded.encode('ascii')).decode('ascii')
            tokens = parse_qs(querystring, keep_blank_values=True)

            offset = tokens.get('o', ['0'])[0]
            offset = _positive_int(offset, cutoff=OFFSET_CUTOFF)

            reverse = tokens.get('r', ['0'])[0]
            reverse = bool(int(reverse))

            position = tokens.get('p', [None])[0]
        except (TypeError, ValueError):
            raise NotFound(self.invalid_cursor_message)

        return Cursor(offset=offset, reverse=reverse, position=position)
Exemplo n.º 7
0
 def get_page_size(self, request):
     page_size = request.query_params.get(self.paginate_by_param,
                                          self.page_size)
     try:
         return _positive_int(page_size, cutoff=self.max_page_size)
     except ValueError:
         raise MalformedPageParameter()
Exemplo n.º 8
0
    def decode_cursor(self, request):
        """
        Given a request with a cursor, return a `Cursor` instance.
        """
        # Determine if we have a cursor, and if so then decode it.
        encoded = request.query_params.get(self.cursor_query_param)
        if encoded is None:
            return None

        # The offset in the cursor is used in situations where we have a
        # nearly-unique index. (Eg millisecond precision creation timestamps)
        # We guard against malicious users attempting to cause expensive database
        # queries, by having a hard cap on the maximum possible size of the offset.
        offset_cutoff = getattr(settings, 'CURSOR_PAGINATION_OFFSET_CUTOFF', 1000)

        try:
            querystring = b64decode(encoded.encode('ascii')).decode('ascii')
            tokens = parse_qs(querystring, keep_blank_values=True)

            offset = tokens.get('o', ['0'])[0]
            offset = _positive_int(offset, cutoff=offset_cutoff)

            reverse = tokens.get('r', ['0'])[0]
            reverse = bool(int(reverse))

            position = tokens.get('p', [None])[0]
        except (TypeError, ValueError):
            raise NotFound(self.invalid_cursor_message)

        return Cursor(offset=offset, reverse=reverse, position=position)
Exemplo n.º 9
0
    def decode_cursor(self, request):
        """
        Given a request with a cursor, return a `Cursor` instance.

        Differs from the standard CursorPagination to handle a tuple in the
        position field.
        """
        # Determine if we have a cursor, and if so then decode it.
        encoded = request.query_params.get(self.cursor_query_param)
        if encoded is None:
            return None

        try:
            querystring = b64decode(encoded.encode('ascii')).decode('ascii')
            tokens = parse.parse_qs(querystring, keep_blank_values=True)

            offset = tokens.get('o', ['0'])[0]
            offset = _positive_int(offset, cutoff=self.offset_cutoff)

            reverse = tokens.get('r', ['0'])[0]
            reverse = bool(int(reverse))

            # The difference. Don't get just the 0th entry: get all entries.
            position = tokens.get('p', None)
        except (TypeError, ValueError):
            raise NotFound(self.invalid_cursor_message)

        return Cursor(offset=offset, reverse=reverse, position=position)
    def decode_cursor(self, request):
        """
        Given a request with a cursor, return a `Cursor` instance.

        Differs from the standard CursorPagination to handle a tuple in the
        position field.
        """
        # Determine if we have a cursor, and if so then decode it.
        encoded = request.query_params.get(self.cursor_query_param)
        if encoded is None:
            return None

        try:
            querystring = b64decode(encoded.encode('ascii')).decode('ascii')
            tokens = urlparse.parse_qs(querystring, keep_blank_values=True)

            offset = tokens.get('o', ['0'])[0]
            # This was hard-coded until Django REST Framework 3.4.0.
            try:
                offset_cutoff = self.offset_cutoff
            except AttributeError:
                offset_cutoff = 1000
            offset = _positive_int(offset, cutoff=offset_cutoff)

            reverse = tokens.get('r', ['0'])[0]
            reverse = bool(int(reverse))

            # The difference. Don't get just the 0th entry: get all entries.
            position = tokens.get('p', None)
        except (TypeError, ValueError):
            raise NotFound(self.invalid_cursor_message)

        return Cursor(offset=offset, reverse=reverse, position=position)
Exemplo n.º 11
0
 def get_offset(self, request):
     """Return offset parameter."""
     try:
         return _positive_int(
             get_query_param(request, self.offset_query_param), )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 12
0
    def decode_cursor(self, request):
        """
        Given a request with a cursor, return a `Cursor` instance.
        """
        # Determine if we have a cursor, and if so then decode it.
        encoded = request.query_params.get(self.cursor_query_param)
        if encoded is None:
            return None

        try:
            reverse = False
            if encoded.startswith('-'):
                encoded = encoded[1:]
                reverse = True

            offset = 0
            parts = encoded.split('+', 1)
            if '-' in parts[0]:
                position = parts[0]
            else:
                position = None

            if len(parts) > 1:
                offset = _positive_int(parts[1], cutoff=self.offset_cutoff)

        except (TypeError, ValueError):
            raise NotFound(self.invalid_cursor_message)

        return Cursor(offset=offset, reverse=reverse, position=position)
Exemplo n.º 13
0
    def paginate_queryset(self, queryset, request, view=None):
        """
        Paginate a queryset according to start/end values (zero-indexed) in
        `range` header, or `None` if pagination is not properly configured.
        """
        self.count = self.get_count(queryset)
        self.start_index = 0
        self.end_index = self.start_index + self.page_size - 1

        # TODO: this logic is repeated below...
        if self.end_index > self.count - 1:
            self.end_index = self.count - 1 if self.count else 0

        range_string = request.GET.get(self.range_query_param)

        if range_string:
            try:
                page_range = json.loads(range_string)
            except json.JSONDecodeError:
                return None

            if len(page_range) != 2:
                return None

            self.start_index, self.end_index = [
                pagination._positive_int(x) for x in page_range
            ]

        if self.end_index > self.count - 1:
            self.end_index = self.count - 1 if self.count else 0

        if self.start_index > self.end_index:
            self.start_index = self.end_index

        return list(queryset[self.start_index:self.end_index + 1])
Exemplo n.º 14
0
 def get_offset(self, request):
     try:
         return _positive_int(
             request.query_params[self.offset_query_param],
         )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 15
0
 def get_page_size(self, request):
     if self.page_size_query_param:
         try:
             return _positive_int(
                 request.query_params[self.page_size_query_param], strict=False, cutoff=self.max_page_size)
         except (KeyError, ValueError):
             pass
     return self.page_size
Exemplo n.º 16
0
 def get_offset(self, request):
     try:
         return _positive_int(request.query_params[self.offset_query_param])
     except KeyError:
         return 0
     except ValueError:
         raise ValidationError({'detail': f'Offset parameter must be >= 0'},
                               code=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 17
0
    def get_page_size(self, request):
        if self.header_page_size_param in request.META.keys():
            return pagination._positive_int(
                request.META[self.header_page_size_param],
                strict=True,
                cutoff=self.max_page_size)

        elif self.page_size_query_param:
            try:
                return pagination._positive_int(
                    request.query_params[self.page_size_query_param],
                    strict=True,
                    cutoff=self.max_page_size)
            except (KeyError, ValueError):
                pass

        return self.page_size
Exemplo n.º 18
0
 def get_limit(self, request):
     if self.limit_query_param:
         try:
             return drf_pagination._positive_int(
                 request.query_params[self.limit_query_param])
         except (KeyError, ValueError):
             pass
     return self.default_limit
Exemplo n.º 19
0
 def get_offset(self, request):
     try:
         return _positive_int(
             request.query_params[self.offset_query_param]
             if self.offset_query_param in request.query_params else
             request.data[self.offset_query_param], )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 20
0
 def get_page_size(self, request):
     try:
         return _positive_int(
             request.query_params[self.page_size_query_param],
             strict=True,
         )
     except (KeyError, ValueError):
         return self.page_size
Exemplo n.º 21
0
 def get_offset(self, request):
     try:
         return _positive_int(
             request.query_params[self.offset_query_param],
             cutoff=((self.limit * self.max_pages) - self.limit)
         )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 22
0
 def get_offset(self, request):
     try:
         return pagination._positive_int(
             request.query_params[self.offset_query_param], )
     except KeyError:
         return 0
     except ValueError:
         raise BadRequest()
Exemplo n.º 23
0
 def get_offset(self, request):
     """Return offset parameter."""
     try:
         return _positive_int(
             get_query_param(request, self.offset_query_param),
         )
     except (KeyError, ValueError):
         return 0
Exemplo n.º 24
0
 def get_limit(self, *args):
     if self._rql_limit is not None:
         try:
             return _positive_int(self._rql_limit,
                                  strict=False,
                                  cutoff=self.max_limit)
         except ValueError:
             pass
     return self.default_limit
Exemplo n.º 25
0
 def get_limit(self, request):
     ret = pagination._positive_int(
         request.query_params.get(self.limit_query_param, self.default_limit),
         strict=False,
         cutoff=self.max_limit,
     )
     if ret == 0:
         return self.max_limit
     return ret
Exemplo n.º 26
0
 def get_page_size_2(self, request):
     try:
         return _positive_int(
             request.query_params[self.page_size_query_param],
             strict=True,
             cutoff=self.max_page_size
         )
     except (KeyError, ValueError):
         return None
Exemplo n.º 27
0
 def _get_offset_from_request(self, request):
     try:
         offset = _positive_int(
             request.query_params[self.offset_query_param])
     except (KeyError, ValueError):
         offset = 0
     if offset < 1:
         offset = 1
     return offset
Exemplo n.º 28
0
    def get_page_number(self, request, view=None):
        """获取页码

        :param request: 请求对象
        :param view: viewset实例
        :return: 页码
        """
        return pagination._positive_int(request.query_params.get(
            self.page_query_param, 1),
                                        strict=True)
Exemplo n.º 29
0
 def get_limit(self, request):
     ret = pagination._positive_int(
         request.query_params.get(self.limit_query_param,
                                  self.default_limit),
         strict=False,
         cutoff=self.max_limit,
     )
     if ret == 0:
         return self.max_limit
     return ret
 def get_limit(self):
     if self.limit_query_param:
         try:
             return _positive_int(
                 self.request.query_params[self.limit_query_param],
                 strict=True,
                 cutoff=self.max_limit)
         except (KeyError, ValueError):
             return None
     return None
Exemplo n.º 31
0
    def get_limit(self, query_params):
        if self.limit_query_param:
            try:
                return pagination._positive_int(
                    query_params[self.limit_query_param],
                    cutoff=self.max_limit)
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 32
0
    def get_limit(self, request):
        if self.limit_query_param:
            try:
                return _positive_int(
                    request.query_params[self.limit_query_param],
                    cutoff=self.max_limit)
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 33
0
 def get_page_number(self, request, view=None):
     try:
         page_query = pagination._positive_int(
             request.query_params.get(self.page_query_param, 1),
             strict=True
         )
         if page_query > unlimit_number:
             raise exceptions.ValidationError('Field Invalid')
         return page_query
     except Exception:
         raise exceptions.ValidationError('Field Invalid')
 def get_offset(self, request):
     try:
         offset = _positive_int(
             request.query_params[self.offset_query_param], )
         if offset == 0:
             offset = 1
         if offset > 1:
             offset = (offset - 1) * self.default_limit + 1
         return offset - 1
     except (KeyError, ValueError):
         return 0
Exemplo n.º 35
0
    def get_limit(self, query_params):
        if self.limit_query_param:
            try:
                return pagination._positive_int(
                    query_params[self.limit_query_param],
                    cutoff=self.max_limit
                )
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 36
0
    def get_limit(self, request):
        if self.limit_query_param:
            try:
                return pagination._positive_int(
                    request.query_params[self.limit_query_param],
                    strict=True,
                    cutoff=self.max_limit)
            except (KeyError, ValueError) as e:
                pass

        return self.default_limit
Exemplo n.º 37
0
    def get_limit(self, request):
        if self.limit_query_param:
            try:
                return _positive_int(
                    request.query_params[self.limit_query_param],
                    cutoff=self.max_limit
                )
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 38
0
    def get_limit(self, request):
        """Return limit parameter."""
        if self.limit_query_param:
            try:
                return _positive_int(get_query_param(request,
                                                     self.limit_query_param),
                                     strict=True,
                                     cutoff=self.max_limit)
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 39
0
 def get_page_size(self, request):
     if self.page_size_query_param:
         try:
             page_size = pagination._positive_int(
                 request.query_params[self.page_size_query_param],
                 strict=True,
                 cutoff=self.max_page_size)
             self.page_size = page_size
             return self.page_size
         except (KeyError, ValueError):
             pass
     return self.page_size
Exemplo n.º 40
0
    def get_page_size(self, request):
        if self.page_size_query_param:
            try:
                return _positive_int(
                    request.query_params[self.page_size_query_param],
                    strict=True,
                    cutoff=self.max_page_size
                )
            except (KeyError, ValueError):
                pass

        return self.page_size
Exemplo n.º 41
0
    def get_limit(self, request):
        """Return limit parameter."""
        if self.limit_query_param:
            try:
                return _positive_int(
                    get_query_param(request, self.limit_query_param),
                    strict=True,
                    cutoff=self.max_limit
                )
            except (KeyError, ValueError):
                pass

        return self.default_limit
Exemplo n.º 42
0
    def get_page_size(self, request):
        if self.page_size_query_param:
            cutoff = self.max_page_size
            if request.query_params.get('format', '').lower() == 'xlsx':
                cutoff = 50000
            try:
                return _positive_int(
                    request.query_params[self.page_size_query_param],
                    strict=True, cutoff=cutoff
                )
            except (KeyError, ValueError):
                pass

        return self.page_size
Exemplo n.º 43
0
 def adjust_offset(self, url, page_offset):
     if url is None:
         return
     (scheme, netloc, path, query, fragment) = urlparse.urlsplit(force_str(url))
     query_dict = QueryDict(query)
     try:
         offset = pagination._positive_int(
             query_dict[self.offset_query_param],
         )
     except (KeyError, ValueError):
         pass
     else:
         if offset > page_offset:
             url = replace_query_param(url, self.offset_query_param, max(0, offset - self.overlapping))
         elif offset < page_offset:
             url = replace_query_param(url, self.offset_query_param, offset + self.overlapping)
     return url
Exemplo n.º 44
0
 def get_page_size(self, request):
     """
     this is overrided to allow 0 as a page_size.
     if page_size=0, we will set page_size as max_page_size.
     """
     page_size = self.page_size
     if self.page_size_query_param:
         try:
             page_size = _positive_int(
                 request.query_params[self.page_size_query_param],
                 strict=False,
                 cutoff=self.max_page_size
             )
         except (KeyError, ValueError):
             pass
     if page_size == 0:
         page_size = self.max_page_size
     return page_size