03 Helper three

  Back structured data

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

  Recursive 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

  Gets the total number of pages

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)

  Paging trying to function

. 1  DEF getHtmlPage (Page. 1 =, = URL '' , Total = 0):
 2      '' ' 
. 3      * html content acquired tab
 . 4      * URL hoplinks
 . 5      * Total number of the total
 . 6      * P = starting page
 7      ' '' 
. 8      from Flask Import of CURRENT_APP
 . 9      Import Math
 10      the try :
 . 11          pageSize current_app.config = [ ' of pAGESIZE ' ]
 12 is      the except :
 13 is          pageSize. 5 =
 14      # pages 
15     = Math.ceil Pages (Total / pageSize)
 16      # shows up to 10 p 
. 17      _pageNum 10 =
 18 is      # if the current page was less than 1 1, otherwise its value 
. 19      Page 1 = IF Page <1 the else Page
 20 is      # If the current The total number of pages is larger than the number of pages, for the total number of pages or pages for 
21 is      page pages = IF page> pages the else page
 22 is      # total number of pages is less than this page was this page 
23 is      pages page = IF pages <page the else pages
 24  
25      # calculates the starting page 
26 is      _start page = - Math.floor (_pageNum / 2)
 27      _start. 1 = IF _start <. 1 the else _start
 28  
29      # calculation end page 
30      the _end Math.floor + = Page (_pageNum / 2 )
 31 is      the _end = Pages IF the _end> Pages the else the _end
 32  
33 is      # displays the current page number 
34 is      _currentPageNum the _end = - _start. 1 +
 35  
36      # If the current page number of the maximum number of pages is not enough, left and right adjustment 
37      # left adjuster 
38 is      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 :
     _pageHtml + =61 is'<H4> No data ... </ H4>'         _pageHtml + =60
"</ul></div>"
62     # 返回值
63     return '' if pages < 1 else _pageHtml

  Generating a random number, the random number six, less than six, a leading zero

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

  Leading and trailing spaces and special characters removed

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

  Get a menu function

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

  Upload pictures stored

 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 (). the strftime ( ' %% Y-M-% D ' ))
 12 is          filename = DateTime.Now (). the strftime ( ' % D% H% M% S ' ) + ' - ' + str (random.randint (10000, 99999)) + EXT
 13          dir_exists (path) # check whether the folder is created, if not created, you create 
14          pics = path + filename
 15          with Open (pics, ' WB ' ) AS FP:
 16              fp.write (image.read ())
 . 17          pics_url.append (filename)
 18 is     return ';'.join(pics_url)

 

Guess you like

Origin www.cnblogs.com/a2534786642/p/11040680.html