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.

119 lines
3.5 KiB

6 years ago
  1. import os
  2. import copy
  3. import json
  4. from modules import utils
  5. from flask import Flask, request, Response
  6. from flask_restful import Resource, Api, abort
  7. app = Flask(__name__)
  8. api = Api(app)
  9. db_path = os.path.join(app.root_path, 'databases', 'users.json')
  10. with open(db_path, 'r') as f:
  11. users = json.load(f)
  12. class Users(Resource):
  13. def post(self):
  14. """
  15. Example POST Data:
  16. username=<username>&
  17. password=<password>&
  18. realname=<realname>& # OPTIONAL
  19. avatar=<avatar_url>& # OPTIONAL
  20. """
  21. args = request.form
  22. user_id = utils.generate_id()
  23. user = {
  24. 'id': user_id,
  25. 'username': args['username'],
  26. 'realname': args.get('realname'),
  27. 'avatar' : args.get('avatar'),
  28. 'password': utils.md5(args['password']),
  29. 'stats': {
  30. 'bus_usage_week': 0,
  31. 'bus_usage_month': 0,
  32. 'bus_usage_year': 0
  33. },
  34. 'daily_electricity_usage': [],
  35. 'points': 0
  36. }
  37. users.append(user)
  38. with open(db_path, 'w') as f:
  39. json.dump(users, f, indent=2)
  40. return user
  41. class User(Resource):
  42. def get(self, user_id):
  43. try:
  44. user = copy.deepcopy(utils.find_by_id(users.values(), user_id))
  45. if not user:
  46. raise Exception('User not found!')
  47. del user['password']
  48. resp = Response(json.dumps(user))
  49. resp.headers['Access-Control-Allow-Origin'] = '*'
  50. return resp
  51. except:
  52. abort(404, error="User {} doesn't exist".format(user_id))
  53. class Login(Resource):
  54. def post(self):
  55. """
  56. Example POST Data:
  57. username=<username>&
  58. password=<password>
  59. """
  60. #Password for efe is 12345
  61. args = request.form
  62. username = args['username']
  63. password = utils.md5(args['password'])
  64. if not username in users:
  65. return [False, {}]
  66. user = copy.deepcopy(users[username])
  67. if user['password'] == password:
  68. del user["password"]
  69. return [True, json.dumps(user)]
  70. else:
  71. return [False, {}]
  72. class ReducePoints(Resource):
  73. def post(self):
  74. user_id = request.form['id']
  75. user = utils.find_by_id(users.values(), user_id)
  76. if user:
  77. username = ''
  78. for k, v in users.items():
  79. if user_id == v['id']:
  80. username = k
  81. users[username]['points'] -= int(request.form['reduce'])
  82. with open(db_path, 'w') as f:
  83. json.dump(users, f, indent=2)
  84. else:
  85. abort(404, error="User {} doesn't exist".format(user_id))
  86. class Image(Resource):
  87. def get(self, user_id):
  88. try:
  89. user = copy.deepcopy(utils.find_by_id(users.values(), user_id))
  90. if not user:
  91. raise Exception('User not found!')
  92. del user['password']
  93. resp = Response(user["image"])
  94. resp.headers['Access-Control-Allow-Origin'] = '*'
  95. return resp
  96. except:
  97. abort(404, error="User {} doesn't exist".format(user_id))
  98. if __name__ == '__main__':
  99. api.add_resource(Users, '/users', '/users/')
  100. api.add_resource(User, '/users/<path:user_id>', '/users/<path:user_id>/')
  101. api.add_resource(Login, '/login', '/login/')
  102. app.run(host='0.0.0.0', port=5000)