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.

35 lines
988 B

6 years ago
6 years ago
  1. import os
  2. import copy
  3. import json
  4. from api.modules import utils
  5. from flask import Flask, request
  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 Resources(Resource):
  13. def post(self):
  14. user = utils.find_by_id(users.values(), request.form['user_id'])
  15. if request.form["type"] == "electricity":
  16. return {
  17. 'daily_electricity_usage': user['daily_electricity_usage'],
  18. 'ideal_electricity_usage': user['ideal_electricity_usage']
  19. }
  20. elif request.form["type"] == "water":
  21. return {
  22. 'daily_water_usage': user['daily_water_usage'],
  23. 'ideal_water_usage': user['ideal_water_usage']
  24. }
  25. if __name__ == '__main__':
  26. api.add_resource(Resources, '/resources', '/resources/')
  27. app.run(host='0.0.0.0', port=5000)