CreateResult.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from flask import request, abort, jsonify
  2. from flask.views import View
  3. from pycs.database.File import File
  4. from pycs.database.Result import Result
  5. from pycs.frontend.notifications.NotificationManager import NotificationManager
  6. class CreateResult(View):
  7. """
  8. create a result for a file
  9. """
  10. # pylint: disable=arguments-differ
  11. methods = ['POST']
  12. def dispatch_request(self, file_id: int):
  13. # extract request data
  14. request_data = request.get_json(force=True)
  15. if request_data.get('type') not in ['labeled-image', 'bounding-box']:
  16. return abort(400)
  17. rtype = request_data['type']
  18. if 'label' in request_data and request_data['label']:
  19. label = request_data['label']
  20. elif request_data['type'] == 'labeled-image':
  21. return abort(400, "label missing for the labeled-image annotation")
  22. else:
  23. label = None
  24. if request_data.get('data'):
  25. data = request_data['data']
  26. elif request_data['type'] == 'bounding-box':
  27. return abort(400, "data missing for the bounding box annotation")
  28. else:
  29. data = {}
  30. # find file
  31. file = File.query.get(file_id)
  32. if file is None:
  33. return abort(404)
  34. removed = []
  35. # find full-image labels and remove them
  36. results = file.results.filter_by(type='labeled-image')
  37. for result in results.all():
  38. removed.append(result.serialize())
  39. results.delete()
  40. # insert into database
  41. new_result = file.create_result('user', rtype, label, data)
  42. for result in removed:
  43. NotificationManager.removed("result", result)
  44. NotificationManager.created("result", new_result.id, Result)
  45. return jsonify(new_result)