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.

134 lines
3.7 KiB

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