from flask import g from auth.auth_view import RegisteredAuthorizedView from utils.exceptions import ValidationError from constants.http_status import HTTP_201_CREATED from campaigns.schemas import ( campaign_response_schema, campaign_list_query_params, campaign_create_schema, campaign_actions_reqest_schema, campaign_details_response_schema ) from campaigns.services.campaigns_service import CampaignsService from flask_apispec import marshal_with, use_kwargs, doc @doc(tags=['CampaignList']) class CampaignListView(RegisteredAuthorizedView): """Endpoint for getting list of campaigns and creating new campaigns.""" service = CampaignsService() @doc(description='Retrieve list of project campaigns') @use_kwargs(campaign_list_query_params, location='query') @marshal_with(campaign_response_schema, code=200, description='JSON with campaign list.') @marshal_with(None, code=401, description='User is not authenticated.') @marshal_with(None, code=403, description='User does not have access to the project.') @marshal_with(None, code=404, description='Project is not found.') def get(self, params, project_id): """Retrieve list of project campaigns. Args: params: CampaignListQueryParams project_id (int): Project ID. Returns: HTTP 200: JSON with campaign list. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have access to the project. HTTP 404: If project is not found. """ self.permissions.can_access_project(project_id) if params.earliestStartDate and params.latestEndDate and params.earliestStartDate > params.latestEndDate: raise ValidationError("End date must be greater than or equal to start date.") return self.service.get_campaigns_by_project(project_id, params) @doc(description='Create new campaign for a given project') @use_kwargs(campaign_create_schema) @marshal_with(campaign_details_response_schema, code=201, description='JSON with ID of the new campaign.') @marshal_with(None, code=401, description='User is not authenticated.') @marshal_with(None, code=403, description='User does not have access to the project.') @marshal_with(None, code=404, description='Project is not found.') def post(self, params, project_id): """Create new campaign for a given project. Args: params: CampaignCreateSchema project_id (int): Project ID. Returns: HTTP 201: JSON with ID of the new campaign. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have access to the project. HTTP 404: If project is not found. """ return self.no_content() self.permissions.can_create_campaign(project_id) campaign = self.service.create_campaign(project_id, created_by=g.user_id, campaign_data=params) return campaign, HTTP_201_CREATED @doc(description='Delete campaign') @use_kwargs(campaign_actions_reqest_schema, location='query') @marshal_with(None, code=204, description='Campaign deleted successfully.') @marshal_with(None, code=401, description='User is not authenticated.') @marshal_with(None, code=403, description='User does not have access to the project.') @marshal_with(None, code=404, description='Project is not found.') def delete(self, params, project_id): self.permissions.can_delete_campaign(project_id) self.service.delete_campaign(params.campaignUuid, project_id, user_id=g.user_id) return self.no_content()