Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for projects by exact mapping type #3904

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/api/projects/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ def setup_search_dto(self):
search_dto.mapping_types = map(
str, mapping_types_str.split(",")
) # Extract list from string
search_dto.mapping_types_exact = strtobool(
willemarcel marked this conversation as resolved.
Show resolved Hide resolved
request.args.get("mappingTypesExact", "false")
)
project_statuses_str = request.args.get("projectStatuses")
if project_statuses_str:
search_dto.project_statuses = map(str, project_statuses_str.split(","))
Expand Down Expand Up @@ -569,6 +572,11 @@ def get(self):
- in: query
name: mappingTypes
type: string
- in: query
name: mappingTypesExact
type: boolean
default: false
description: if true, limits projects to match the exact mapping types requested
- in: query
name: organisationName
description: Organisation name to search for
Expand Down
1 change: 1 addition & 0 deletions backend/models/dtos/project_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class ProjectSearchDTO(Model):
preferred_locale = StringType(default="en")
mapper_level = StringType(validators=[is_known_mapping_level])
mapping_types = ListType(StringType, validators=[is_known_mapping_type])
mapping_types_exact = BooleanType(required=False)
project_statuses = ListType(StringType, validators=[is_known_project_status])
organisation_name = StringType()
organisation_id = IntType()
Expand Down
19 changes: 14 additions & 5 deletions backend/services/project_search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,21 @@ def _filter_projects(search_dto: ProjectSearchDTO, user):
if search_dto.mapping_types:
# Construct array of mapping types for query
mapping_type_array = []
mapping_type_array = [
MappingTypes[mapping_type].value
for mapping_type in search_dto.mapping_types
]

query = query.filter(Project.mapping_types.contains(mapping_type_array))
if search_dto.mapping_types_exact:
mapping_type_array = [
{
MappingTypes[mapping_type].value
for mapping_type in search_dto.mapping_types
}
]
query = query.filter(Project.mapping_types.in_(mapping_type_array))
else:
mapping_type_array = [
MappingTypes[mapping_type].value
for mapping_type in search_dto.mapping_types
]
query = query.filter(Project.mapping_types.overlap(mapping_type_array))

if search_dto.text_search:
# We construct an OR search, so any projects that contain or more of the search terms should be returned
Expand Down