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

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


class EditProjectName(View):
    """
    edit a projects name
    """
    # 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)
        name = data.get('name')

        if name is None:
            abort(400, 'name argument is missing')

        project = Project.get_or_404(project_id)

        project.name = name
        project.commit()

        self.nm.edit_project(project)

        return make_response()
