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.

118 lines
3.4 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
  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 post(self):
  17. """
  18. Example POST Data:
  19. latitude=<latitude>&
  20. longitude=<longitude>
  21. """
  22. latitude = float(request.form['latitude'])
  23. longitude = float(request.form['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. #
  55. # ratings.append(rating)
  56. #
  57. # with open(db_path, 'w') as f:
  58. # json.dump(ratings, f, indent=4)
  59. #
  60. # return rating
  61. class Rating(Resource):
  62. def get(self, rating_id):
  63. try:
  64. rating = copy.deepcopy(ratings[rating_id - 1])
  65. del rating['rates']
  66. return rating
  67. except:
  68. abort(404, error="Rating {} doesn't exist".format(rating_id))
  69. class Rate(Resource):
  70. def post(self):
  71. """
  72. Example POST Data:
  73. rating_id=<rating_id>&
  74. score=<score>&
  75. note=<note>& # ADDITIONAL
  76. rater_id=<user_id>
  77. """
  78. if utils.find_by_id( users.values(), request.form[ 'rater_id' ] ):
  79. rating_id = int(request.form['rating_id'])
  80. score = int(request.form['score'])
  81. if 0 >= score >= 10:
  82. abort(500, 'Score should be between 0 and 10')
  83. note = request.form.get('note')
  84. ratings[rating_id - 1]['rates']['9vard12ty0ad2yvwp3q53rsf3h43r2vq'] = {
  85. 'id': len(ratings[rating_id - 1]['rates']) + 1,
  86. 'rater': request.form['rater_id'],
  87. 'score': score,
  88. 'note': note
  89. }
  90. with open(db_path, 'w') as f:
  91. json.dump(ratings, f, indent=4)
  92. return {'message': 'Success'}
  93. return {'error': 'User doesn\'t exists'}
  94. if __name__ == '__main__':
  95. api.add_resource(Ratings, '/ratings', '/ratings/')
  96. api.add_resource(Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
  97. api.add_resource(Rate, '/rate', '/rate/')
  98. app.run(host='0.0.0.0', port=5000)