TRM-coding | d1cbf67 | 2025-06-18 15:15:08 +0800 | [diff] [blame^] | 1 | from flask import Blueprint, render_template |
| 2 | from .functions.Fpost import Fpost; |
| 3 | from sqlalchemy import create_engine |
| 4 | from sqlalchemy.orm import sessionmaker |
| 5 | from config import Config |
| 6 | from flask import jsonify,request |
| 7 | |
| 8 | main = Blueprint('main', __name__) |
| 9 | |
| 10 | @main.route('/sgiveadmin',methods=['POST','GET']) |
| 11 | def giveadmin(): |
| 12 | data=request.get_json() |
| 13 | print(data) |
| 14 | engine=create_engine(Config.SQLURL) |
| 15 | SessionLocal = sessionmaker(bind=engine) |
| 16 | session = SessionLocal() |
| 17 | f=Fpost(session) |
| 18 | checres=f.checkid(data['userid'],'superadmin') |
| 19 | if(not checres): |
| 20 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 21 | |
| 22 | res=f.giveadmin(data['targetid']) |
| 23 | if not res: |
| 24 | return jsonify({'status': 'error', 'message': 'User not found'}) |
| 25 | |
| 26 | return jsonify({'status': 'success', 'message': 'User role updated to admin'}) |
| 27 | |
| 28 | @main.route('/sgiveuser',methods=['POST','GET']) |
| 29 | def giveuser(): |
| 30 | data=request.get_json() |
| 31 | print(data) |
| 32 | engine=create_engine(Config.SQLURL) |
| 33 | SessionLocal = sessionmaker(bind=engine) |
| 34 | session = SessionLocal() |
| 35 | f=Fpost(session) |
| 36 | checres=f.checkid(data['userid'],'superadmin') |
| 37 | if(not checres): |
| 38 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 39 | |
| 40 | res=f.giveuser(data['targetid']) |
| 41 | if not res: |
| 42 | return jsonify({'status': 'error', 'message': 'User not found'}) |
| 43 | |
| 44 | return jsonify({'status': 'success', 'message': 'User role updated to user'}) |
| 45 | |
| 46 | |
| 47 | @main.route('/sgivesuperadmin',methods=['POST','GET']) |
| 48 | def givesuperadmin(): |
| 49 | data=request.get_json() |
| 50 | print(data) |
| 51 | engine=create_engine(Config.SQLURL) |
| 52 | SessionLocal = sessionmaker(bind=engine) |
| 53 | session = SessionLocal() |
| 54 | f=Fpost(session) |
| 55 | checres=f.checkid(data['userid'],'superadmin') |
| 56 | if(not checres): |
| 57 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 58 | |
| 59 | res=f.givesuperadmin(data['targetid']) |
| 60 | if not res: |
| 61 | return jsonify({'status': 'error', 'message': 'User not found'}) |
| 62 | |
| 63 | return jsonify({'status': 'success', 'message': 'User role updated to superadmin'}) |
| 64 | |
| 65 | @main.route('/sgetuserlist',methods=['POST','GET']) |
| 66 | def userlist(): |
| 67 | data=request.get_json() |
| 68 | print(data) |
| 69 | engine=create_engine(Config.SQLURL) |
| 70 | SessionLocal = sessionmaker(bind=engine) |
| 71 | session = SessionLocal() |
| 72 | f=Fpost(session) |
| 73 | checres=f.checkid(data['userid'],'superadmin') |
| 74 | if(not checres): |
| 75 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 76 | res=f.getuserlist() |
| 77 | respons=[] |
| 78 | for datai in res: |
| 79 | respons.append({ |
| 80 | 'id': datai[0], |
| 81 | 'username': datai[1], |
| 82 | 'role': datai[2] |
| 83 | }) |
| 84 | return jsonify(respons) |
| 85 | |
| 86 | @main.route('/apostlist',methods=['POST','GET']) |
| 87 | def postlist(): |
| 88 | data=request.get_json() |
| 89 | print(data) |
| 90 | engine=create_engine(Config.SQLURL) |
| 91 | SessionLocal = sessionmaker(bind=engine) |
| 92 | session = SessionLocal() |
| 93 | f=Fpost(session) |
| 94 | checres=f.checkid(data['userid'],'admin') |
| 95 | if(not checres): |
| 96 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 97 | res=f.getlist() |
| 98 | respons=[] |
| 99 | for datai in res: |
| 100 | respons.append({ |
| 101 | 'id': datai[0], |
| 102 | 'title': datai[1], |
| 103 | 'status': datai[2] |
| 104 | }) |
| 105 | return jsonify(respons) |
| 106 | |
| 107 | @main.route('/agetpost',methods=['POST','GET']) |
| 108 | def post(): |
| 109 | data=request.get_json() |
| 110 | engine=create_engine(Config.SQLURL) |
| 111 | SessionLocal = sessionmaker(bind=engine) |
| 112 | session = SessionLocal() |
| 113 | f=Fpost(session) |
| 114 | checres=f.checkid(data['userid'],'admin') |
| 115 | if(not checres): |
| 116 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 117 | res=f.getpost(data['postid']) |
| 118 | |
| 119 | return jsonify(res.to_dict() if res else {}) |
| 120 | |
| 121 | @main.route('/areview',methods=['POST','GET']) |
| 122 | def review(): |
| 123 | data=request.get_json() |
| 124 | engine=create_engine(Config.SQLURL) |
| 125 | SessionLocal = sessionmaker(bind=engine) |
| 126 | session = SessionLocal() |
| 127 | f=Fpost(session) |
| 128 | checres=f.checkid(data['userid'],'admin') |
| 129 | if(not checres): |
| 130 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 131 | |
| 132 | res=f.review(data['postid'],data['status']) |
| 133 | if not res: |
| 134 | return jsonify({'status': 'error', 'message': 'Post not found'}) |
| 135 | |
| 136 | return jsonify({'status': 'success', 'message': 'Post reviewed successfully'}) |
| 137 | |
| 138 | |
| 139 | |
| 140 | @main.route('/nginxauth',methods=['POST','GET']) |
| 141 | def nginxauth(): |
| 142 | data=request.get_json() |
| 143 | engine=create_engine(Config.SQLURL) |
| 144 | SessionLocal = sessionmaker(bind=engine) |
| 145 | session = SessionLocal() |
| 146 | f=Fpost(session) |
| 147 | checres=f.checkid(data['userid'],'admin') |
| 148 | if(not checres): |
| 149 | return jsonify({'status': 'error', 'message': 'Unauthorized'}) |
| 150 | |
| 151 | res=f.nginxauth(data['postid'],data['status']) |
| 152 | if not res: |
| 153 | return jsonify({'status': 'error', 'message': 'Post not found'}) |
| 154 | |
| 155 | return jsonify({'status': 'success', 'message': 'Nginx auth updated successfully'}) |