def get_organisation_by_id(organisation_id: int) -> Organisation: org = Organisation.get(organisation_id) if org is None: raise NotFound() return org
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
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_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_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)