#!/usr/bin/env python
import tornado.web
LIST_INFO = [
{'username':'taochen','email':'540123685@qq.com'}
]
for i in range(99):
temp = {'username':'qwee'+str(i),"email":str(i)+"ewq"}
LIST_INFO.append(temp)
class Paging:
def __init__(self,base_url,current_page,len_info,page_info):
try:
current_page = int(current_page)
except:
current_page = 1
if current_page < 1:
current_page = 1
self.current_page = current_page
self.len_info = len_info
self.page_info =page_info
self.base_url = "{}{}{}".format('/',base_url,'/')
def all_page(self):
all_pager, c = divmod (self.len_info, self.page_info )
if c > 0:
all_pager += 1
return all_pager
def start_page(self):
return (self.current_page - 1)*self.page_info
def end_page(self):
return (self.current_page)*self.page_info
def paging(self):
list_page = []
if self.all_page() < 11:
s = 1
t = self.all_page()
else:
if self.current_page < 6:
s = 1
t = 11
else:
if (self.current_page + 5) > self.all_page():
s = self.all_page() - 10
t = self.all_page()
else:
s = self.current_page - 5
t = self.current_page + 5
temp = "<a href={}>首页</a>".format (self.base_url )
if self.current_page == 1:
temp = "<a href='javascript:viod(0)'>首页</a>"
else:
temp = "<a href={}>首页</a>".format( self.base_url )
list_page.append ( temp )
if self.current_page == 1:
temp = "<a href='javascript:viod(0)'>上一页</a>"
else:
temp = "<a href={}{}>上一页</a>".format( self.base_url,self.current_page - 1 )
list_page.append ( temp )
for i in range ( s, t + 1 ):
if i == self.current_page:
temp = "<a class='active' href={}{}>{}</a>".format (self.base_url ,i, i )
else:
temp = "<a href={}{}>{}</a>".format (self.base_url, i, i )
list_page.append ( temp )
if self.current_page >= self.all_page():
temp = "<a href='javascript:viod(0)'>下一页</a>"
else:
temp = "<a href={}{}>下一页</a>".format( self.base_url,self.current_page + 1 )
list_page.append ( temp )
if self.current_page == self.all_page():
temp = "<a href='javascript:viod(0)'>尾页</a>"
else:
temp = "<a href={}{}>尾页</a>".format( self.base_url,self.all_page() )
list_page.append ( temp )
jump = """<input id="paging" type ='text'/><a name= '{}' onclick="Jump('{}',this)">Go</a>""".format(self.all_page(),self.base_url)
script = """
<script>
function Jump(self,ths) {
var ret = ths.previousElementSibling.value;
if(ret.trim().length > 0){
if( ret > parseInt(ths.name)){
ret = parseInt(ths.name);
}
location.href = self+ret;
}
}
</script>
"""
list_page.append (jump)
list_page.append (script)
str_page = "".join ( list_page )
return str_page
class IndexHandler(tornado.web.RequestHandler):
def get(self,page):
current_obj = Paging("index",page,len(LIST_INFO),5)
current_list = LIST_INFO[current_obj.start_page():current_obj.end_page()]
self.render('home/index.html',list_info = current_list,current_page = page, str_page = current_obj.paging())
def post(self, page):
user = self.get_argument("username")
email =self.get_argument("email")
temp = {'username':user,'email':email}
LIST_INFO.append(temp)
self.redirect('/index/'+page)