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

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


class EditProjectDescription(View):
    """
    edit a projects description
    """
    # 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, identifier):
        # extract request data
        data = request.get_json(force=True)

        if 'description' not in data or not data['description']:
            return abort(400)

        # start transaction
        with self.db:
            # find project
            project = self.db.project(identifier)
            if project is None:
                return abort(404)

            # set description
            project.set_description(data['description'])
            self.nm.edit_project(project)

        return make_response()
