示例#1
0
 def get_count(self, queryset, view=None):
     if view and getattr(view, 'page_cache', False):
         cache_key = view._default_generate_count_cache_key()
         count = view.cache.get(cache_key)
         if count is None:
             count = pagination._get_count(queryset)
             view.cache.set(cache_key, count, view.get_cache_age())
     else:
         count = pagination._get_count(queryset)
     return count
    def paginate_queryset(self, queryset, request, view=None):
        # Check if there are suggest queries in the queryset,
        # ``execute_suggest`` method shall be called, instead of the
        # ``execute`` method and results shall be returned back immediately.
        # Placing this code at the very start of ``paginate_queryset`` method
        # saves us unnecessary queries.
        is_suggest = getattr(queryset, '_suggest', False)
        if is_suggest:
            return queryset.execute_suggest().to_dict()

        self.count = _get_count(queryset)
        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True

        if self.count == 0 or self.offset > self.count:
            return []

        __queryset = queryset[self.offset:self.offset + self.limit].execute()
        self.facets = getattr(__queryset, 'aggregations', None)
        return list(queryset[self.offset:self.offset + self.limit])
def get_count(self, queryset):
    """Get count.

    :param self:
    :param queryset:
    :return:
    """
    if _get_count is None:
        return self.get_count(queryset)
    else:
        return _get_count(queryset)
示例#4
0
    def paginate_queryset(self, queryset, request, view=None):
        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        self.count = _get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True
        return list(queryset[self.offset:self.offset + self.limit])
示例#5
0
文件: views.py 项目: itatelier/dveta
    def paginate_queryset(self, queryset, request, view=None):
        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        self.count = _get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True
        return list(queryset[self.offset:self.offset + self.limit])
示例#6
0
    def paginate_queryset(self, queryset, request, view=None):
        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        self.count = _get_count(queryset)

        # when requesting all records, set the limit to one more than the queryset count
        if self.limit == 'all':
            self.limit = self.count + 1

        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True
        return list(queryset[self.offset:self.offset + self.limit])
示例#7
0
    def get_objects(self, instance):
        """
        Return a list of objects matching the faceted result.
        """
        view = self.context["view"]
        queryset = self.context["objects"]

        page = view.paginate_queryset(queryset)
        if page is not None:
            serializer = view.get_facet_objects_serializer(page, many=True)
            return OrderedDict([("count", _get_count(queryset)),
                                ("next", view.paginator.get_next_link()),
                                ("previous",
                                 view.paginator.get_previous_link()),
                                ("results", serializer.data)])

        serializer = view.get_serializer(queryset, many=True)
        return serializer.data
示例#8
0
    def paginate_queryset(self, queryset, request, view=None):
        # Modified from superclass method to skip an extra count call for ES
        # searches, since it provides .total in the response. (This is similar
        # to ESPaginator's behavior.)
        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        page = queryset[self.offset:self.offset + self.limit]
        if isinstance(page, Search):
            page = page.execute()
            self.count = page.hits.total
        else:
            self.count = self.count_override or pagination._get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True
        return list(page)
示例#9
0
    def get_objects(self, instance):
        """
        Return a list of objects matching the faceted result.
        """
        view = self.context["view"]
        queryset = self.context["objects"]

        page = view.paginate_queryset(queryset)
        if page is not None:
            serializer = view.get_facet_objects_serializer(page, many=True)
            return OrderedDict([
                ("count", _get_count(queryset)),
                ("next", view.paginator.get_next_link()),
                ("previous", view.paginator.get_previous_link()),
                ("results", serializer.data)
            ])

        serializer = view.get_serializer(queryset, many=True)
        return serializer.data
示例#10
0
    def paginate_queryset(self, queryset, request, view=None):
        self.limit = self.get_limit(request)
        # This is so we can always display pagination without having to specify
        # an upper limite.
        # if self.limit is None:
        #     return None

        self.offset = self.get_offset(request)
        self.count = pagination._get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True

        # If we have a limit, slice it
        if self.limit:
            return list(queryset[self.offset:self.offset + self.limit])
        # Otherwise only slice from the offset
        else:
            return list(queryset[self.offset:])
示例#11
0
    def paginate_queryset(self, queryset, request, view=None):
        self.limit = self.get_limit(request)
        # This is so we can always display pagination without having to specify
        # an upper limite.
        # if self.limit is None:
        #     return None

        self.offset = self.get_offset(request)
        self.count = pagination._get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True

        # If we have a limit, slice it
        if self.limit:
            return list(queryset[self.offset:self.offset + self.limit])
        # Otherwise only slice from the offset
        else:
            return list(queryset[self.offset:])
示例#12
0
    def paginate_queryset(self, queryset, request, view=None):
        self.total_duration = (queryset.aggregate(
            Sum('duration'))['duration__sum'])

        self.limit = self.get_limit(request)
        if self.limit is None:
            return None

        self.offset = self.get_offset(request)
        self.count = _get_count(queryset)
        self.request = request
        if self.count > self.limit and self.template is not None:
            self.display_page_controls = True

        if self.count == 0 or self.offset > self.count:
            return []

        queryset = queryset[self.offset:self.offset + self.limit]
        self.subtotal_duration = (queryset.aggregate(
            Sum('duration'))['duration__sum'])

        return list(queryset)
示例#13
0
 def paginate_queryset(self, queryset, request, view=None):
     self.limit = self.get_limit(request)
     # set the limit to one more than the queryset count
     if self.limit == 'all':
         self.limit = _get_count(queryset) + 1
     return super(OptionalLimitOffsetPagination, self).paginate_queryset(queryset, request, view)