import math import base64 import json import hashlib from flask import Blueprint, jsonify, request from flask import render_template from ..model import School # 导入上级模块 # 从 app 中导入 limiter 对象 from app import limiter # 导入 flask_login 内部函数 from flask_login import login_required s = Blueprint('school', __name__, url_prefix='/ss') def Pagination(page, total): """ Pagination 用于封装分页参数到字典变量中,并返回到前台 :param page: 传入参数,页码 :param total: 数据总量 :return: """ page_obj = { "prev_page": page - 1, "next_page": page + 1, "current_page": 0, "total_page": 0, "max_page": 0, "page_size": 20, "total": total, "offset": 0, "page_range": None } page_obj["total_page"] = math.ceil(total / page_obj["page_size"]) page_obj["max_page"] = page_obj["total_page"] if page <= 1: page = 1 page_obj["prev_page"] = 1 if page >= page_obj["max_page"]: page = page_obj["max_page"] page_obj["next_page"] = page_obj["max_page"] page_obj["current_page"] = page if total == 0: page_obj["offset"] = 0 else: page_obj["offset"] = (page_obj["current_page"] - 1) * page_obj["page_size"] page_range = [] for i in range(1, page_obj["max_page"] + 1): page_range.append(i) page_obj["page_range"] = page_range return page_obj def pagination_object(page=1): # schools = School.query.all() query = School.query total = query.count() # 获取数据总量 pagination = Pagination(page, total) # 获取分页对象详细参数 if total != 0: data_list = query.offset(pagination["offset"]).limit(pagination["page_size"]).all() else: data_list = [] data_list = [row.to_dict() for row in data_list] pagination["data_list"] = data_list return pagination @s.route('/list') def list_school(): page = int(request.args.get("page", 1)) # 获取页码 pagination = pagination_object(page) return render_template('school/index.html', pagination=pagination) # 登录后可以查阅 @s.route('/login_list') @login_required def login_list_school(): page = int(request.args.get("page", 1)) # 获取页码 pagination = pagination_object(page) return render_template('school/login_index.html', pagination=pagination) @s.route('ajax_list') def ajax_list(): page = 1 # 初始化第一页数据 pagination = pagination_object(page) return render_template('school/ajax_list.html', pagination=pagination) @s.route('api2') def ajax_list_school(): page = int(request.args.get("page", 1)) pagination = pagination_object(page) return jsonify(pagination) @s.route('pr') def pull_refresh(): page = int(request.args.get("page", 1)) pagination = pagination_object(page) return render_template('school/pulltorefresh.html', pagination=pagination) """ 简单返回加密 """ @s.route('encry_list') def encry_ajax_list(): page = 1 # 初始化第一页数据 pagination = pagination_object(page) return render_template('school/encry_ajax_list.html', pagination=pagination) # 加密 API @s.route('encry_api') def encry_api(): page = int(request.args.get("page", 1)) pagination = pagination_object(page) data_bytes = json.dumps(pagination["data_list"]).encode() # 重新赋值 pagination["data_list"] = base64.b64encode(data_bytes).decode("utf-8") return jsonify(pagination) """ 间隔10秒生成一Cookie """ @s.route('ajax_list2') def ajax_list2(): page = 1 # 初始化第一页数据 pagination = pagination_object(page) return render_template('school/ajax_list2.html', pagination=pagination) @s.route('api_token') def token_list_school(): page = int(request.args.get("page", 1)) token = request.args.get("token", None) timestamp = request.args.get("timestamp", None) if token is None or timestamp is None: return "请求出错", 403 # 加密 timestamp,与 token 比较 hash = hashlib.md5() hash.update(timestamp.encode()) p_token = hash.hexdigest() print(p_token) if token != p_token: return "请求出错", 403 pagination = pagination_object(page) return jsonify(pagination) """ 限制 IP 访问 """ @s.route('ajax_list3') def ajax_list3(): page = 1 # 初始化第一页数据 pagination = pagination_object(page) return render_template('school/ajax_list3.html', pagination=pagination) @s.route('api3') @limiter.limit("3/second") def school_api3(): page = int(request.args.get("page", 1)) pagination = pagination_object(page) return jsonify(pagination)