from flask import g, request from auth.auth_view import RegisteredAuthorizedView from campaigns.schemas import CampaignCreateSchema, campaign_details_response_schema, campaign_create_schema from campaigns.exceptions import CampaignNotFound from campaigns.services.campaigns_service import CampaignsService from services.recent_search.recent_search_service import RecentSearchesService from flask_apispec import marshal_with, use_kwargs, doc from shared.validators import validate_UUID @doc(tags=['CampaignDetail']) class CampaignDetailView(RegisteredAuthorizedView): service = CampaignsService() recent_search_service = RecentSearchesService() """Endpoint for retrieving, updating and deleting an campaign.""" @doc(description='Retrieve campaign details') @marshal_with(campaign_details_response_schema, code=200, description='JSON with campaign details.') @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 or campaign is not found.') def get(self, project_id, campaign_uuid): """Retrieve campaign details. Args: project_id (int): Project ID. campaign_uuid (str): Campaign UUID. Returns: HTTP 200: JSON with campaign details. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have access to the project. HTTP 404: If project or campaign is not found. """ self.permissions.can_access_project(project_id) validate_UUID(campaign_uuid) campaign = self.service.get_campaign_detail(campaign_uuid, project_id) if not campaign: raise CampaignNotFound() self.recent_search_service.track_opened_campaign_details(campaign_uuid, g.user_id, request.args) return campaign """ Deprecated, needs to be rewritten to accommodate new MediaPlanPhase logic """ @doc(description='Campaign update') @use_kwargs(campaign_create_schema) @marshal_with(campaign_details_response_schema, code=200, description='Campaign 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 permission to update campaign.') @marshal_with(schema=None, code=404, description='Project or campaign is not found.') def put(self, project_id, campaign_id): """Update an campaign. Args: params: CampaignCreateSchema project_id (int): Project ID. campaign_id (int): Campaign ID. Returns: HTTP 204: If campaign has been updated successfully. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have permission to update campaign. HTTP 404: If project or campaign is not found. """ return self.no_content() self.permissions.can_edit_campaign(project_id=project_id, campaign_id=campaign_id) params = get_request_model(CampaignCreateSchema) return self.service.update_campaign( campaign_id=campaign_id, campaign_data=params, updated_by=g.user_id, project_id=project_id) @doc(description='Delete campaign') @marshal_with(schema=None, code=401, description='User is not authenticated.') @marshal_with(schema=None, code=403, description='User does not have permission to delete the campaign.') @marshal_with(schema=None, code=404, description='Project or campaign is not found.') @marshal_with(schema=None, code=204, description='Campaign deleted successfully.') def delete(self, project_id, campaign_uuid): """Delete an campaign. Args: project_id (int): Project ID. campaign_uuid (str): Campaign UUID. Returns: HTTP 204: If campaign deleted successfully. Raises: HTTP 401: If user is not authenticated. HTTP 403: If user does not have permission to delete the campaign. HTTP 404: If project or campaign is not found. """ self.permissions.can_delete_campaign(project_id) validate_UUID(campaign_uuid) self.service.delete_campaign(campaign_uuid, project_id, user_id=g.user_id) return self.no_content()