from flask import Flask, jsonify, request from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity, create_access_token from datetime import timedelta from FlaskApp.BackendApi.Models import RoleModel, UserModel from FlaskApp.BackendApi.SharixQuery import SharixQuery, ReturnData from FlaskApp.api_config import Config app = Flask(__name__) app.config.from_object(Config) jwt = JWTManager(app) db_query = SharixQuery() @app.route('/') def server_hello(): return "It`s workk!" @app.route('/auth', methods=['POST']) def post_auth(): try: result = db_query.auth_query(request.get_json()) return gen_token(result.data.id, 5), result.code except Exception as ex: return err_mes(ex) @app.route('/adduser', methods=['POST']) def add_user(): try: result = db_query.add_user(request.get_json()) return jsonify(result.data), result.code except Exception as ex: return err_mes(ex) @app.route('/user/', methods=['GET']) def get_user(id): try: result = db_query.get_user(id) if type(result) == UserModel: result.password = None result.insys = None result.id = None return jsonify(result.as_dict()) else: return result.data, result.code except Exception as ex: return err_mes(ex) @app.route('/users', methods=['GET']) def get_show_user(): try: data = [] result = db_query.get_all_user() if type(result) == list: for user in result: user.lastlogintime = None user.insys = None data.append(user.as_dict()) return jsonify(data), 200 else: return result.data, result.code except Exception as ex: return err_mes(ex) @app.route('/deleteuser', methods=['POST']) def delete_user(): try: result = db_query.delete_user_query(UserModel.id, request.get_json()["id"]) return jsonify(result.data), result.code except Exception as ex: return err_mes(ex) @app.route('/updateuser', methods=['POST']) def test_api(): try: result = db_query.update_user(request.get_json()) return jsonify(result.data), result.code except Exception as ex: return err_mes(ex) @app.route('/roles', methods=['GET']) def get_all_roles(): try: data = [] result = db_query.get_roles() if type(result) == list: for role in result: data.append(role.as_dict()) return jsonify(data), 200 else: return result.data, result.code except Exception as ex: return err_mes(ex) @app.route('/deleterole', methods=['POST']) def delete_role(): try: result = db_query.delete_role(RoleModel.id, request.get_json()["id"]) return result.data, result.code except Exception as ex: return err_mes(ex) @app.route('/addrole', methods=['POST']) def add_role(): try: result = db_query.add_role(request.get_json()) return result.data, result.code except Exception as ex: return err_mes(ex) def gen_token(id, min): token = {"access_token" : create_access_token(identity=id, expires_delta=timedelta(minutes=min))} return jsonify(token) def err_mes(ex): return jsonify({"msg":str(ex)}), 500 if __name__ == '__main__': app.run()