def get(self, tag_name): page = int(self.get_argument('page', 1)) sql = 'SELECT b.blog_id FROM blogs_tag AS a, blogs_blog_tag AS b WHERE a.name = %s AND a.id = b.tag_id' query = self.db.query(sql, (tag_name,)) blog_ids = ','.join([str(item['blog_id']) for item in query]) count_sql = "SELECT count(*) FROM blogs_blog where id in (%s)" % blog_ids query_sql = "SELECT * FROM blogs_blog where id in (%s) ORDER BY created DESC" % blog_ids paginator = Paginator(db=self.db, count_sql=count_sql, query_sql=query_sql, per_page=PAGE_NUM) try: blogs = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. blogs = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. blogs = paginator.page(paginator.num_pages) for item in blogs: item['detail_url'] = '/blogs/%s/' % item.id content = util.filter_html(item.content) if item.content else "" item['brief'] = content[:100] self.render("blogs/index.html", blogs=blogs, selected_category=None, selected_tags=[tag_name], **get_cat_tag(self))
def manage_user(page = 1): """ 查看用户信息 """ log.info("manage_user") user = request.get_cookie('user',secret="chermong") user = db.user.find_one({'name':user}) users = list(db.user.find()) paginator = Paginator(users,10,0) try: users = paginator.page(page) except (EmptyPage, InvalidPage): users = paginator.page(paginator.num_pages) page_range,num_pages = paginator.page_range,paginator.num_pages show_firstpage = False if int(page) == 1 else True show_lastpage = False if int(page) == num_pages else True show_pages = 5 if num_pages <= show_pages:#当数量不足时 newpage_range = page_range else: newpage_range = list() if int(page) + show_pages > num_pages:#用于显示后几位 for pagenum in range(num_pages - show_pages + 1,num_pages + 1): newpage_range.append(pagenum) else: for pagenum in range(int(page),int(page)+show_pages): newpage_range.append(pagenum) return jinja2_template('manage-user.html',user = user,app = chermongapp, users = users,show_firstpage=show_firstpage,show_lastpage=show_lastpage, num_pages=num_pages,page_range=newpage_range)
def show_entries(view,pagin,page): if view =='books': entries_all = db_session.query(Book).all() else : entries_all = db_session.query(Author).all() paginator = Paginator(entries_all,pagin) try: entries = paginator.page(page) except PageNotAnInteger: entries = paginator.page(1) except EmptyPage: entries = paginator.page(paginator.num_pages) return render_template('show_entries.html', entries=entries, entries_per_page=pagin,view_mode=view, config=app.config)
def edit(page = 1): """ 进行互评 """ log.info("edit") user = request.get_cookie('user',secret="chermong") user = db.user.find_one({'name':user}) schoolid,option = request.query.get('schoolid'),request.query.get('option') db.assessment.update({'schoolid':user['schoolid'],"pro-others.schoolid":schoolid},{"$set":{"pro-others.$.option":option}}) pro_others = list(db.assessment.find_one({'schoolid':user['schoolid']},fields = ['pro-others'])['pro-others']) options = list(db.options.find())#因为返回的是一个类似的list的cursor,但是它只能循环一次,所以将它转换为list paginator = Paginator(pro_others,10,0) pro_others = paginator.page(page) page_range,num_pages = paginator.page_range,paginator.num_pages show_firstpage = False if int(page) == 1 else True show_lastpage = False if int(page) == num_pages else True show_pages = 5 if num_pages <= show_pages:#当数量不足时 newpage_range = page_range else: newpage_range = list() if int(page) + show_pages > num_pages:#用于显示后几位 for pagenum in range(num_pages - show_pages + 1,num_pages + 1): newpage_range.append(pagenum) else: for pagenum in range(int(page),int(page)+show_pages): newpage_range.append(pagenum) return jinja2_template('edit-assessment.html',user = user,app = chermongapp, options = options,pro_others = pro_others,show_firstpage=show_firstpage, show_lastpage=show_lastpage,num_pages=num_pages,page_range=newpage_range)
def get_results(self, request): paginator = Paginator(self.query_set, self.list_per_page) # Get the number of objects, with admin filters applied. result_count = paginator.count # Get the total number of objects, with no admin filters applied. # Perform a slight optimization: Check to see whether any filters were # given. If not, use paginator.hits to calculate the number of objects, # because we've already done paginator.hits and the value is cached. if not self.query_set._filtered: full_result_count = result_count else: full_result_count = self.root_query_set.count() can_show_all = result_count <= 1000 multi_page = result_count > self.list_per_page # Get the list of objects to display on this page. if (self.show_all and can_show_all) or not multi_page: result_list = self.query_set.fetch(1000) else: try: result_list = paginator.page(self.page_num + 1).object_list except: raise self.result_count = result_count self.full_result_count = full_result_count self.result_list = result_list self.can_show_all = can_show_all self.multi_page = multi_page self.paginator = paginator
def show_recipes(tag, start='a', page=0, refresh=False): recipes = evernote_wrapper.get_recipes(tag, refresh) logging.info('%s recipes', len(recipes)) paginator = Paginator(recipes, 'title', start, int(page), 6) tagurl = '/tag/%s/' % tag start += '/' return render_template('recipes.html', recipes=paginator.page(), tagurl=tagurl, start=start, page=int(page))
def get_results(self, request): paginator = Paginator(self.query_set, self.list_per_page) # Get the number of objects, with admin filters applied. result_count = paginator.count # Get the total number of objects, with no admin filters applied. # Perform a slight optimization: Check to see whether any filters were # given. If not, use paginator.hits to calculate the number of objects, # because we've already done paginator.hits and the value is cached. if not self.query_set._filtered: full_result_count = result_count else: full_result_count = self.root_query_set.count() can_show_all = result_count <= 1000 multi_page = result_count > self.list_per_page # Get the list of objects to display on this page. if (self.show_all and can_show_all) or not multi_page: result_list = self.query_set.fetch(1000) else: try: result_list = paginator.page(self.page_num+1).object_list except: raise self.result_count = result_count self.full_result_count = full_result_count self.result_list = result_list self.can_show_all = can_show_all self.multi_page = multi_page self.paginator = paginator
def show_tags(start='a', page=0, refresh=False): tags = evernote_wrapper.get_tags(refresh) paginator = Paginator(tags, 'name', start, int(page), 6) start += '/' return render_template('tags.html', tags=paginator.page(), tagurl='/', start=start, page=int(page))
def show_list(self): plist = db.GqlQuery("SELECT * FROM Page ORDER BY url ASC") for item in plist: item.id = item.key().id() # paginate pg = Paginator(plist, PAGESIZE) logging.info(pg) pi = self.request.get("pg") try: pgdata = pg.page(pi) except PageNotAnInteger: # If page is not an integer, deliver first page. pgdata = pg.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. pgdata = pg.page(pg.num_pages) logging.info(pgdata) payload = { 'list': pgdata } self.respond('page_list.html', payload)
class PaginatorTestCase(unittest.TestCase): """Unit tests for our custom paginator.""" def setUp(self): self.paginator = Paginator(range(10), 5) def test_small_page_number(self): """Numbers less than 1 should be rounded up to 1.""" page = self.paginator.page(-1) self.assertEqual(1, page.number) def test_large_page_number(self): """Numbers greater than the maximum n should be rounded down to n.""" page = self.paginator.page(3) self.assertEqual(2, page.number) def test_invalid_page_number(self): """Numbers that can't be converted to integers should default to 1.""" page = self.paginator.page("Hello!") self.assertEqual(1, page.number)
def get(self): page = int(self.get_argument('page', 1)) count_sql = "SELECT count(*) FROM blogs_blog" query_sql = "SELECT * FROM blogs_blog ORDER BY created DESC" paginator = Paginator(db=self.db, count_sql=count_sql, query_sql=query_sql, per_page=PAGE_NUM) try: blogs = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. blogs = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. blogs = paginator.page(paginator.num_pages) for item in blogs: item['detail_url'] = '/blogs/%s/' % item.id content = util.filter_html(item.content) if item.content else "" item['brief'] = content[:100] self.render("blogs/index.html", blogs=blogs, selected_category=None, selected_tags=[], **get_cat_tag(self))
def lookup(page = 1): """ 显示查看页面 """ log.info("lookup") user = request.get_cookie('user',secret="chermong") user = db.user.find_one({'name':user}) options = list(db.options.find()) origi_option = {i.get('option'):0 for i in options} students = OrderedDict()#利用OrderedDict实现快速查找 for i in db.user.find({'role':'student'}): user_info = {'name':i.get('name'),'schoolid':i.get('schoolid'),'sex':i.get('sex')} user_info.update(origi_option) students[i.get('schoolid')] = user_info for i in db.assessment.find(): for j in i.get('pro-others'): if j.get('option') in origi_option.keys(): students[j.get('schoolid')][j.get('option')]+=1 students = list(students.values()) #students = [{'name':u'郭猛','schoolid':'101110312','sex':u'男',u'优':14,u'良':12,u'一般':5}] #注意:上面注释的写法,eg:u,数字的使用 paginator = Paginator(students,10,0) students = paginator.page(page) page_range,num_pages = paginator.page_range,paginator.num_pages show_firstpage = False if int(page) == 1 else True show_lastpage = False if int(page) == num_pages else True show_pages = 5 if num_pages <= show_pages:#当数量不足时 newpage_range = page_range else: newpage_range = list() if int(page) + show_pages > num_pages:#用于显示后几位 for pagenum in range(num_pages - show_pages + 1,num_pages + 1): newpage_range.append(pagenum) else: for pagenum in range(int(page),int(page)+show_pages): newpage_range.append(pagenum) return jinja2_template('lookup.html',user = user,app = chermongapp, options = options,students = students,show_firstpage=show_firstpage, show_lastpage=show_lastpage,num_pages=num_pages,page_range=newpage_range)
def test_paginator(): items = range(1, 491) p = Paginator(items, page=1, per_page=20) assert p.page == 1 assert not p.has_prev assert p.has_next assert p.total_items == 490 assert p.total_pages == 25 assert p.next_page_number == 2 assert list(p.pages) == [1, 2, 3, 4, None, 24, 25] assert p.pages_range == (0, 19) p.page = 10 assert p.page == 10 assert p.has_prev assert list(p.pages) == [1, 2, None, 7, 8, 9, 10, 11, 12, 13, None, 24, 25] assert list(p) == list(range(181, 201)) assert Paginator(range(5))
def paginate(objects_list, request): paginator = Paginator(objects_list, 5) page = request.GET.get('page') contacts = paginator.page(page) return contacts
def paginate(objects_list,request): paginator = Paginator(objects_list,5) page = request.GET.get('page') contacts = paginator.page(page) return contacts