webpy分页类 + 上传类
webpy没有分页类。按照php的思路。自己编了一个。数据库用的是sqlite。
class Page(object):
'''分页类'''
def __init__(self,page_size,data_count,page_current):
import math
self.size = page_size
self.data_count = data_count
self.page_current = int(page_current)
self.page_max = int(math.ceil(self.data_count * 0.1 * 10 / self.size )) self.page_current = 1 if self.page_current < 1 else self.page_current
self.page_current = self.page_max if self.page_current > self.page_max else self.page_current self.offset = ( self.page_current - 1) * self.size
def set_url(self,url):
if "?" in url:
f_url = url.split("page")[0]
if "&" in url:
f_url += "&"
else:
f_url = url + "?"
self.url = f_url
def get_html(self):
self.page_pre = self.page_current - 1
self.page_next = self.page_current + 1
if self.page_max in (0,1) :
html = u'''
<span>首页</span>
<span>上一页</span>
<span>下一页</span>
<span>尾页</span>
'''
elif self.page_current <= 1:
html = u'''
<span>首页</span>
<span>上一页</span>
<span><a href="{self.url}page={self.page_next}">下一页</a></span>
<span><a href="{self.url}page={self.page_max}">尾页</a></span>
'''.format(self=self)
elif self.page_current >= self.page_max:
html = u'''
<span><a href="{self.url}page=1">首页</a></span>
<span><a href="{self.url}page={self.page_pre}">上一页</a></span>
<span>下一页</span>
<span>尾页</span>
'''.format(self=self)
else:
html = u'''
<span><a href="{self.url}page=1">首页</a></span>
<span><a href="{self.url}page={self.page_pre}">上一页</a></span>
<span><a href="{self.url}page={self.page_next}">下一页</a></span>
<span><a href="{self.url}page={self.page_max}">尾页</a></span>
'''.format(self=self)
banner = u'''第<span id="spanPageNum">{self.page_current}</span>页/共<span id="spanTotalPage">{self.page_max}</span>页'''.format(self=self)
html = "<div>%s</div>"%(html + banner) if self.data_count > self.size:
return html
else:
return ""
调用:
page_current = i.get("page",1)
page_size = 5 #每页显示几条数据
data_count = (db.select("messages",what="count(content) c")[0]["c"]) #总数据量
page = conf.Page(page_size,data_count,page_current)
page.set_url(web.ctx.fullpath)
page_html = page.get_html()
data = db.select("messages",order="datetime desc",limit=page.size,offset=page.offset)
上传文件类:
class Upload(object):
u'''文件上传类,接受excel,csv文件''' def __init__(self,upfile):
import os
self.file = upfile
self.file_ext = upfile.filename.split(".")[-1]
self.file_name = upfile.filename.split(".")[-2]
#self.file_path = upfile.filename.replace('\\','/')
def save(self):
'''将文件存入服务器文件夹'''
import os,time
if not self.file_ext in ("xlsx","xls"):
return u"文件类型错误"
try:
os.mkdir(r"static/upload/file_dir/")
save_path = r"static/upload/file_dir/"
except:
save_path = r"static/upload/file_dir/" now = str(time.time()).split(".")[0] try:
with open(save_path+"%s"%(now+"."+self.file_ext),"wb") as f:
f.write(self.file.file.read())
self.filepath = save_path+"%s"%(now+"."+self.file_ext)
except:
pass def get_data(self):
'''返回数据'''
import os
# try:
# import openpyxl
# wb = openpyxl.load_workbook(self.filepath)
# ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
# data = [[j.value for j in i] for i in ws.rows[2:]]
# return data
# except:
# return "openpyxl is not exists!"
try:
import xlrd
wb = xlrd.open_workbook(self.filepath)
ws = wb.sheet_by_index(0)
data = [ws.row_values(i) for i in range(ws.nrows)][2:]
return data
except:
return "xlrd is not exists!"
finally:
os.remove(self.filepath)
class Upload(object):
u'''文件上传类,接受excel文件''' def __init__(self,upfile):
import os
self.file = upfile
self.file_ext = upfile.filename.split(".")[-1]
self.file_name = upfile.filename.split(".")[-2]
#self.file_path = upfile.filename.replace('\\','/')
def save(self):
'''将文件存入服务器文件夹'''
import os,time
if not self.file_ext in ("xlsx","xls"):
return u"文件类型错误"
try:
os.mkdir(r"static/upload/file_dir/")
save_path = r"static/upload/file_dir/"
except:
save_path = r"static/upload/file_dir/" now = str(time.time()).split(".")[0] try:
with open(save_path+"%s"%(now+"."+self.file_ext),"wb") as f:
f.write(self.file.file.read())
self.filepath = save_path+"%s"%(now+"."+self.file_ext)
except:
pass def get_data(self):
'''返回数据'''
import os
# try:
# import openpyxl
# wb = openpyxl.load_workbook(self.filepath)
# ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
# data = [[j.value for j in i] for i in ws.rows[2:]]
# return data
# except:
# return "openpyxl is not exists!"
try:
import xlrd
wb = xlrd.open_workbook(self.filepath)
ws = wb.sheet_by_index(0)
data = [ws.row_values(i) for i in range(ws.nrows)][2:]
return data
except:
return "xlrd is not exists!"
finally:
os.remove(self.filepath)
webpy分页类 + 上传类的更多相关文章
- php-数据库-分页类-上传类
config.ini.php <?php header("content-type:text/html;charset=utf-8"); //项目的根目录 define(&q ...
- php四个常用类封装 :MySQL类、 分页类、缩略图类、上传类;;分页例子;
Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = null;//数据库连接 /** * 私有的构造方法 */ pr ...
- 自定义MVC框架之工具类-文件上传类
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...
- THINKPHP源码学习--------文件上传类
TP图片上传类的理解 在做自己项目上传图片的时候一直都有用到TP的上传图片类,所以要进入源码探索一下. 文件目录:./THinkPHP/Library/Think/Upload.class.php n ...
- PHP图片上传类
前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也很有必要. 图片上传的流程图 一.控制器调用 public function upload_file() { if (IS_P ...
- Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传
在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类. ① 下载编辑器(下载地址:http ...
- ASP.NET 文件上传类 简单好用
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
- PHP多文件上传类
<?php class Upload{ var $saveName;// 保存名 var $savePath;// 保存路径 var $fileFormat = array('gif','jpg ...
- PHP 文件上传类
FileUpload.; $]; $_newname = date(,). : To ...
随机推荐
- linux下安装nginx后开机启动篇
众所周知nginx安装后需要手动去启动,每次开机之后都要执行nginx的启动命令很蛋疼.那么我们来让nginx开机启动吧 1.先創建一個nginx文件把 [root@localhost ~]# vi ...
- Grunt完成对LESS实时编译
安装 安装grunt需要先安装node.js. 之后需要借助npm来安装grunt-cli,在cmd中npm install -g grunt-cli.(测试grunt --version看是否正确显 ...
- 【代码】verilog之:电子钟
功能:显示时分秒,能够设置时间. 实现:两个按键,一个进入设置,一个加数字.显示用LCD5110 用状态机实现,总共四种状态 idle(正常运行)——s_hour(时设置状态)——s_min(分设置状 ...
- android 总结(样式)—跑马灯 button的点击效果 RadioGroup 实现滑动的效果 button 下面有阴影 卡片样式
<Button android:layout_width="wrap_content" android:layout_height="wrap_content&qu ...
- 解决 iOS7 通过tag 找不到 UITableViewCell 的子控件
当iOS7问世,程序的世界就混乱了,以前良好的程序,现在是一塌糊涂,于是只能把问题一个一个攻破. 由于项目当中需要每个cell显示数目不同的图片,于是我在每个cell 赋值之前,通过一下方法把cell ...
- HiveQL(HiveSQL)跟普通SQL最大区别一直使用PIG,而今也需要兼顾HIVE
HiveQL(Hive SQL)跟普通SQL最大区别 一直使用PIG,而今也需要兼顾HIVE.网上搜了点资料,感觉挺有用,这里翻译过来.翻译估计不太准确,待自己熟悉HIVE后再慢慢总结. * No t ...
- Jmeter—7 测试中使用到的定时器和逻辑控制器
1 测试中提交数据有延时1min,所以查询数据是否提交成功要设置定时器. 固定定时器页面:单位是毫秒 [dinghanhua] 2 集合点.Synchronizing Timer 集合点编辑:集合用户 ...
- mysql linux终端登陆
mysql -uroot -hlocalhost -psorry 设置远程登录 用户名及密码 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDE ...
- SAP web 开发 (第二篇 bsp 开发 mvc模式 Part1 )
Model-View-Controller 简称MVC. 简单的说就是把数据处理,显示,页面事件及处理过程分离开来,企业应用多数都采用这种方式,多层架构的优缺点不再多言,google一下啥都知道. 在 ...
- 开发经验之状态机思想,分别使用了swift,OC,C,PHP语言实现
这里设计一个简单的练习,使用状态机思想实现,分别使用了swift,OC,C,PHP语言实现 题目:1到10000遍历,开始-打印奇数-遇到7的倍数开始打印偶数--遇到10的倍数打印奇数 //部分结 ...