def create_campaign(campaign_dto: NewCampaignDTO): campaign = Campaign.from_dto(campaign_dto) campaign.create() if campaign_dto.organisations: for org_id in campaign_dto.organisations: organisation = OrganisationService.get_organisation_by_id( org_id) campaign.organisation.append(organisation) db.session.commit() return campaign
def create_campaign(campaign_dto: NewCampaignDTO): campaign = Campaign.from_dto(campaign_dto) try: campaign.create() if campaign_dto.organisations: for org_id in campaign_dto.organisations: organisation = OrganisationService.get_organisation_by_id(org_id) campaign.organisation.append(organisation) db.session.commit() except IntegrityError as e: current_app.logger.info("Integrity error: {}".format(e.args[0])) raise ValueError() return campaign
def create_campaign(campaign_dto: NewCampaignDTO): """ Creates a new campaign """ campaign = Campaign.from_dto(campaign_dto) try: campaign.create() if campaign_dto.organisations: for org_id in campaign_dto.organisations: organisation = OrganisationService.get_organisation_by_id( org_id) campaign.organisation.append(organisation) db.session.commit() except IntegrityError as e: current_app.logger.info("Integrity error: {}".format(e.args[0])) if isinstance(e.orig, UniqueViolation): raise ValueError("Campaign name already exists") from e if isinstance(e.orig, NotNullViolation): raise ValueError("Campaign name cannot be null") from e return campaign
def get_all_campaigns() -> CampaignListDTO: """ Returns a list of all campaigns """ query = Campaign.query.order_by(Campaign.name).distinct() return Campaign.campaign_list_as_dto(query)
def get_project_campaigns_as_dto(project_id: int) -> CampaignListDTO: """Gets all the campaigns for a specified project""" query = (Campaign.query.join(campaign_projects).filter( campaign_projects.c.project_id == project_id).all()) return Campaign.campaign_list_as_dto(query)
def get_organisation_campaigns_as_dto( organisation_id: int) -> CampaignListDTO: """ Gets all the campaigns for a specified project """ query = (Campaign.query.join(campaign_organisations).filter( campaign_organisations.c.organisation_id == organisation_id).all()) return Campaign.campaign_list_as_dto(query)
def get_all_campaigns() -> CampaignListDTO: """List all campaigns""" return Campaign.get_all_campaigns()
def get_project_campaigns_as_dto(project_id: int) -> CampaignListDTO: """Gets all the campaigns for a specified project""" return Campaign.get_project_campaigns_as_dto(project_id)
def get_organisation_campaigns_as_dto(organisation_id: int) -> CampaignListDTO: """ Gets all the campaigns for a specified project """ return Campaign.get_organisation_campaigns_as_dto(organisation_id)
def post(self, organisation_id, campaign_id): """ Assigns a campaign to an organisation --- tags: - campaigns produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: organisation_id in: path description: Unique organisation ID required: true type: integer default: 1 - name: campaign_id in: path description: Unique campaign ID required: true type: integer default: 1 responses: 200: description: Organisation and campaign assigned successfully 401: description: Unauthorized - Invalid credentials 403: description: Forbidden - users have submitted mapping 404: description: Project not found 500: description: Internal Server Error """ try: if OrganisationService.can_user_manage_organisation( organisation_id, token_auth.current_user()): if Campaign.campaign_organisation_exists( campaign_id, organisation_id): message = "Campaign {} is already assigned to organisation {}.".format( campaign_id, organisation_id) return {"Error": message}, 400 CampaignService.create_campaign_organisation( organisation_id, campaign_id) message = "campaign with id {} assigned for organisation with id {}".format( campaign_id, organisation_id) return {"Success": message}, 200 else: return { "Error": "User is not a manager of the organisation" }, 403 except Exception as e: error_msg = f"Campaign Organisation POST - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500