Пример #1
0
def get_pagination(cy,size,f,c_or_f):
    cquery = ""
    if cy == "":
        if c_or_f == 'c':
            cquery = "MATCH (n:sample) RETURN count(n) AS tot"
        else:
            cquery = "MATCH (n:file) RETURN count(n) AS tot"
        res = process_cquery_http(cquery)
        calcs = pagination_calcs(res[0]['tot'],f,size,c_or_f)
        return Pagination(count=calcs[2], sort=calcs[4], fromNum=f, page=calcs[1], total=calcs[3], pages=calcs[0], size=size)
    else:
        if '"op"' in cy:
            if c_or_f == 'c':
                cquery = build_cypher(cy,"null","null","null","c_pagination")
            else:
                cquery = build_cypher(cy,"null","null","null","f_pagination")
        else:
            if c_or_f == 'c':
                cquery = build_adv_cypher(cy,"null","null","null","c_pagination")
                cquery = cquery.replace('WHERE "',"WHERE ") # where does this phantom quote come from?!
            else:
                cquery = build_adv_cypher(cy,"null","null","null","f_pagination")
                cquery = cquery.replace('WHERE "',"WHERE ") # where does this phantom quote come from?!
                
        res = process_cquery_http(cquery)
        calcs = pagination_calcs(res[0]['tot'],f,size,c_or_f)
        return Pagination(count=calcs[2], sort=calcs[4], fromNum=f, page=calcs[1], total=calcs[3], pages=calcs[0], size=size)
Пример #2
0
def get_employees(request: Request):
    # TODO add a wrapper params handler that allows you to specify the type
    # and handles validation errors automatically.
    page_size = request.params.get('page[size]')
    if page_size is None:
        page_size = 10
    else:
        page_size = int(page_size)  # error thrown here

    page_after = request.params.get('page[after]')

    query = select([db.employee
                    ]).limit(page_size + 1).order_by(db.employee.c.id.asc())
    if page_after is not None:
        query = query.where(db.employee.c.id > id_from_cursor(page_after))

    employees = list(db.execute(Employee, query))
    next_id = None
    if len(employees) > page_size:
        next_id = employees[-1].id
        employees = employees[:-1]

    return Envelope(
        employees,
        Pagination(next=None if next_id is None else cursor_from_id(next_id)))
Пример #3
0
	def generate(self, uri, paginate_until=None, after=None):
		method = 'GET'
		response_data = []
		url = self._base_url + uri
		response = requests.request(
			method = method,
			url = url,
			params = {'after': after},
			auth = self._auth,
			verify = True)
		print(response)
		print(response.text)
		if isinstance(response.json(), list):
			response_data += response.json()
		else:
			yield response.json()
		if paginate_until is None:
			for r in response_data:
				yield r
		else:
			db_session = get_db_session()
			pagination_key = paginate_until['key']
			pagination_condition = paginate_until['condition']
			if len(response_data) == 0:
				return
			for r in response_data:
				if pagination_condition(r[pagination_key]):
					yield r
			while pagination_condition(response_data[-1][pagination_key]):
				if 'CB-AFTER' in response.headers:
					time.sleep(0.25) # To avoid rate limit
					after = response.headers['CB-AFTER']
					response = requests.request(
						method=method, url=url, auth=self._auth, params={'after': after})
					response_data = response.json()
					if not isinstance(response_data, list):
						response_data = [response_data]
					if len(response_data) == 0:
						break
					for r in response_data:
						if pagination_condition(r[pagination_key]):
							yield r
					try:
						# Record pagination info in DB
						pagination = Pagination(
							account = get_account_from_base_url(self._base_url),
							product_id = get_product_id_from_uri(uri),
							url = url,
							start_time = response_data[-1]['created_at'],
							end_time = response_data[0]['created_at'],
							cursor_before = response.headers['CB-BEFORE'],
							cursor_after = response.headers['CB-AFTER'])
						db_session.add(pagination)
						db_session.commit()
					except Exception as e:
						print(str(e))
						# Let this go since it's just extra info that is nice to have
				else:
					break
			db_session.remove()
