def get_campaign_as_dto(campaign_id: int, user_id: int): """Gets the specified campaign""" campaign = CampaignService.get_campaign(campaign_id) campaign_dto = CampaignDTO() campaign_dto.id = campaign.id campaign_dto.url = campaign.url campaign_dto.name = campaign.name campaign_dto.logo = campaign.logo campaign_dto.description = campaign.description campaign_dto.organisations = [] orgs = ( Organisation.query.join(campaign_organisations) .filter(campaign_organisations.c.campaign_id == campaign.id) .all() ) for org in orgs: if user_id != 0: logged_in = OrganisationService.can_user_manage_organisation( org.id, user_id ) else: logged_in = False organisation_dto = OrganisationDTO() organisation_dto.organisation_id = org.id organisation_dto.name = org.name organisation_dto.logo = org.logo organisation_dto.url = org.url organisation_dto.is_manager = logged_in return campaign_dto
def delete(self, organisation_id, campaign_id): """ Unassigns an organization from an campaign --- 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 unassociated 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() ): CampaignService.delete_organisation_campaign( organisation_id, campaign_id ) return ( {"Success": "Organisation and campaign unassociated successfully"}, 200, ) else: return {"Error": "User is not a manager of the organisation"}, 403 except NotFound: return {"Error": "Organisation Campaign Not Found"}, 404 except Exception as e: error_msg = f"Organisation Campaigns DELETE - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def delete(self, organisation_id): """ Deletes an organisation --- tags: - organisations 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: The unique organisation ID required: true type: integer default: 1 responses: 200: description: Organisation deleted 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 404: description: Organisation not found 500: description: Internal Server Error """ if not OrganisationService.can_user_manage_organisation( organisation_id, token_auth.current_user() ): return {"Error": "User is not an admin for the org"}, 403 try: OrganisationService.delete_organisation(organisation_id) return {"Success": "Organisation deleted"}, 200 except OrganisationServiceError: return {"Error": "Organisation has some projects"}, 403 except NotFound: return {"Error": "Organisation Not Found"}, 404 except Exception as e: error_msg = f"Organisation DELETE - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def post(self, team_id): """ Updates a team information --- tags: - teams produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: team_id in: path description: Unique team ID required: true type: integer default: 1 - in: body name: body required: true description: JSON object for updating a team schema: properties: name: type: string default: HOT - Mappers logo: type: string default: https://tasks.hotosm.org/assets/img/hot-tm-logo.svg members: type: array items: schema: $ref: "#/definitions/TeamMembers" organisation: type: string default: HOT description: type: string default: HOT's mapping editors inviteOnly: type: boolean default: false responses: 201: description: Team updated successfully 400: description: Client Error - Invalid Request 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: team_dto = TeamDTO(request.get_json()) team_dto.team_id = team_id team_dto.validate() authenticated_user_id = token_auth.current_user() team_details_dto = TeamService.get_team_as_dto( team_id, authenticated_user_id) org = TeamService.assert_validate_organisation( team_dto.organisation_id) TeamService.assert_validate_members(team_details_dto) if not TeamService.is_user_team_manager( team_id, authenticated_user_id ) and not OrganisationService.can_user_manage_organisation( org.id, authenticated_user_id): return { "Error": "User is not a admin or a manager for the team" }, 401 except DataError as e: current_app.logger.error(f"error validating request: {str(e)}") return str(e), 400 try: TeamService.update_team(team_dto) return {"Status": "Updated"}, 200 except NotFound as e: return {"Error": str(e)}, 404 except TeamServiceError as e: return str(e), 402 except Exception as e: error_msg = f"Team POST - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def patch(self, organisation_id): """ Updates an organisation --- tags: - organisations 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: The unique organisation ID required: true type: integer default: 1 - in: body name: body required: true description: JSON object for updating an organisation schema: properties: name: type: string default: HOT slug: type: string default: HOT logo: type: string default: https://tasks.hotosm.org/assets/img/hot-tm-logo.svg url: type: string default: https://hotosm.org managers: type: array items: type: string default: [ user_1, user_2 ] responses: 201: description: Organisation updated successfully 400: description: Client Error - Invalid Request 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 500: description: Internal Server Error """ if not OrganisationService.can_user_manage_organisation( organisation_id, token_auth.current_user()): return {"Error": "User is not an admin for the org"}, 403 try: organisation_dto = UpdateOrganisationDTO(request.get_json()) organisation_dto.organisation_id = organisation_id # Don't update organisation type and subscription_tier if request user is not an admin if User.get_by_id(token_auth.current_user()).role != 1: org = OrganisationService.get_organisation_by_id( organisation_id) organisation_dto.type = OrganisationType(org.type).name organisation_dto.subscription_tier = org.subscription_tier organisation_dto.validate() except DataError as e: current_app.logger.error(f"error validating request: {str(e)}") return str(e), 400 try: OrganisationService.update_organisation(organisation_dto) return {"Status": "Updated"}, 200 except NotFound as e: return {"Error": str(e)}, 404 except OrganisationServiceError as e: return str(e), 402 except Exception as e: error_msg = f"Organisation PATCH - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
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 CampaignService.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