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.

78 lines
2.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from api.modules import utils
  2. from flask import Flask, request
  3. from flask_restful import Resource, Api, abort
  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. username= ""
  18. for user in users:
  19. if users[user]["id"] == args["id"]:
  20. username=user
  21. break
  22. trust = int(users[username]["trustability"])
  23. if trust > 20 or args["accepted"] == "true":
  24. return {"success":True}
  25. else:
  26. return {"success":False,"penalty":"{}".format(100*(20-trust))}
  27. class Denunciations(Resource):
  28. def get(self):
  29. return [
  30. {
  31. 'id' : v['id'],
  32. 'info': v['info'],
  33. 'priority': v['priority'],
  34. 'location' : v['location']
  35. }
  36. for v in denunciations
  37. ]
  38. class Denounce(Resource):
  39. def post(self):
  40. args = request.form
  41. reporter = args['id']
  42. if utils.find_by_id(users.values(), reporter):
  43. denunciation_info = args['info']
  44. denunciation_priority = args['priority']
  45. denunciation_location = {
  46. "latitude": args['latitude'],
  47. "longitude": args['longitude']
  48. }
  49. denunciation = {
  50. 'id': len(denunciations) + 1,
  51. 'reporter': reporter,
  52. 'info': denunciation_info,
  53. 'priority': denunciation_priority,
  54. 'location': denunciation_location
  55. }
  56. denunciations.append(denunciation)
  57. with open(db_path, 'w') as f:
  58. json.dump(denunciations, f, indent=4)
  59. return denunciation
  60. else:
  61. return {'error': 'User doesn\'t exists'}