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

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


class ConfirmResult(View):
    """
    confirm a result (change its origin to user)
    """
    # 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
        data = request.get_json(force=True)


        if not data.get('confirm', False):
            return abort(400, "confirm flag is missing")


        result.set_origin('user')

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