from flask import g, request from auth.auth_view import RegisteredAuthorizedView from projects.services.projects_service import ProjectsService from projects.services.project_history_service import ProjectHistoryService from utils.handlers import get_request_model from projects.schemas import UpdateProjectParams, project_response_schema from services.recent_search.recent_search_service import RecentSearchesService from flask_apispec import marshal_with, doc @doc(tags=["Project"]) class ProjectDetailView(RegisteredAuthorizedView): """Endpoint for retrieving, updating and deleting a project.""" service = ProjectsService() recent_search_service = RecentSearchesService() project_history_service = ProjectHistoryService() @doc(description='Retrieve project details by id') @marshal_with(project_response_schema, code=200, description='Project details') @marshal_with(schema=None, code=403, description='Forbidden because of permissions.') @marshal_with(schema=None, code=401, description='User is not authenticated.') def get(self, project_id): """Retrieve project details. Args: project_id (int): ID of the project. Returns: HTTP 200: JSON with project details. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have access to the project. User have access to a project if he created it or has the same label or this project is shared with him. HTTP 404: If project is not found. """ self.permissions.can_access_project(project_id) project = self.service.get_project_detail(project_id) self.recent_search_service.track_opened_project_details(project_id, g.user_id, request.args) return project def put(self, project_id): """Update project. Args: project_id: Project ID. Returns: HTTP 200: JSON with updated project data. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user is not the project owner. Project owner is the user who created it. HTTP 404: If project is not found. """ self.permissions.can_update_project(project_id) return self.service.update_project(project_id, get_request_model(UpdateProjectParams), g.user_id)