def search_projects(
            search_dto: ProjectSearchDTO) -> ProjectSearchResultsDTO:
        """ Searches all projects for matches to the criteria provided by the user """

        all_results, paginated_results = ProjectSearchService._filter_projects(
            search_dto)

        if paginated_results.total == 0:
            raise NotFound()

        features = []
        for project in all_results:
            # This loop creates a geojson feature collection so you can see all active projects on the map
            properties = {
                "projectId": project.id,
                "priority": ProjectPriority(project.priority).name
            }
            centroid = project.centroid
            feature = geojson.Feature(geometry=geojson.loads(project.centroid),
                                      properties=properties)
            features.append(feature)
        feature_collection = geojson.FeatureCollection(features)
        dto = ProjectSearchResultsDTO()
        dto.map_results = feature_collection

        for project in paginated_results.items:
            # This loop loads the paginated text results
            # TODO would be nice to get this for an array rather than individually would be more efficient
            project_info_dto = ProjectInfo.get_dto_for_locale(
                project.id, search_dto.preferred_locale,
                project.default_locale)

            list_dto = ListSearchResultDTO()
            list_dto.project_id = project.id
            list_dto.locale = project_info_dto.locale
            list_dto.name = project_info_dto.name
            list_dto.priority = ProjectPriority(project.priority).name
            list_dto.mapper_level = MappingLevel(project.mapper_level).name
            list_dto.short_description = project_info_dto.short_description
            list_dto.organisation_tag = project.organisation_tag
            list_dto.campaign_tag = project.campaign_tag
            list_dto.percent_mapped = Project.calculate_tasks_percent(
                'mapped', project.total_tasks, project.tasks_mapped,
                project.tasks_validated, project.tasks_bad_imagery)
            list_dto.percent_validated = Project.calculate_tasks_percent(
                'validated', project.total_tasks, project.tasks_mapped,
                project.tasks_validated, project.tasks_bad_imagery)
            list_dto.status = ProjectStatus(project.status).name
            list_dto.active_mappers = Project.get_active_mappers(project.id)

            dto.results.append(list_dto)

        dto.pagination = Pagination(paginated_results)
        return dto
    def _get_project_and_base_dto(self, project_id):
        """ Populates a project DTO with properties common to all roles """
        project = Project.get(project_id)

        if project is None:
            return None, None

        aoi = project.area_of_interest

        base_dto = ProjectDTO()
        base_dto.project_id = project_id
        base_dto.project_status = ProjectStatus(project.status).name
        base_dto.default_locale = project.default_locale
        base_dto.project_priority = ProjectPriority(project.priority).name
        base_dto.area_of_interest = aoi.get_aoi_geometry_as_geojson()
        base_dto.enforce_mapper_level = project.enforce_mapper_level
        base_dto.enforce_validator_role = project.enforce_validator_role
        base_dto.private = project.private
        base_dto.mapper_level = MappingLevel(project.mapper_level).name
        base_dto.entities_to_map = project.entities_to_map
        base_dto.changeset_comment = project.changeset_comment
        base_dto.due_date = project.due_date
        base_dto.imagery = project.imagery
        base_dto.josm_preset = project.josm_preset
        base_dto.campaign_tag = project.campaign_tag
        base_dto.organisation_tag = project.organisation_tag
        base_dto.license_id = project.license_id
        base_dto.last_updated = project.last_updated
        base_dto.author = User().get_by_id(project.author_id).username

        if project.private:
            # If project is private it should have a list of allowed users
            allowed_usernames = []
            for user in project.allowed_users:
                allowed_usernames.append(user.username)
            base_dto.allowed_usernames = allowed_usernames

        if project.mapping_types:
            mapping_types = []
            for mapping_type in project.mapping_types:
                mapping_types.append(MappingTypes(mapping_type).name)

            base_dto.mapping_types = mapping_types

        if project.priority_areas:
            geojson_areas = []
            for priority_area in project.priority_areas:
                geojson_areas.append(priority_area.get_as_geojson())

            base_dto.priority_areas = geojson_areas

        return project, base_dto
