109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
|
from functions import *
|
|
from models import db, User, Photo
|
|
import os
|
|
|
|
# 初始化Flask应用
|
|
app = Flask(__name__)
|
|
app.secret_key = 'your_secret_key'
|
|
|
|
# 设置数据库
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' # 修改为你的数据库URI
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
# 初始化数据库
|
|
db.init_app(app)
|
|
|
|
# 创建数据库表
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
# 首页
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
# 用户管理
|
|
@app.route('/user_management')
|
|
def user_management():
|
|
users = User.query.all()
|
|
return render_template('user_management.html', users=users)
|
|
|
|
@app.route('/get_encoding/<int:user_id>')
|
|
def get_encoding(user_id):
|
|
user = User.query.get(user_id)
|
|
return jsonify({"encoding": user.encoding if user and user.encoding else "无数据"})
|
|
|
|
# 添加用户
|
|
@app.route('/add_user', methods=['POST'])
|
|
def add_new_user():
|
|
username = request.form['username']
|
|
userphoto = request.files['userphoto']
|
|
if add_user(username, userphoto):
|
|
flash("用户添加成功", "success")
|
|
else:
|
|
flash("用户添加失败", "danger")
|
|
return redirect(url_for('user_management'))
|
|
|
|
# 删除用户
|
|
@app.route('/delete_user/<int:user_id>', methods=['POST'])
|
|
def delete_existing_user(user_id):
|
|
if delete_user(user_id):
|
|
flash("用户删除成功", "success")
|
|
else:
|
|
flash("用户删除失败", "danger")
|
|
return redirect(url_for('user_management'))
|
|
|
|
# 编辑用户
|
|
@app.route('/edit_user/<int:user_id>', methods=['GET', 'POST'])
|
|
def edit_existing_user(user_id):
|
|
user = get_user(user_id)
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
userphoto = request.files.get('userphoto')
|
|
if edit_user(user_id, username, userphoto):
|
|
flash("用户信息更新成功", "success")
|
|
else:
|
|
flash("更新失败", "danger")
|
|
return redirect(url_for('user_management'))
|
|
return render_template('edit_user.html', user=user)
|
|
|
|
# 照片上传
|
|
@app.route('/upload', methods=['GET', 'POST'])
|
|
def upload():
|
|
if request.method == 'POST':
|
|
photos = request.files.getlist('photos') # 获取上传的照片
|
|
classify_photos(photos) # 进行照片分类处理
|
|
|
|
# 获取数据库中最新的照片记录,按照创建时间排序
|
|
recent_photos = Photo.query.order_by(Photo.created_at.desc()).limit(len(photos)).all()
|
|
|
|
# 组装数据:获取照片对应的用户信息
|
|
photos_with_users = []
|
|
for photo in recent_photos:
|
|
user = User.query.get(photo.user_id) # 获取用户信息
|
|
photos_with_users.append({
|
|
"filename": photo.classification_image_path, # 分类后的照片路径
|
|
"username": user.username if user else "未知", # 用户名
|
|
"created_at": photo.created_at # 照片创建时间
|
|
})
|
|
|
|
return render_template('upload.html', photos=photos_with_users)
|
|
|
|
return render_template('upload.html', photos=[])
|
|
|
|
|
|
|
|
# 照片搜索
|
|
@app.route('/search', methods=['GET', 'POST'])
|
|
def search():
|
|
if request.method == 'POST':
|
|
name = request.form['name']
|
|
date = request.form['date']
|
|
photos = search_photos(name, date)
|
|
return render_template('search.html', photos=photos)
|
|
return render_template('search.html')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|