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.

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