Example #3
0
    def _get_project_and_base_dto(self):
        """ Populates a project DTO with properties common to all roles """
        base_dto = ProjectDTO()
        base_dto.project_id = self.id
        base_dto.project_status = ProjectStatus(self.status).name
        base_dto.default_locale = self.default_locale
        base_dto.project_priority = ProjectPriority(self.priority).name
        base_dto.area_of_interest = self.get_aoi_geometry_as_geojson()
        base_dto.enforce_mapper_level = self.enforce_mapper_level
        base_dto.enforce_validator_role = self.enforce_validator_role
        base_dto.private = self.private
        base_dto.mapper_level = MappingLevel(self.mapper_level).name
        base_dto.entities_to_map = self.entities_to_map
        base_dto.changeset_comment = self.changeset_comment
        base_dto.due_date = self.due_date
        base_dto.imagery = self.imagery
        base_dto.josm_preset = self.josm_preset
        base_dto.campaign_tag = self.campaign_tag
        base_dto.organisation_tag = self.organisation_tag
        base_dto.license_id = self.license_id
        base_dto.created = self.created
        base_dto.last_updated = self.last_updated
        base_dto.id_custom_imagery = self.id_custom_imagery
        base_dto.id_custom_presets = self.id_custom_presets
        base_dto.id_min_editable_zoom = self.id_min_editable_zoom
        base_dto.author = User().get_by_id(self.author_id).username
        base_dto.active_mappers = Project.get_active_mappers(self.id)
        base_dto.task_creation_mode = TaskCreationMode(
            self.task_creation_mode).name

        if self.private:
            # If project is private it should have a list of allowed users
            allowed_usernames = []
            for user in self.allowed_users:
                allowed_usernames.append(user.username)
            base_dto.allowed_usernames = allowed_usernames

        if self.mapping_types:
            mapping_types = []
            for mapping_type in self.mapping_types:
                mapping_types.append(MappingTypes(mapping_type).name)

            base_dto.mapping_types = mapping_types

        if self.priority_areas:
            geojson_areas = []
            for priority_area in self.priority_areas:
                geojson_areas.append(priority_area.get_as_geojson())

            base_dto.priority_areas = geojson_areas

        return self, base_dto
Example #4
0
    def search_projects(
            search_dto: ProjectSearchDTO) -> ProjectSearchResultsDTO:
        """ Searches all projects for matches to the criteria provided by the user """

        filtered_projects = ProjectSearchService._filter_projects(search_dto)

        if filtered_projects.total == 0:
            raise NotFound()

        dto = ProjectSearchResultsDTO()
        for project in filtered_projects.items:
            # TODO would be nice to get this for an array rather than individually would be more efficient
            project_info_dto = ProjectInfo.get_dto_for_locale(
                project.id, search_dto.preferred_locale,
                project.default_locale)

            result_dto = ProjectSearchResultDTO()
            result_dto.project_id = project.id
            result_dto.locale = project_info_dto.locale
            result_dto.name = project_info_dto.name
            result_dto.priority = ProjectPriority(project.priority).name
            result_dto.mapper_level = MappingLevel(project.mapper_level).name
            result_dto.short_description = project_info_dto.short_description
            result_dto.aoi_centroid = geojson.loads(project.centroid)
            result_dto.organisation_tag = project.organisation_tag
            result_dto.campaign_tag = project.campaign_tag
            result_dto.percent_mapped = round(
                (project.tasks_mapped /
                 (project.total_tasks - project.tasks_bad_imagery)) * 100, 0)
            result_dto.percent_validated = round(
                ((project.tasks_validated + project.tasks_bad_imagery) /
                 project.total_tasks) * 100, 0)

            dto.results.append(result_dto)

        dto.pagination = Pagination(filtered_projects)
        return dto