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.

135 lines
3.7 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
  1. import os
  2. import copy
  3. import json
  4. from api.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', 'ratings.json')
  10. user_db = os.path.join(app.root_path, 'databases', 'users.json')
  11. with open(db_path, 'r') as f:
  12. ratings = json.load(f)
  13. with open(user_db, 'r') as f:
  14. users = json.load(f)
  15. class Ratings(Resource):
  16. def get(self):
  17. resp = Response(json.dumps([
  18. {
  19. 'id' : v['id'],
  20. 'name': v['name'],
  21. 'desc': v['desc'],
  22. 'img' : v['img']
  23. }
  24. for v in ratings
  25. ]))
  26. resp.headers['Access-Control-Allow-Origin'] = '*'
  27. return resp
  28. def post(self):
  29. """
  30. Example POST Data:
  31. latitude=<latitude>&
  32. longitude=<longitude>
  33. """
  34. latitude = float(request.form['latitude'])
  35. longitude = float(request.form['longitude'])
  36. ret_data = []
  37. for rating in ratings:
  38. diff = rating['location']['diff']
  39. rating_latitude = rating['location']['latitude']
  40. rating_longitude = rating['location']['longitude']
  41. if (rating_latitude - diff) < latitude < (rating_latitude + diff) and \
  42. (rating_longitude - diff) < longitude < (rating_longitude + diff):
  43. ret_data.append({
  44. 'id': rating['id'],
  45. 'name': rating['name'],
  46. 'desc': rating['desc'],
  47. 'img': rating['img']
  48. })
  49. return ret_data
  50. # def post(self):
  51. # """
  52. # Example POST Data:
  53. # name=<rating_name>&
  54. # desc=<rating_desc>& # OPTIONAL
  55. # img=<rating_img>& # OPTIONAL
  56. # """
  57. # args = request.form
  58. # rating_id = len(ratings) + 1
  59. # rating = {
  60. # 'id': rating_id,
  61. # 'name': args['name'],
  62. # 'desc': args.get('desc'),
  63. # 'img' : args.get('img'),
  64. # 'rates': []
  65. # }
  66. #
  67. # ratings.append(rating)
  68. #
  69. # with open(db_path, 'w') as f:
  70. # json.dump(ratings, f, indent=4)
  71. #
  72. # return rating
  73. class Rating(Resource):
  74. def get(self, rating_id):
  75. try:
  76. resp = Response(json.dumps(ratings[rating_id - 1]))
  77. resp.headers['Access-Control-Allow-Origin'] = '*'
  78. return resp
  79. except:
  80. abort(404, error="Rating {} doesn't exist".format(rating_id))
  81. class Rate(Resource):
  82. def post(self):
  83. """
  84. Example POST Data:
  85. rating_id=<rating_id>&
  86. score=<score>&
  87. note=<note>& # ADDITIONAL
  88. rater_id=<user_id>
  89. """
  90. if utils.find_by_id( users.values(), request.form[ 'rater_id' ] ):
  91. rating_id = int(request.form['rating_id'])
  92. score = int(request.form['score'])
  93. if 0 >= score >= 10:
  94. abort(500, 'Score should be between 0 and 10')
  95. note = request.form.get('note')
  96. ratings[rating_id - 1]['rates'][request.form['rater_id']] = {
  97. 'id': len(ratings[rating_id - 1]['rates']) + 1,
  98. 'rater': request.form['rater_id'],
  99. 'score': score,
  100. 'note': note
  101. }
  102. with open(db_path, 'w') as f:
  103. json.dump(ratings, f, indent=4)
  104. return {'message': 'Success'}
  105. return {'error': 'User doesn\'t exists'}
  106. if __name__ == '__main__':
  107. api.add_resource(Ratings, '/ratings', '/ratings/')
  108. api.add_resource(Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
  109. api.add_resource(Rate, '/rate', '/rate/')
  110. app.run(host='0.0.0.0', port=5000)