You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import os
  2. import copy
  3. import json
  4. import base64
  5. import pyDes
  6. import qrcode
  7. from api.modules import utils
  8. from flask import Flask, request
  9. from flask_restful import Resource, Api, abort
  10. enc = pyDes.triple_des(b'Kz\n\x1a\xc1~\x05#\xf9\xad\xc8\xa2\x15\xd5J\x89\xe4RT\x8d\xb3?\x93\x1c')
  11. app = Flask(__name__)
  12. api = Api(app)
  13. db_path = os.path.join(app.root_path, 'databases', 'users.json')
  14. with open(db_path, 'r') as f:
  15. users = json.load(f)
  16. class Users(Resource):
  17. def post(self):
  18. """
  19. Example POST Data:
  20. username=<username>&
  21. password=<password>&
  22. realname=<realname>& # OPTIONAL
  23. avatar=<avatar_url>& # OPTIONAL
  24. """
  25. args = request.form
  26. user_id = utils.generate_id()
  27. user = {
  28. 'id': user_id,
  29. 'username': args['username'],
  30. 'realname': args.get('realname'),
  31. 'avatar' : args.get('avatar'),
  32. 'password': utils.md5( args[ 'password' ] ),
  33. 'stats': {
  34. 'bus_usage_week': 0,
  35. 'bus_usage_month': 0,
  36. 'bus_usage_year': 0
  37. },
  38. 'daily_electricity_usage': [],
  39. 'points': 0
  40. }
  41. users.append(user)
  42. with open(db_path, 'w') as f:
  43. json.dump(users, f, indent=4)
  44. return user
  45. class User(Resource):
  46. def get(self, user_id):
  47. try:
  48. user = utils.find_by_id( users.values(), user_id )
  49. if not user:
  50. raise Exception('User not found!')
  51. del user['password']
  52. return user
  53. except:
  54. abort(404, error="User {} doesn't exist".format(user_id))
  55. class Login(Resource):
  56. def post(self):
  57. """
  58. Example POST Data:
  59. username=<username>&
  60. password=<password>
  61. """
  62. #Password for efe is 12345
  63. args = request.form
  64. username = args['username']
  65. password = utils.md5( args[ 'password' ] )
  66. if not username in users:
  67. return [False, {}]
  68. user = copy.deepcopy(users[username])
  69. if user['password'] == password:
  70. del user["password"]
  71. return [True, json.dumps(user)]
  72. else:
  73. return [False, {}]
  74. class QRCode(Resource):
  75. def post(self):
  76. """
  77. POST Data:
  78. id=<user_id>
  79. """
  80. user_id = request.form['id']
  81. if utils.find_by_id(users.values(), user_id):
  82. image_path = os.path.join(app.root_path, '..', 'images', user_id + '_qr' + '.png')
  83. if not os.path.exists(image_path):
  84. encrypted_id = enc.encrypt(user_id, padmode=2)
  85. img = qrcode.make(base64.b64encode(encrypted_id).decode('utf-8'))
  86. img.save(image_path)
  87. return '/img/' + user_id + '_qr' + '.png'
  88. else:
  89. abort(404, error="User {} doesn't exist".format(user_id))
  90. class QRRead(Resource):
  91. def post(self):
  92. """
  93. POST Data:
  94. qr_data=<qr_data>
  95. """
  96. qr_data = base64.b64decode(request.form['qr_data'])
  97. user_id = enc.decrypt(qr_data, padmode=2)
  98. return utils.find_by_id(users.values(), user_id.decode())
  99. if __name__ == '__main__':
  100. api.add_resource(Users, '/users', '/users/')
  101. api.add_resource(User, '/users/<path:user_id>', '/users/<path:user_id>/')
  102. api.add_resource(Login, '/login', '/login/')
  103. app.run(host='0.0.0.0', port=5000)