def get_organisations(manager_user_id: int):
     if manager_user_id is None:
         """ Get all organisations """
         return Organisation.get_all_organisations()
     else:
         return Organisation.get_organisations_managed_by_user(
             manager_user_id)
    def get_organisation_by_name(organisation_name: str) -> Organisation:
        organisation = Organisation.get_organisation_by_name(organisation_name)

        if organisation is None:
            raise NotFound()

        return organisation
    def get_organisation_by_id(organisation_id: int) -> Organisation:
        org = Organisation.get(organisation_id)

        if org is None:
            raise NotFound()

        return org
Exemple #4
0
    def get_organisation_by_id_as_dto(organisation_id: int, user_id: int):
        org = Organisation.get(organisation_id)

        if org is None:
            raise NotFound()

        organisation_dto = org.as_dto()

        organisation_dto.projects = []
        organisation_dto.teams = []

        if user_id != 0:
            organisation_dto.is_manager = OrganisationService.can_user_manage_organisation(
                organisation_id, user_id)
        else:
            organisation_dto.is_manager = False

        teams = OrganisationService.get_teams_by_organisation_id(
            organisation_id)
        for team in teams:
            organisation_dto.teams.append(team.as_dto_inside_org())

        projects = OrganisationService.get_projects_by_organisation_id(
            organisation_id)
        for project in projects:
            organisation_dto.projects.append([project.id, project.name])

        return organisation_dto
Exemple #5
0
 def check_draft_project_visibility(self, authenticated_user_id: int):
     """" Check if a User is allowed to see a Draft Project """
     is_allowed_user = False
     if authenticated_user_id:
         is_team_manager = False
         user = User.get_by_id(authenticated_user_id)
         user_orgs = Organisation.get_organisations_managed_by_user(
             authenticated_user_id
         )
         if self.teams:
             for project_team in self.teams:
                 team_members = Team.get(project_team.team_id)._get_team_members()
                 for member in team_members:
                     if (
                         user.username == member["username"]
                         and member["function"] == TeamMemberFunctions.MANAGER.name
                     ):
                         is_team_manager = True
                         break
         if (
             UserRole(user.role) == UserRole.ADMIN
             or authenticated_user_id == self.author_id
             or self.organisation in user_orgs
             or is_team_manager
         ):
             is_allowed_user = True
     return is_allowed_user
    def get_organisation_by_id_as_dto(
        organisation_id: int, user_id: int, abbreviated: bool
    ):
        org = Organisation.get(organisation_id)

        if org is None:
            raise NotFound()

        organisation_dto = org.as_dto(abbreviated)

        if user_id != 0:
            organisation_dto.is_manager = (
                OrganisationService.can_user_manage_organisation(
                    organisation_id, user_id
                )
            )
        else:
            organisation_dto.is_manager = False

        if abbreviated:
            return organisation_dto

        organisation_dto.teams = [team.as_dto_inside_org() for team in org.teams]

        return organisation_dto
    def is_user_an_org_manager(organisation_id: int, user_id: int):
        """ Check that the user is an manager for the org """

        org = Organisation.get(organisation_id)

        if org is None:
            raise NotFound()
        user = UserService.get_user_by_id(user_id)

        return user in org.managers
 def create_organisation(new_organisation_dto: NewOrganisationDTO) -> int:
     """
     Creates a new organisation using an organisation dto
     :param new_organisation_dto: Organisation DTO
     :returns: ID of new Organisation
     """
     try:
         org = Organisation.create_from_dto(new_organisation_dto)
         return org.id
     except IntegrityError:
         raise OrganisationServiceError(
             f"Organisation name already exists: {new_organisation_dto.name}"
         )
Exemple #9
0
    def update(self, team_dto: TeamDTO):
        """ Updates Team from DTO """
        if team_dto.organisation:
            self.organisation = Organisation().get_organisation_by_name(
                team_dto.organisation)

        for attr, value in team_dto.items():
            if attr == "visibility" and value is not None:
                value = TeamVisibility[team_dto.visibility].value

            if attr in ("members", "organisation"):
                continue

            try:
                is_field_nullable = self.__table__.columns[attr].nullable
                if is_field_nullable and value is not None:
                    setattr(self, attr, value)
                elif value is not None:
                    setattr(self, attr, value)
            except KeyError:
                continue

        if team_dto.members != self._get_team_members() and team_dto.members:
            for member in self.members:
                db.session.delete(member)

            for member in team_dto.members:
                user = User.get_by_username(member["userName"])

                if user is None:
                    raise NotFound("User not found")

                new_team_member = TeamMembers()
                new_team_member.team = self
                new_team_member.member = user
                new_team_member.function = TeamMemberFunctions[
                    member["function"]].value

        db.session.commit()
