def to_ordered_collection_page(queryset, remote_id, id_only=False, page=1, pure=False, **kwargs): """serialize and pagiante a queryset""" paginated = Paginator(queryset, PAGE_LENGTH) activity_page = paginated.get_page(page) if id_only: items = [s.remote_id for s in activity_page.object_list] else: items = [s.to_activity(pure=pure) for s in activity_page.object_list] prev_page = next_page = None if activity_page.has_next(): next_page = "%s?page=%d" % (remote_id, activity_page.next_page_number()) if activity_page.has_previous(): prev_page = "%s?page=%d" % (remote_id, activity_page.previous_page_number()) return activitypub.OrderedCollectionPage( id="%s?page=%s" % (remote_id, page), partOf=remote_id, orderedItems=items, next=next_page, prev=prev_page, )
def to_ordered_collection_page(self, queryset, remote_id, \ id_only=False, min_id=None, max_id=None): ''' serialize and pagiante a queryset ''' # TODO: weird place to define this limit = 20 # filters for use in the django queryset min/max filters = {} if min_id is not None: filters['id__gt'] = min_id if max_id is not None: filters['id__lte'] = max_id page_id = self.page(min_id=min_id, max_id=max_id) items = queryset.filter( **filters ).all()[:limit] if id_only: page = [s.remote_id for s in items] else: page = [s.to_activity() for s in items] return activitypub.OrderedCollectionPage( id='%s%s' % (remote_id, page_id), partOf=remote_id, orderedItems=page, next='%s%s' % (remote_id, self.next_page(items)), prev='%s%s' % (remote_id, self.prev_page(items)) ).serialize()
def to_ordered_collection_page(queryset, remote_id, id_only=False, page=1): ''' serialize and pagiante a queryset ''' paginated = Paginator(queryset, PAGE_LENGTH) activity_page = paginated.page(page) if id_only: items = [s.remote_id for s in activity_page.object_list] else: items = [s.to_activity() for s in activity_page.object_list] prev_page = next_page = None if activity_page.has_next(): next_page = '%s?page=%d' % (remote_id, activity_page.next_page_number()) if activity_page.has_previous(): prev_page = '%s?page=%d' % \ (remote_id, activity_page.previous_page_number()) return activitypub.OrderedCollectionPage( id='%s?page=%s' % (remote_id, page), partOf=remote_id, orderedItems=items, next=next_page, prev=prev_page ).serialize()