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

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


class RemoveProject(View):
    """
    remove a project from database and remove its directory
    """
    # pylint: disable=arguments-differ
    methods = ['POST']

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

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

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

        # find project
        project = Project.get_or_404(project_id)

        # remove from database
        project_dump, model_dump = project.delete()

        # send update
        self.nm.remove_model(model_dump)
        self.nm.remove_project(project_dump)

        return make_response()
