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 RemoveResult(View):
    """
    removes a result from the database
    """
    # 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):
        result = Result.get_or_404(result_id)

        # extract request data
        data = request.get_json(force=True)

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


        dump = result.delete()

        self.nm.remove_result(dump)
        return make_response()
