6
0

CopyResults.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from flask import abort
  2. from flask import make_response
  3. from flask import request
  4. from flask.views import View
  5. from pycs import db
  6. from pycs.database.File import File
  7. from pycs.frontend.notifications.NotificationManager import NotificationManager
  8. class CopyResults(View):
  9. """
  10. copy all results for one file to another
  11. """
  12. # pylint: disable=arguments-differ
  13. methods = ['POST']
  14. def __init__(self, nm: NotificationManager):
  15. # pylint: disable=invalid-name
  16. self.nm = nm
  17. def dispatch_request(self, user: str, file_id: int):
  18. request_data = request.get_json(force=True)
  19. if 'copy_from' not in request_data:
  20. abort(400, "copy_from argument is missing")
  21. new = []
  22. # start transaction
  23. with db.session.begin_nested():
  24. file = File.get_or_404(file_id)
  25. other_file = File.get_or_404(request_data.get('copy_from'))
  26. for result in other_file.results.all():
  27. new_result = file.create_result(
  28. origin='pipeline',
  29. result_type=result.type,
  30. origin_user=None,
  31. label=result.label_id,
  32. data=result.data,
  33. commit=False)
  34. new.append(new_result)
  35. for new_result in new:
  36. self.nm.create_result(new_result)
  37. return make_response()