from flask import Flask, render_template, request, redirect from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) # 设置数据库连接 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:20000521@127.0.0.1:3306/donation' # 定义模型 class Platform(db.Model): platform_id = db.Column(db.String(14), primary_key=True) name = db.Column(db.String(255), unique=True) mobile = db.Column(db.String(15), nullable=True) e_mail = db.Column(db.String(25), unique=True) address = db.Column(db.String(255), nullable=False) class unit(db.Model): unit_id = db.Column(db.String(50), primary_key=True) unit_name = db.Column(db.String(255), unique=True) class Material(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), unique=True) Price = db.Column(db.Float, nullable=False) class Charity(db.Model): id = db.Column(db.String(19), primary_key=True) name = db.Column(db.String(255), unique=True) mobile = db.Column(db.String(15), nullable=False) e_mail = db.Column(db.String(50), nullable=False) password = db.Column(db.String(16), nullable=False) class Project(db.Model): id = db.Column(db.String(24), primary_key=True) name = db.Column(db.String(255), nullable=False) propose = db.Column(db.String(255), nullable=False) goal = db.Column(db.Integer, nullable=False) rate = db.Column(db.Integer) plat_id = db.Column(db.String(14), db.ForeignKey(Platform.platform_id), nullable=False) c_id = db.Column(db.String(19), db.ForeignKey(Charity.id), nullable=False) class support(db.Model): Pro_id = db.Column(db.String(24), db.ForeignKey(Project.id), primary_key=True, nullable=False) unit_id = db.Column(db.String(50), db.ForeignKey(unit.unit_id), primary_key=True, nullable=False) ma_id = db.Column(db.Integer, db.ForeignKey(Material.id), primary_key=True, nullable=False) number = db.Column(db.Integer, nullable=False) class budget(db.Model): Pro_id = db.Column(db.String(24), db.ForeignKey(Project.id), primary_key=True) ma_id = db.Column(db.Integer, db.ForeignKey(Material.id), primary_key=True) number = db.Column(db.Integer) class user(db.Model): user_ID = db.Column(db.String(20), primary_key=True) pass_word = db.Column(db.String(16), nullable=False) plat_id = db.Column(db.String(14), db.ForeignKey(Platform.platform_id)) class jk(db.Model): Pro_id = db.Column(db.String(24), db.ForeignKey(Project.id), primary_key=True) User_id = db.Column(db.String(20), db.ForeignKey(user.user_ID), primary_key=True) money = db.Column(db.Integer, nullable=False) # 查询捐赠项目 @app.route('/select', methods=['GET']) def select_project(user_id): user_id = user_id project = db.session.query(Project.id, Project.name, Project.propose, Project.goal, Project.rate, Charity.name, Project.plat_id).filter(Project.c_id == Charity.id).filter( Project.plat_id == Platform.platform_id).all() platform = Platform.query.first() return render_template("wzjz.html", pro_list=project, plat_list=platform, user_id=user_id) @app.route('/') def index(): return redirect('/enter') @app.route('/enter', methods=['GET','POST']) def user_enter(): if request.method == 'GET': return render_template('enter.html') else: user_id = request.form['user_id'] pass_word = str(request.form['password']) ser = user.query.filter(user.user_ID == user_id).first() if pass_word == ser.pass_word: return select_project(user_id) else: return redirect('/') # 查看项目详情 @app.route("/chakan", methods=['GET']) def select_detail(): id = request.args.get('id') user_id = request.args.get('user_id') project = Project.query.filter(Project.id == id).first() charity = Charity.query.filter(Charity.id == project.c_id).first() yu = db.session.query(Material.name, Material.Price, budget.number).filter(Material.id == budget.ma_id).filter( budget.Pro_id == id).all() sp = db.session.query(unit.unit_name, Material.name, support.number).filter(unit.unit_id == support.unit_id).filter( Material.id == support.ma_id).filter(support.Pro_id == id).all() return render_template("chakan.html", cha_list=charity, pro=project, yu_list=yu, sp_list=sp, user_id=user_id) @app.route('/alter', methods=['GET', 'POST']) def donate(): if request.method == 'GET': user_id = request.args.get('user_id') id = request.args.get('id') project = Project.query.filter(Project.id == id).first() return render_template('alter.html', project=project, user_id=user_id) else: user_id = request.form['user_id'] id = request.form['id'] money = int(request.form['money']) jke = jk(Pro_id=id, User_id=user_id, money=money) db.session.add(jke) db.session.commit() donation = Project.query.filter(Project.id == id).first() donation.rate = donation.rate + (money / donation.goal) * 100 db.session.commit() return select_project(user_id) @app.route('/insert', methods=['GET', 'POST']) def insert_project(): if request.method == 'GET': cha_id = request.args.get('cha_id') plat_id = request.args.get('plat_id') return render_template('insert.html', plat_id=plat_id, cha_id=cha_id) else: id = request.form['id'] name = request.form['name'] propose = request.form['propose'] goal = int(request.form['number']) plat_id = request.form['plat_id'] c_id = request.form['cha_id'] print(plat_id) project = Project(id=id, name=name, propose=propose, goal=goal, rate=0, plat_id=plat_id, c_id=c_id) db.session.add(project) db.session.commit() pro = Project.query.filter(Project.c_id == c_id).all() return render_template("cha_select.html", pro_list=pro) @app.route('/delete', methods=['GET']) def pro_delete(): id = request.args.get('pro_id') c_id = request.args.get('cha_id') sp = support.query.filter(support.Pro_id == id).all() for spp in sp: db.session.delete(spp) db.session.commit() bd = budget.query.filter(budget.Pro_id == id).all() for each in bd: db.session.delete(each) db.session.commit() project = Project.query.filter(Project.id == id).first() db.session.delete(project) db.session.commit() pro = Project.query.filter(Project.c_id == c_id).all() return render_template("cha_select.html", pro_list=pro) @app.route('/cha_pass', methods=['GET', 'POST']) def cha_project(): if request.method == 'GET': plat_id = request.args.get('plat_id') plat = Platform.query.filter(Platform.platform_id == plat_id).first() return render_template("cha_pass.html", plat_id=plat) else: plat_id = request.form['plat_id'] cha_name = request.form['cha_name'] cha_password = request.form.get('password') c_id = db.session.query(Charity.id).filter(Charity.name == cha_name).first() pass_word = db.session.query(Charity.password).filter(Charity.id == c_id).first() if cha_password == pass_word[0]: project = Project.query.filter(Project.c_id == c_id).all() return render_template("cha_select.html", pro_list=project) else: return render_template('cha_pass.html', plat_id=plat_id) @app.route('/update', methods=['GET', 'POST']) def pro_update(): if request.method == 'GET': pro_id = request.args.get('id') a = pro_id return render_template('update.html', pro_id=a) else: pro_id = request.form['pro_id'] ma_name = request.form['ma_name'] number = int(request.form['number']) area_name = request.form.get('area_name') ma_id = db.session.query(Material.id).filter(Material.name == ma_name).first() pro = db.session.query(Charity.id).filter(Charity.id == Project.c_id).filter(Project.id == pro_id).first() project = Project.query.filter(Project.c_id == pro).all() area_id = db.session.query(unit.unit_id).filter(unit.unit_name == area_name).first() ap = support(Pro_id=pro_id, area_id=area_id[0], ma_id=ma_id[0], number=number) db.session.add(ap) db.session.commit() return render_template('cha_select.html', pro_list=project) @app.route('/jk_detail') def jk_detail(): pro_id = request.args.get('pro_id') jz = jk.query.filter(jk.Pro_id == pro_id) return render_template("detail.html", jk_list = jz) if __name__ == "__main__": app.run(debug=True, host='0.0.0.0', port=8800)