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.

94 lines
2.5 KiB

6 years ago
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,Response
  3. from flask_restful import Resource, Api, abort
  4. import json
  5. import os
  6. import base64
  7. from PIL import Image
  8. import cv2
  9. from io import BytesIO
  10. import numpy as np
  11. app = Flask(__name__)
  12. api = Api(app)
  13. db_path = os.path.join(app.root_path, 'databases', 'denunciations.json')
  14. with open(db_path, 'r') as f:
  15. denunciations = json.load(f)
  16. users_path = os.path.join(app.root_path, 'databases', 'users.json')
  17. with open(users_path, 'r') as f:
  18. users = json.load(f)
  19. def readb64(base64_string):
  20. sbuf = BytesIO()
  21. sbuf.write(base64.b64decode(base64_string))
  22. pimg = Image.open(sbuf)
  23. return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
  24. class Alert(Resource):
  25. def post(self):
  26. args = request.form
  27. cvimg = readb64(args["photo"])
  28. username= ""
  29. for user in users:
  30. if users[user]["id"] == args["id"]:
  31. username=user
  32. break
  33. trust = int(users[username]["trustability"])
  34. if trust > 20 or args["accepted"] == "true":
  35. return {"success":True}
  36. else:
  37. return {"success":False,"penalty":"{}".format(100*(20-trust))}
  38. class Denunciations(Resource):
  39. def get(self):
  40. resp = Response(json.dumps([
  41. {
  42. 'id' : v['id'],
  43. 'info': v['info'],
  44. 'priority': v['priority'],
  45. 'location' : v['location']
  46. }
  47. for v in denunciations
  48. ]
  49. ))
  50. resp.headers['Access-Control-Allow-Origin'] = '*'
  51. return resp
  52. class Denounce(Resource):
  53. def post(self):
  54. args = request.form
  55. reporter = args['id']
  56. if utils.find_by_id(users.values(), reporter):
  57. denunciation_info = args['info']
  58. denunciation_priority = args['priority']
  59. denunciation_location = {
  60. "latitude": args['latitude'],
  61. "longitude": args['longitude']
  62. }
  63. denunciation = {
  64. 'id': len(denunciations) + 1,
  65. 'reporter': reporter,
  66. 'info': denunciation_info,
  67. 'priority': denunciation_priority,
  68. 'location': denunciation_location
  69. }
  70. denunciations.append(denunciation)
  71. with open(db_path, 'w') as f:
  72. json.dump(denunciations, f, indent=4)
  73. return denunciation
  74. else:
  75. return {'error': 'User doesn\'t exists'}