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

from pycs.database.Database import Database
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, db: Database, nm: NotificationManager):
        # pylint: disable=invalid-name
        self.db = db
        self.nm = nm

    def dispatch_request(self, result_id: int):
        # extract request data
        data = request.get_json(force=True)

        if 'remove' not in data or data['remove'] is not True:
            abort(400)

        # find result
        result = self.db.result(result_id)
        if result is None:
            return abort(404)

        # start transaction
        with self.db:
            result.remove()

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