from flask import abort
from flask import make_response
from flask import request
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, user: str, 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.confirm(user)

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