-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.py
115 lines (93 loc) · 3.37 KB
/
Base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import json as sjson
from aiohttp import web
from peewee import SqliteDatabase
import models
__author__ = 'SunCoder'
APP_PATH = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(APP_PATH,'fanhao.db')
PHOTO_PATH = os.path.join(APP_PATH, 'photos')
STATIC_PATH = os.path.join(APP_PATH, 'static')
def initsys():
if not os.path.exists(PHOTO_PATH):
os.mkdir(PHOTO_PATH)
if not os.path.exists(DB_PATH):
db = SqliteDatabase(os.path.join(APP_PATH, 'fanhao.db'))
db.create_table(models.fanhao)
db.close()
def json(code, msg='', data='', callback=None):
json_data = {
'code': code,
'msg': msg,
'data': data
}
return json_data
def rjson(code, msg='', data='', callback=None):
json_data = {
'code': code,
'msg': msg,
'data': data
}
jsonstr = sjson.dumps(json_data)
return web.Response(body=bytes(jsonstr, encoding="utf-8"),content_type='text/html')
class Pager():
url = ''
total = 0
index = 1
size = 15
pages = []
def __init__(self, total, index, size):
'''
分页函数
:param count: 总数
:param index: 页码
:param size: 页容量
:return: object paging
'''
self.total = total
self.index = index
self.size = size
self.count, tail = divmod(total, size)
if tail is not 0: self.count += 1
def render(self):
self.pages.clear()
self.pages.append('<nav><ul class="pagination">')
rag = range(0, self.count)
if self.count > 10:
if self.index <= 5:
rag = rag[0: 10]
elif self.index > 5:
if self.count - self.index < 5:
rag = rag[-10:]
else:
rag = rag[self.index - 5:self.index + 5]
if self.index == 1:
self.pages.append('<li class="disabled"><span>第1页</a></li>')
self.pages.append('<li class="disabled"><span>«</a></li>')
else:
self.pages.append('<li><a href="javascript:goPage(%s);">第1页</a></li>' % 1)
self.pages.append('<li><a href="javascript:goPage(%s);">«</a></li>' % (self.index - 1))
for i in rag:
v = i + 1
if self.index == v:
self.pages.append('<li class="active"><span>%s</span></li>' % (v if v > 9 else ('0%s' % v)))
else:
self.pages.append('<li><a href="javascript:goPage(%s);">%s</a></li>' % (v, (v if v > 9 else ('0%s' % v))))
if self.index == self.count:
self.pages.append('<li class="disabled"><span>»</span></li>')
self.pages.append('<li class="disabled"><span>第%s页</span></li>' % self.count)
else:
self.pages.append('<li><a href="javascript:goPage(%s);">»</a></li>' % (self.index + 1))
self.pages.append('<li><a href="javascript:goPage(%s);">第%s页</a></li>' % (self.count, self.count))
self.pages.append('<li><span>共%s个</span></li></ul></nav>' % self.total)
return "".join(self.pages)
def __str__(self):
return self.render()
if __name__ == '__main__':
print(Pager(202, 10, 4))
print(Pager(202, 2, 4))
print(Pager(202, 50, 4))
print(Pager(10, 2, 3))
print(Pager(10, 3, 3))