Пример #4
0
def index():
    boards = Board.all()
    # you can set the quantity of unreplied_post by list slicing
    unreplied_posts = [p for p in Post.cache_all() if len(p.replies()) == 0]
    board_name = request.args.get('tab', '全部')
    # current_page is str passed from url
    # page 1 by default
    current_page = int(request.args.get('page', 1))
    if board_name == '全部':
        # top_posts = Post.find_all(top=True)
        # ordinary_posts = Post.find_all(top=False)
        # for performance
        ordinary_posts = Post.cache_all()
        top_posts = find_all_and_separate(ordinary_posts, top=True)
        top_posts = sorted(top_posts, key=lambda post: post.ct, reverse=True)
        ordinary_posts = sorted(ordinary_posts,
                                key=lambda post: post.ct,
                                reverse=True)
        sorted_posts = top_posts + ordinary_posts
    else:
        board = Board.find_by(name=board_name)
        if board is not None:
            # posts = Post.find_all(board_id=board.id)
            # top_posts = [p for p in posts if p.top == True]
            # ordinary_posts = [p for p in posts if p.top == False]
            # for performance
            ordinary_posts = Post.find_all(board_id=board.id)
            top_posts = find_all_and_separate(ordinary_posts, top=True)
            top_posts = sorted(top_posts,
                               key=lambda post: post.ct,
                               reverse=True)
            ordinary_posts = sorted(ordinary_posts,
                                    key=lambda post: post.ct,
                                    reverse=True)
            sorted_posts = top_posts + ordinary_posts
            # print('sorted_posts', len(sorted_posts))
        else:
            # in order to keep the consistency down here
            # we give the sorted_posts empty list
            sorted_posts = []
    page = Pagination(len(sorted_posts), current_page)
    data = page.data_by_page(sorted_posts)
    return render_template('index.html',
                           data=data,
                           page=page,
                           unreplied_posts=unreplied_posts,
                           boards=boards)
Пример #5
0
def posts(page):
    count = db.session.query(Post).count()
    offset = (page - 1) * PER_PAGE
    posts = Post.query.limit(PER_PAGE).offset(offset)

    if not posts and page != 1:
        abort(404)
    pagination = Pagination(page, PER_PAGE, count)
    return render_template('posts.html', pagination=pagination, posts=posts)
Пример #6
0
    def get_pagination(self):
        """
        function parser the pagination xml file
        """

        print "getting pagination"

        current_page = self.pagination.find('current_page').text
        last_page = self.pagination.find('last_page').text
        per_page = self.pagination.find('per_page').text
        has_more_pages = self.pagination.find('has_more_pages').text
        first_item = self.pagination.find('first_item').text
        last_item = self.pagination.find('last_item').text
        total = self.pagination.find('total').text
        count = self.pagination.find('count').text

        return Pagination(current_page, last_page, per_page, has_more_pages,
                          first_item, last_item, total, count)
Пример #7
0
def index():
    """
    Index.
    """
    tid = request.args.get('t', None)
    page = int(request.args.get('p', 1))
    start = (page - 1) * PAGE_COUNT
    if tid is not None:
        all_posts = Post.find_tags(tids=tid)
    else:
        all_posts = Post.all()
    count = Post.count(all_posts)
    all_posts = Post.page_post(all_posts, skip=start, limit=PAGE_COUNT)
    pagination = Pagination(page, PAGE_COUNT, count)
    return render_template('blog/index.html',
                           posts=all_posts,
                           pagination=pagination,
                           tags=Tags.all())
Пример #8
0
    def get_pagination(self):
        """
        function parser the pagination xml file
        """
        for pagination in self.meta:
            total = pagination.find('total').text
            count = pagination.find('count').text
            per_page = pagination.find('per_page').text
            current_page = pagination.find('current_page').text
            total_pages = pagination.find('total_pages').text

            links = []

            for child in pagination:
                if child.tag == 'links':
                    next = child.find('next').text
                    links.append(Links(next))
            return Pagination(total, count, per_page, current_page,
                              total_pages, links)