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.

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