Exemple #10
0
    def create_from_dto(cls, new_team_dto: NewTeamDTO):
        """ Creates a new team from a dto """
        new_team = cls()

        new_team.name = new_team_dto.name
        new_team.description = new_team_dto.description
        new_team.invite_only = new_team_dto.invite_only
        new_team.visibility = TeamVisibility[new_team_dto.visibility].value

        org = Organisation.get(new_team_dto.organisation_id)
        new_team.organisation = org

        # Create team member with creator as a manager
        new_member = TeamMembers()
        new_member.team = new_team
        new_member.user_id = new_team_dto.creator
        new_member.function = TeamMemberFunctions.MANAGER.value
        new_member.active = True

        new_team.members.append(new_member)

        new_team.create()
        return new_team
    def update(self, project_dto: ProjectDTO):
        """ Updates project from DTO """
        self.status = ProjectStatus[project_dto.project_status].value
        self.priority = ProjectPriority[project_dto.project_priority].value
        self.default_locale = project_dto.default_locale
        self.enforce_random_task_selection = project_dto.enforce_random_task_selection
        self.private = project_dto.private
        self.mapper_level = MappingLevel[
            project_dto.mapper_level.upper()].value
        self.changeset_comment = project_dto.changeset_comment
        self.due_date = project_dto.due_date
        self.imagery = project_dto.imagery
        self.josm_preset = project_dto.josm_preset
        self.id_presets = project_dto.id_presets
        self.last_updated = timestamp()
        self.license_id = project_dto.license_id

        if project_dto.osmcha_filter_id:
            # Support simple extraction of OSMCha filter id from OSMCha URL
            match = re.search(r"aoi=([\w-]+)", project_dto.osmcha_filter_id)
            self.osmcha_filter_id = (match.group(1) if match else
                                     project_dto.osmcha_filter_id)
        else:
            self.osmcha_filter_id = None

        if project_dto.organisation:
            org = Organisation.get(project_dto.organisation)
            if org is None:
                raise NotFound("Organisation does not exist")
            self.organisation = org

        # Cast MappingType strings to int array
        type_array = []
        for mapping_type in project_dto.mapping_types:
            type_array.append(MappingTypes[mapping_type].value)
        self.mapping_types = type_array

        # Cast Editor strings to int array
        mapping_editors_array = []
        for mapping_editor in project_dto.mapping_editors:
            mapping_editors_array.append(Editors[mapping_editor].value)
        self.mapping_editors = mapping_editors_array

        validation_editors_array = []
        for validation_editor in project_dto.validation_editors:
            validation_editors_array.append(Editors[validation_editor].value)
        self.validation_editors = validation_editors_array
        self.country = project_dto.country_tag

        # Add list of allowed users, meaning the project can only be mapped by users in this list
        if hasattr(project_dto, "allowed_users"):
            self.allowed_users = [
            ]  # Clear existing relationships then re-insert
            for user in project_dto.allowed_users:
                self.allowed_users.append(user)

        # Update teams and projects relationship.
        self.teams = []
        if hasattr(project_dto, "project_teams") and project_dto.project_teams:
            for team_dto in project_dto.project_teams:
                team = Team.get(team_dto.team_id)

                if team is None:
                    raise NotFound("Team not found")

                role = TeamRoles[team_dto.role].value
                ProjectTeams(project=self, team=team, role=role)

        # Set Project Info for all returned locales
        for dto in project_dto.project_info_locales:

            project_info = self.project_info.filter_by(
                locale=dto.locale).one_or_none()

            if project_info is None:
                new_info = ProjectInfo.create_from_dto(
                    dto)  # Can't find info so must be new locale
                self.project_info.append(new_info)
            else:
                project_info.update_from_dto(dto)

        self.priority_areas = [
        ]  # Always clear Priority Area prior to updating
        if project_dto.priority_areas:
            for priority_area in project_dto.priority_areas:
                pa = PriorityArea.from_dict(priority_area)
                self.priority_areas.append(pa)

        if project_dto.custom_editor:
            if not self.custom_editor:
                new_editor = CustomEditor.create_from_dto(
                    self.id, project_dto.custom_editor)
                self.custom_editor = new_editor
            else:
                self.custom_editor.update_editor(project_dto.custom_editor)
        else:
            if self.custom_editor:
                self.custom_editor.delete()

        # handle campaign update
        try:
            new_ids = [c.id for c in project_dto.campaigns]
            new_ids.sort()
        except TypeError:
            new_ids = []
        current_ids = [c.id for c in self.campaign]
        current_ids.sort()
        if new_ids != current_ids:
            self.campaign = Campaign.query.filter(
                Campaign.id.in_(new_ids)).all()

        if project_dto.mapping_permission:
            self.mapping_permission = MappingPermission[
                project_dto.mapping_permission.upper()].value

        if project_dto.validation_permission:
            self.validation_permission = ValidationPermission[
                project_dto.validation_permission.upper()].value

        # handle interests update
        try:
            new_ids = [c.id for c in project_dto.interests]
            new_ids.sort()
        except TypeError:
            new_ids = []
        current_ids = [c.id for c in self.interests]
        current_ids.sort()
        if new_ids != current_ids:
            self.interests = Interest.query.filter(
                Interest.id.in_(new_ids)).all()

        # try to update country info if that information is not present
        if not self.country:
            self.set_country_info()

        db.session.commit()
 def get_organisation_name_by_id(organisation_id: int) -> str:
     return Organisation.get_organisation_name_by_id(organisation_id)
 def assert_validate_name(org: Organisation, name: str):
     """ Validates that the organisation name doesn't exist """
     if org.name != name and Organisation.get_organisation_by_name(
             name) is not None:
         raise OrganisationServiceError(
             f"Organisation name already exists: {name}")
    def get_organisations_managed_by_user(user_id: int):
        """ Get all organisations a user manages """
        if UserService.is_user_an_admin(user_id):
            return Organisation.get_all_organisations()

        return Organisation.get_organisations_managed_by_user(user_id)
Exemple #15
0
 def get_organisation_by_id_as_dto(organisation_id: int, user_id: int,
                                   abbreviated: bool):
     org = Organisation.get(organisation_id)
     return OrganisationService.get_organisation_dto(
         org, user_id, abbreviated)