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)
时间: 2024-08-11 09:56:32

webpy分页类 + 上传类的相关文章

ajax结合文件上传类进行多文件的单个上传

今天做项目的时候碰见一个问题:之前一个同事离职之前做了一个网站,有一个上传商品详细图片的功能,当时已经完成,但是由于后期程序的有更改以及更改的程序员的水平也是参差不齐,最后导致程序bug很多,由于当时用的是一个框架,最终也没找到说明文档,后来我就重新写了一个结合ajax上传文件的upload.classs.php虽然界面欠缺美观,但是通俗易懂好维护. //首先是页面. index.php <!DOCTYPE html> <html lang="en"> <

Ueditor 1.4.3.1 使用 ThinkPHP 3.2.3 的上传类进行图片上传

在 ThinkPHP 3.2.3 中集成百度编辑器最新版 Ueditor 1.4.3.1,同时将编辑器自带的上传类替换成 ThinkPHP 3.2.3 中的上传类. ① 下载编辑器(下载地址:http://ueditor.baidu.com/website/download.html),解压后放入项目根目录的 Data 目录并且将解压出来的目录重命名为 ueditor. 项目中的控制器 ./Application/Admin/Controller/BlogController.class.php

艾恩ASP无组件上传类(上传组件)说明文档(from www.sysoft.cc)

艾恩ASP无组件上传类(上传组件)说明文档2010-1-18 By Anlige一.简介自从接触ASP就开始接触上传,看过一些上传类,但是总感觉封装的还是不够简单,因此自己尝试写一个能够用最少最简单的代码实现各种上传方式的上传类.在学校期间就开始写,一点点的完善.优化,到现在的版本,现在的版本能适应各种上传方式.上传类的主要的功能如下:1.自由设置最大上传大小.单文件最大上传大小2.自由设置允许上传的文件类型3.可设置文本的编码,以适应各种上传环境4.内置进度条,a用户可选择开启和关闭5.多种错

PHP图片上传类

前言 在php开发中,必不可少要用到文件上传,整理封装了一个图片上传的类也还有必要.一.控制器调用 public function upload_file() { if (IS_POST) { if (!empty($_FILES['Filedata'])) { import('Org.Upload', COMMON_PATH); $upload = new \Upload(); // 允许上传文件大小 $upload->allowMaxSize(C('ALLOW_UPLOAD_FILE_MAX

面向对象中的文件上传类

<?php /** *文件上传类 * **/ class Upload { //上传到哪个目录 protected $path = './upload/'; //准许的MIME protected $allowmime = ['image/png','image/jpg','image/jpeg','image/pjpeg','image/bmp','image/wbmp','image/gif','image/x-png']; //准许的后缀 protected $allowsubfix = 

PHP文件上传类(页面和调用部分)

<!--upform.html内容--> <form action="upload.php" method="post" enctype="multipart/form-data" > name: <input type="text" name="username" value="" /><br> <input type="

文件上传类

<?php /** file: fileupload.class.php 文件上传类FileUpload 本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */ class FileUpload { private $path = "./uploads"; //上传文件保存的路径 private $allowtype = array('jpg','gif','png'); //设置限制上传文件的类型 private $maxsize = 1000000;

asp无惧上传类2.2上传文件的同时,通过 Request.QueryString将参数传递到保存页面中

先转一段文字,对不对再评论 在后台asp程序中,以前获取表单提交的ASCII 数据,非常的容易.但是如果 需要获取上传的文件,就必须使用Request对象的BinaryRead方法来读取.BinaryRead方法是对当前输入流进行指定字节数的二进制读取,有点需要注意的 是,一旦使用BinaryRead 方法后,再也不能使用Request.Form 或  Request.QueryString 集合了.结合Request对象的TotalBytes属性,可以将 所有表单提交的数据全部变成二进制,不过

PHP文件上传类

<form action="upload.php" method="post" enctype="multipart/form-data" > name: <input type="text" name="username" value="" /><br> <input type="hidden" name="MAX