from auth.auth_view import RegisteredAuthorizedView from media_plan.schemas import ( phase_schema, create_update_phase_request_schema, media_plan_phase_schema ) from media_plan.phases_service import PhasesService from media_plan.exceptions import PhaseNotFound from flask_apispec import marshal_with, use_kwargs, doc from media_plan.validators import ProjectMediaPlanValidator @doc(tags=['Phase']) class PhaseView(RegisteredAuthorizedView): phases_service = PhasesService() validator = ProjectMediaPlanValidator() @doc(description='Retrieve phase') @marshal_with(media_plan_phase_schema, code=200, description='JSON with phase.') @marshal_with(schema=None, code=401, description='User is not authenticated.') @marshal_with(schema=None, code=403, description='User does not have access to the project') @marshal_with(schema=None, code=404, description='Phase is not found.') def get(self, project_id: int, media_plan_id: int, phase_id: int): self.permissions.can_access_project(project_id) self.validator.validate_project_media_plan(project_id, media_plan_id) self.validator.validate_phase(phase_id) phase_response = self.phases_service.get_phase(media_plan_id, phase_id) if phase_response is None: raise PhaseNotFound() return phase_response @doc(description='Update phase') @use_kwargs(create_update_phase_request_schema) @marshal_with(media_plan_phase_schema, code=200, description='Phase has been updated successfully.') @marshal_with(schema=None, code=401, description='User is not authenticated.') @marshal_with(schema=None, code=403, description='User does not have access to the project') @marshal_with(schema=None, code=404, description='Phase is not found.') def patch(self, params, project_id: int, media_plan_id: int, phase_id: int): self.permissions.can_access_project(project_id) self.validator.validate_project_media_plan(project_id, media_plan_id) self.validator.validate_phase(phase_id) self.permissions.can_update_phases(media_plan_id) return self.phases_service.update_phase(project_id, phase_id, params) @doc(description='Delete phase') @marshal_with(schema=None, code=204, description='Phase deleted successfully.') @marshal_with(schema=None, code=401, description='User is not authenticated.') @marshal_with(schema=None, code=403, description='User does not have access to the phase') @marshal_with(schema=None, code=404, description='Phase is not found.') def delete(self, project_id: int, media_plan_id: int, phase_id: int): self.permissions.can_access_project(project_id) self.validator.validate_project_media_plan(project_id, media_plan_id) self.validator.validate_phase(phase_id) self.permissions.can_update_phases(media_plan_id) self.phases_service.delete_phase(project_id, media_plan_id, phase_id) return self.no_content() @doc(tags=['Phases']) class PhasesView(RegisteredAuthorizedView): phases_service = PhasesService() validator = ProjectMediaPlanValidator() @doc(description='Retrieve phases') @marshal_with(phase_schema(many=True), code=200, description='JSON with phases') @marshal_with(schema=None, code=401, description='User is not authenticated') @marshal_with(schema=None, code=403, description='User does not have access to the project') @marshal_with(schema=None, code=404, description='Project is not found') def get(self, project_id: int, media_plan_id: int): self.permissions.can_access_project(project_id) self.validator.validate_project_media_plan(project_id, media_plan_id) self.permissions.can_access_media_plan(media_plan_id) return self.phases_service.get_phases(media_plan_id) @doc(description='Create phases') @use_kwargs(create_update_phase_request_schema) @marshal_with(media_plan_phase_schema, code=201, description='JSON with phases') @marshal_with(schema=None, code=401, description='User is not authenticated') @marshal_with(schema=None, code=403, description='User does not have access to the project') @marshal_with(schema=None, code=404, description='Project is not found') def post(self, params, project_id: int, media_plan_id: int): self.permissions.can_access_project(project_id) self.validator.validate_project_media_plan(project_id, media_plan_id) self.permissions.can_update_phases(media_plan_id) return self.phases_service.create_phase(project_id, media_plan_id, params)