Skip to content

Commit

Permalink
Fix: Use old query for other uses of create_search_query
Browse files Browse the repository at this point in the history
  • Loading branch information
bshankar committed Sep 30, 2024
1 parent f43feab commit e9b84d4
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions backend/services/project_search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,62 @@ def __init__(self, message):

class ProjectSearchService:
@staticmethod
def create_search_query(user=None):
query = (
db.session.query(
Project.id.label("id"),
ProjectInfo.name.label("project_name"),
Project.difficulty,
Project.priority,
Project.default_locale,
Project.centroid.ST_AsGeoJSON().label("centroid"),
Project.organisation_id,
Project.tasks_bad_imagery,
Project.tasks_mapped,
Project.tasks_validated,
Project.percent_mapped,
Project.percent_validated,
Project.status,
Project.total_tasks,
Project.last_updated,
Project.due_date,
Project.country,
Organisation.name.label("organisation_name"),
Organisation.logo.label("organisation_logo"),
Project.created.label("creation_date"),
func.coalesce(
func.sum(func.ST_Area(Project.geometry, True) / 1000000)
).label("total_area"),
def create_search_query(user=None, as_csv: bool = False):
if as_csv:
query = (
db.session.query(
Project.id.label("id"),
ProjectInfo.name.label("project_name"),
Project.difficulty,
Project.priority,
Project.default_locale,
Project.centroid.ST_AsGeoJSON().label("centroid"),
Project.organisation_id,
Project.tasks_bad_imagery,
Project.tasks_mapped,
Project.tasks_validated,
Project.percent_mapped,
Project.percent_validated,
Project.status,
Project.total_tasks,
Project.last_updated,
Project.due_date,
Project.country,
Organisation.name.label("organisation_name"),
Organisation.logo.label("organisation_logo"),
Project.created.label("creation_date"),
func.coalesce(
func.sum(func.ST_Area(Project.geometry, True) / 1000000)
).label("total_area"),
)
.filter(Project.geometry is not None)
.outerjoin(Organisation, Organisation.id == Project.organisation_id)
.group_by(Organisation.id, Project.id, ProjectInfo.name)
)
else:
query = (
db.session.query(
Project.id.label("id"),
Project.difficulty,
Project.priority,
Project.default_locale,
Project.centroid.ST_AsGeoJSON().label("centroid"),
Project.organisation_id,
Project.tasks_bad_imagery,
Project.tasks_mapped,
Project.tasks_validated,
Project.status,
Project.total_tasks,
Project.last_updated,
Project.due_date,
Project.country,
Organisation.name.label("organisation_name"),
Organisation.logo.label("organisation_logo"),
)
.filter(Project.geometry is not None)
.outerjoin(Organisation, Organisation.id == Project.organisation_id)
.group_by(Organisation.id, Project.id)
)
.filter(Project.geometry is not None)
.outerjoin(Organisation, Organisation.id == Project.organisation_id)
.group_by(Organisation.id, Project.id, ProjectInfo.name)
)

# Get public projects only for anonymous user.
if user is None:
Expand Down Expand Up @@ -209,7 +234,7 @@ def get_total_contributions(paginated_results):
@staticmethod
@cached(csv_download_cache)
def search_projects_as_csv(search_dto: ProjectSearchDTO, user) -> str:
all_results, _ = ProjectSearchService._filter_projects(search_dto, user)
all_results, _ = ProjectSearchService._filter_projects(search_dto, user, True)
rows = [row._asdict() for row in all_results]
is_user_admin = user is not None and user.role == UserRole.ADMIN.value

Expand All @@ -218,7 +243,9 @@ def search_projects_as_csv(search_dto: ProjectSearchDTO, user) -> str:
row["difficulty"] = ProjectDifficulty(row["difficulty"]).name
row["status"] = ProjectStatus(row["status"]).name
row["total_area"] = round(row["total_area"], 3)
row["total_contributors"] = Project.get_project_total_contributions(row["id"])
row["total_contributors"] = Project.get_project_total_contributions(
row["id"]
)

if is_user_admin:
partners_names = (
Expand Down Expand Up @@ -310,10 +337,10 @@ def search_projects(search_dto: ProjectSearchDTO, user) -> ProjectSearchResultsD
return dto

@staticmethod
def _filter_projects(search_dto: ProjectSearchDTO, user):
def _filter_projects(search_dto: ProjectSearchDTO, user, as_csv=False):
"""Filters all projects based on criteria provided by user"""

query = ProjectSearchService.create_search_query(user)
query = ProjectSearchService.create_search_query(user, as_csv)

query = query.join(ProjectInfo).filter(
ProjectInfo.locale.in_([search_dto.preferred_locale, "en"])
Expand Down

0 comments on commit e9b84d4

Please sign in to comment.