from flask import abort
from flask import make_response
from flask.views import View
from flask.views import request

from pycs.database.Result import Result
from pycs.frontend.notifications.NotificationManager import NotificationManager


class EditResultData(View):
    """
    edit a result and set its data object
    """
    # pylint: disable=arguments-differ
    methods = ['POST']

    def __init__(self, nm: NotificationManager):
        # pylint: disable=invalid-name
        self.nm = nm

    def dispatch_request(self, result_id: int):
        # find result
        result = Result.get_or_404(result_id)

        # extract request data
        request_data = request.get_json(force=True)
        data = request_data.get('data')

        if data is None:
            abort(400, "Could not find data argument!")

        result.data = data
        result.set_origin('user', commit=True)

        self.nm.edit_result(result)
        return make_response()
