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.

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