__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from flask import Flask, jsonify, request
  2. from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity, create_access_token
  3. from datetime import timedelta
  4. from FlaskApp.BackendApi.Models import RoleModel, UserModel
  5. from FlaskApp.BackendApi.SharixQuery import SharixQuery, ReturnData
  6. from FlaskApp.api_config import Config
  7. app = Flask(__name__)
  8. app.config.from_object(Config)
  9. jwt = JWTManager(app)
  10. db_query = SharixQuery()
  11. @app.route('/')
  12. def server_hello():
  13. return "It`s workk!"
  14. @app.route('/auth', methods=['POST'])
  15. def post_auth():
  16. try:
  17. result = db_query.auth_query(request.get_json())
  18. return gen_token(result.data.id, 5), result.code
  19. except Exception as ex:
  20. return err_mes(ex)
  21. @app.route('/adduser', methods=['POST'])
  22. def add_user():
  23. try:
  24. result = db_query.add_user(request.get_json())
  25. return jsonify(result.data), result.code
  26. except Exception as ex:
  27. return err_mes(ex)
  28. @app.route('/user/<int:id>', methods=['GET'])
  29. def get_user(id):
  30. try:
  31. result = db_query.get_user(id)
  32. if type(result) == UserModel:
  33. result.password = None
  34. result.insys = None
  35. result.id = None
  36. return jsonify(result.as_dict())
  37. else:
  38. return result.data, result.code
  39. except Exception as ex:
  40. return err_mes(ex)
  41. @app.route('/users', methods=['GET'])
  42. def get_show_user():
  43. try:
  44. data = []
  45. result = db_query.get_all_user()
  46. if type(result) == list:
  47. for user in result:
  48. user.lastlogintime = None
  49. user.insys = None
  50. data.append(user.as_dict())
  51. return jsonify(data), 200
  52. else:
  53. return result.data, result.code
  54. except Exception as ex:
  55. return err_mes(ex)
  56. @app.route('/deleteuser', methods=['POST'])
  57. def delete_user():
  58. try:
  59. result = db_query.delete_user_query(UserModel.id, request.get_json()["id"])
  60. return jsonify(result.data), result.code
  61. except Exception as ex:
  62. return err_mes(ex)
  63. @app.route('/updateuser', methods=['POST'])
  64. def test_api():
  65. try:
  66. result = db_query.update_user(request.get_json())
  67. return jsonify(result.data), result.code
  68. except Exception as ex:
  69. return err_mes(ex)
  70. @app.route('/roles', methods=['GET'])
  71. def get_all_roles():
  72. try:
  73. data = []
  74. result = db_query.get_roles()
  75. if type(result) == list:
  76. for role in result:
  77. data.append(role.as_dict())
  78. return jsonify(data), 200
  79. else:
  80. return result.data, result.code
  81. except Exception as ex:
  82. return err_mes(ex)
  83. @app.route('/deleterole', methods=['POST'])
  84. def delete_role():
  85. try:
  86. result = db_query.delete_role(RoleModel.id, request.get_json()["id"])
  87. return result.data, result.code
  88. except Exception as ex:
  89. return err_mes(ex)
  90. @app.route('/addrole', methods=['POST'])
  91. def add_role():
  92. try:
  93. result = db_query.add_role(request.get_json())
  94. return result.data, result.code
  95. except Exception as ex:
  96. return err_mes(ex)
  97. def gen_token(id, min):
  98. token = {"access_token" : create_access_token(identity=id, expires_delta=timedelta(minutes=min))}
  99. return jsonify(token)
  100. def err_mes(ex):
  101. return jsonify({"msg":str(ex)}), 500
  102. if __name__ == '__main__':
  103. app.run()