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.

113 lines
3.3 KiB

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