def get(self, organisation_id): """ Return statistics about projects and active tasks of an organisation --- tags: - organisations produces: - application/json parameters: - name: organisation_id in: path description: The unique organisation ID required: true type: integer default: 1 responses: 200: description: Organisation found 404: description: Organisation not found 500: description: Internal Server Error """ try: OrganisationService.get_organisation_by_id(organisation_id) organisation_dto = OrganisationService.get_organisation_stats( organisation_id) return organisation_dto.to_primitive(), 200 except NotFound: return {"Error": "Organisation Not Found"}, 404 except Exception as e: error_msg = f"Organisation GET - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
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_draft_project(draft_project_dto: DraftProjectDTO) -> int: """ Validates and then persists draft projects in the DB :param draft_project_dto: Draft Project DTO with data from API :raises InvalidGeoJson :returns ID of new draft project """ user_id = draft_project_dto.user_id is_admin = UserService.is_user_an_admin(user_id) user_orgs = OrganisationService.get_organisations_managed_by_user_as_dto( user_id) is_org_manager = len(user_orgs.organisations) > 0 # First things first, we need to validate that the author_id is a PM. issue #1715 if not (is_admin or is_org_manager): user = UserService.get_user_by_id(user_id) raise (ProjectAdminServiceError( f"User {user.username} is not permitted to create project")) # If we're cloning we'll copy all the project details from the clone, otherwise create brand new project if draft_project_dto.cloneFromProjectId: draft_project = Project.clone(draft_project_dto.cloneFromProjectId, user_id) else: draft_project = Project() org = OrganisationService.get_organisation_by_id( draft_project_dto.organisation) if org is None: raise NotFound("Organisation does not exist") draft_project_dto.organisation = org draft_project.create_draft_project(draft_project_dto) draft_project.set_project_aoi(draft_project_dto) # if arbitrary_tasks requested, create tasks from aoi otherwise use tasks in DTO if draft_project_dto.has_arbitrary_tasks: tasks = GridService.tasks_from_aoi_features( draft_project_dto.area_of_interest) draft_project.task_creation_mode = TaskCreationMode.ARBITRARY.value else: tasks = draft_project_dto.tasks ProjectAdminService._attach_tasks_to_project(draft_project, tasks) if draft_project_dto.cloneFromProjectId: draft_project.save() # Update the clone else: draft_project.create() # Create the new project draft_project.set_default_changeset_comment() draft_project.set_country_info() return draft_project.id
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 assert_validate_organisation(org_id: int): """ Makes sure an organisation exists """ try: OrganisationService.get_organisation_by_id(org_id) except NotFound: raise TeamServiceError(f"Organisation {org_id} does not exist")
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