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.

59 lines
1.9 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', 'crashes.json')
  9. with open(db_path, 'r') as f:
  10. crashes = 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 Crash(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(crashes) + 1,
  31. 'reporter': reporter,
  32. 'emergency': args['emergency'],
  33. 'info': denunciation_info,
  34. 'photo': photo,
  35. 'plates': args.get('plates'),
  36. 'injuries': args.get('injuries'),
  37. 'lines_blocked': args.get('lines_blocked'),
  38. 'priority': denunciation_priority,
  39. 'location': denunciation_location
  40. }
  41. crashes.append(denunciation)
  42. with open(db_path, 'w') as f:
  43. json.dump(crashes, f, indent=4)
  44. return {'success': True}
  45. else:
  46. return {'error': 'User doesn\'t exists'}
  47. else:
  48. return {"success": False, "penalty": "{}".format(100*(20-trust))}