识身——一个使用flask+sqlalchemy开发的后台管理系统接口

backend develop · 2024-03-18 · 218 人浏览

使用flask+sqlalchemy开发的后台管理系统接口

持续完成中

项目背景

使用fpga等硬件设备实现了人脸识别、指纹识别、声纹识别和步态识别

现在需要一个web端管理系统,实现

  1. 管理员角色的登录并实现JWT鉴权
  2. 录入一般用户的基础信息和上述识别所需的信息
  3. 展示一般用户的基础信息和上述识别所需的信息
  4. 点击对应路由后唤醒摄像头设备并在网页上显示摄像头画面

综合考量后我选择了python的flask作为开发框架,sqlalchemy作为数据库交互工具,mysql作为数据库

建表

管理员用户信息表

由于是后台管理系统,所以不需要设计注册功能,直接设计一个用户名密码组成的表,并在数据表中记录信息即可

DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin`  (
  `id` tinyint(0) UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;

需要注意的是,为了确保安全性,密码不会明文传输和存储,而是使用md5值

一般用户信息表

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `fingerprint_image_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `face_image_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `voice_print_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `gait_near_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `gait_far_url` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `is_valid` int(0) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;

生成目录结构

import os

def create_directory_structure(root_dir):
    directories = ['app', 'app/routes', 'app/services', 'app/models']
    init_files = ['app/__init__.py', 'app/routes/__init__.py', 'app/services/__init__.py', 'app/models/__init__.py']
    other_files = ['run.py', 'config.py', 'requirements.txt', 'README.md', 'migrations']

    for directory in directories:
        os.makedirs(os.path.join(root_dir, directory), exist_ok=True)

    for init_file in init_files:
        with open(os.path.join(root_dir, init_file), 'w') as f:
            pass

    for file in other_files:
        with open(os.path.join(root_dir, file), 'w') as f:
            pass

if __name__ == "__main__":
    project_dir = input("Enter project directory name: ")

    if not os.path.exists(project_dir):
        os.makedirs(project_dir)

    create_directory_structure(project_dir)
    print("Flask + SQLAlchemy project directory structure created successfully.")

使用如上脚本会生成以下的目录结构,符合一般flask开发逻辑

<project_dir>/
   app/
      __init__.py
      routes/
         __init__.py
      services/
         __init__.py
      models/
         __init__.py
   run.py
   config.py
   requirements.txt
   README.md
   migrations/

生成本地测试http服务器

因为要使用存储在数据库中的url访问资源,所以需要在部署环节中部署ftp服务,不过开发环境下还是在本地起一个http服务测试来得方便

import http.server
import socketserver
import os

# 设置 HTTP 服务器的端口
PORT = 8001

# 配置静态文件目录
Handler = http.server.SimpleHTTPRequestHandler
os.chdir("local_server")

# 启动 HTTP 服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Serving at port", PORT)
    #print(os.getcwd())
    # 开始监听请求
    try:
        httpd.serve_forever()
        
    except KeyboardInterrupt:
        # 捕获键盘中断信号,关闭服务器
        print("KeyboardInterrupt: Stopping the server.")
        httpd.shutdown()
backend develop python
Theme Jasmine by Kent Liao