from flask import request, abort, jsonify from flask.views import View from pycs import db from pycs.database.File import File from pycs.database.Result import Result from pycs.frontend.notifications.NotificationManager import NotificationManager class CreateResult(View): """ create a result for a file """ # pylint: disable=arguments-differ methods = ['POST'] def dispatch_request(self, file_id: int): # extract request data request_data = request.get_json(force=True) if request_data.get('type') not in ['labeled-image', 'bounding-box']: return abort(400) rtype = request_data['type'] if 'label' in request_data and request_data['label']: label = request_data['label'] elif request_data['type'] == 'labeled-image': return abort(400, "label missing for the labeled-image annotation") else: label = None if request_data.get('data'): data = request_data['data'] elif request_data['type'] == 'bounding-box': return abort(400, "data missing for the bounding box annotation") else: data = {} # find file file = File.query.get(file_id) if file is None: return abort(404) removed = [] # start transaction with db.session.begin_nested(): # find full-image labels and remove them for result in file.results.all(): if result.type == 'labeled-image': removed.append(result.serialize()) result.remove(commit=True) # insert into database new_result = file.create_result('user', rtype, label, data, commit=False) for result in removed: NotificationManager.removed("result", result.serialize()) NotificationManager.created("result", new_result.id, Result) return jsonify(new_result)