CopyResults.py 1.4 KB

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