• 03 辅助函数三


      返回结构化数据

    1 def ajaxReturn(info,data={}):
    2     import json
    3     info['data'] = data
    4     return json.dumps(info)

      递归tree

    1 def get_tree(data,pid=0,level=0):
    2     data_list = []
    3     for info in data:
    4         if info['pid'] == pid:
    5             info['level'] = level
    6             info['name'] = '^-^ '*level +  info['name']
    7             data_list.append(info)
    8             data_list += get_tree(data, info['id'], level + 1)
    9     return data_list

      获取总页数

    1 def get_total_page(total):
    2     from flask import current_app
    3     import math
    4     try:
    5         pagesize = current_app.config['PAGESIZE']
    6     except:
    7         pagesize = 5
    8     return math.ceil(total/pagesize)

      分页试图函数

     1 def getHtmlPage(page=1,url='',total=0):
     2     '''
     3     * 获取分页的html内容
     4     * url 跳转链接
     5     * total 总条数
     6     * p = 起始页码
     7     '''
     8     from flask import current_app
     9     import math
    10     try:
    11         pagesize = current_app.config['PAGESIZE']
    12     except:
    13         pagesize = 5
    14     # 总页数
    15     pages = math.ceil(total/pagesize)
    16     # 最多显示10个页码
    17     _pageNum = 10
    18     # 如果当前页面小于1 则为1,否则就是其值
    19     page = 1 if page < 1 else page
    20     # 如果当前页数大于总数页数,则为总页数 否则为其页数
    21     page = pages if page > pages else page
    22     # 总页数小于当前页 则为当前页
    23     pages = page if pages < page else pages
    24 
    25     # 计算开始页
    26     _start = page - math.floor(_pageNum/2)
    27     _start = 1 if _start < 1 else _start
    28 
    29     # 计算结束页
    30     _end = page + math.floor(_pageNum/2)
    31     _end = pages if _end > pages else _end
    32 
    33     # 显示当前的页码数
    34     _currentPageNum = _end - _start + 1
    35 
    36     # 如果当前页码数不够最大页码数,左右调整
    37     # 左调整
    38     if _currentPageNum < _pageNum and _start > 1 :
    39         _start = _start - (_pageNum - _currentPageNum)
    40         _start = 1 if _start < 1 else _start
    41         _currentPageNum = _end - _start + 1
    42 
    43     # 右调整
    44     if _currentPageNum < _pageNum and _end < pages:
    45         _end = _end + (_pageNum - _currentPageNum)
    46         _end = pages if _end > pages else _end
    47 
    48     # 嵌套html
    49     _pageHtml = '<div class="dataTables_paginate paging_bootstrap"><ul class="pagination">'
    50     if page > 1 :
    51         _pageHtml += '<li><a  class="prev" title="上一页" href="'+url+'&page='+str(page - 1)+'">«</a></li>'
    52     for i in range(_start,_end+1):
    53         if i == page:
    54             _pageHtml += '<li><a class="active btn-info white">'+ str(i) +'</a></li>'
    55         else:
    56             _pageHtml += '<li><a href="' +url+ '&page=' +str(i)+ '">' +str(i)+ '</a></li>';
    57     if page < _end :
    58         _pageHtml += '<li><a class="next" title="下一页" href="'+ url + '&page='+str(page + 1)+'">»</a></li>';
    59     if total < 1 :
    60         _pageHtml += '<h4>暂无数据...</h4>'
    61     _pageHtml += "</ul></div>"
    62     # 返回值
    63     return '' if pages < 1 else _pageHtml

      生成一个随机数,六位随机数,不到六位,前面补0

    1 def getUUID(start=1,end=1000,length=6):
    2     import random
    3     randnum = str(random.randint(start,end))
    4     if len(randnum) < length :
    5         randnum = randnum.zfill(length)
    6     return randnum

      去除首尾空格和特殊符号

    1 def xss_clean(raw,flag=''):
    2     if flag == 'Integer':
    3         xraw = int(raw.strip()) if raw else 0
    4     else:
    5         xraw = raw.strip().replace('<', '&lt;').replace('>', '&gt;') if raw else ''
    6     return xraw

      获取菜单函数

    1 def getMenu(data,pid=0,deep=0):
    2     tree = []
    3     for row in data:
    4         if row['pid'] == pid:
    5             row['deep'] = deep
    6             row['child'] = getMenu(data,row['id'],deep+1)
    7             tree.append(row)
    8     return tree

      存储上传图片

     1 def save_images(images):
     2     from datetime import datetime
     3     import random
     4     pics_url = [] # 图片存储路径
     5     dir = 'app/static/images/questions/{}/'
     6     for image in images:
     7         try:
     8             ext = '.'+image.filename.rsplit('.', 1)[1]
     9         except:
    10             ext = '.png'
    11         path = dir.format(datetime.now().strftime('%Y-%m-%d'))
    12         filename = datetime.now().strftime('%d%H%M%S') + '-' + str(random.randint(10000, 99999)) + ext
    13         dir_exists(path) # 检查文件夹是否创建,若未创建,则创建
    14         pics = path + filename
    15         with open(pics,'wb') as fp:
    16             fp.write(image.read())
    17         pics_url.append(filename)
    18     return ';'.join(pics_url)
  • 相关阅读:
    异常[PersistenceUnit: default] Unable to build Hibernate SessionFactory
    关于本地模块安装入maven仓库出现的异常
    dwd面试记录与最近面试的一些感想。
    java编程思想第九章接口
    java编程思想第八章多态
    微服务调用本地测试与外部测试的差异
    Java编程思想第七章复用类
    Gson的学习与使用
    为什么会有这个分类?
    Spark 2.1.1 源码编译
  • 原文地址:https://www.cnblogs.com/a2534786642/p/11040680.html
Copyright © 2020-2023  润新知