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.

77 lines
2.3 KiB

6 years ago
  1. from modules import utils
  2. from flask import Flask, request,Response
  3. from flask_restful import Resource, Api
  4. import json
  5. import os
  6. app = Flask(__name__)
  7. api = Api(app)
  8. db_path = os.path.join(app.root_path, 'databases', 'denunciations.json')
  9. with open(db_path, 'r') as f:
  10. denunciations = json.load(f)
  11. users_path = os.path.join(app.root_path, 'databases', 'users.json')
  12. with open(users_path, 'r') as f:
  13. users = json.load(f)
  14. class Alert(Resource):
  15. def post(self):
  16. args = request.form
  17. reporter = args['id']
  18. user = utils.find_by_id(users.values(), reporter)
  19. trust = int(user["trustability"])
  20. if args["accepted"] == "true" or trust > 20:
  21. photo = args["photo"]
  22. if utils.find_by_id(users.values(), reporter):
  23. denunciation_info = args['note']
  24. denunciation_priority = 5
  25. denunciation_location = {
  26. "latitude": float(args['latitude']),
  27. "longitude": float(args['longitude'])
  28. }
  29. denunciation = {
  30. 'id': len(denunciations) + 1,
  31. 'reporter': reporter,
  32. 'emergency': args['emergency'],
  33. 'info': denunciation_info,
  34. 'photo': photo,
  35. 'priority': denunciation_priority,
  36. 'location': denunciation_location
  37. }
  38. denunciations.append(denunciation)
  39. with open(db_path, 'w') as f:
  40. json.dump(denunciations, f, indent=2)
  41. return {'success': True}
  42. else:
  43. return {'error': 'User doesn\'t exists'}
  44. else:
  45. return {"success": False, "penalty": "{}".format(100*(20-trust))}
  46. class Denunciations(Resource):
  47. def get(self):
  48. resp = Response(json.dumps([
  49. {
  50. 'id': v['id'],
  51. "reporter": v["reporter"],
  52. 'info': v['info'],
  53. 'priority': v['priority'],
  54. 'emergency': v['emergency'],
  55. 'photo': v['photo'],
  56. 'location': v['location']
  57. }
  58. for v in denunciations
  59. ]
  60. ))
  61. resp.headers['Access-Control-Allow-Origin'] = '*'
  62. return resp