Skip to content

Commit

Permalink
Merge pull request #6662 from hotosm/fastapi-refactor
Browse files Browse the repository at this point in the history
Private projects filters fixed and author names in list view and csv …
  • Loading branch information
prabinoid authored Dec 20, 2024
2 parents 556bb3f + f0f4002 commit 1f4dc8d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions backend/services/project_search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ async def create_search_query(db, user=None, as_csv: bool = False):
p.tasks_mapped,
p.tasks_validated,
p.tasks_bad_imagery,
u.name AS author_name,
u.username AS author_username,
o.name AS organisation_name,
ROUND(COALESCE(
(p.tasks_mapped + p.tasks_validated) * 100.0 / NULLIF(p.total_tasks - p.tasks_bad_imagery, 0), 0
Expand All @@ -88,6 +90,7 @@ async def create_search_query(db, user=None, as_csv: bool = False):
p.created AS creation_date
FROM projects p
LEFT JOIN organisations o ON o.id = p.organisation_id
LEFT JOIN users u ON u.id = p.author_id
WHERE p.geometry IS NOT NULL
"""

Expand All @@ -109,10 +112,13 @@ async def create_search_query(db, user=None, as_csv: bool = False):
p.due_date,
p.country,
p.mapping_types,
u.name AS author_name,
u.username AS author_username,
o.name AS organisation_name,
o.logo AS organisation_logo
FROM projects p
LEFT JOIN organisations o ON o.id = p.organisation_id
LEFT JOIN users u ON u.id = p.author_id
WHERE p.geometry IS NOT NULL
"""

Expand All @@ -135,7 +141,6 @@ async def create_search_query(db, user=None, as_csv: bool = False):
team_projects = await db.fetch_all(
team_projects_query, {"user_id": user.id}
)

# Fetch project_ids for user's organisations
org_projects_query = """
SELECT p.id
Expand All @@ -155,12 +160,13 @@ async def create_search_query(db, user=None, as_csv: bool = False):
+ [row["id"] for row in org_projects]
)
)

if project_ids:
filters.append("p.private = :private OR p.id = ANY(:project_ids)")
params["private"] = False
params["project_ids"] = list(project_ids)

else:
filters.append("p.private = :private")
params["private"] = False
if filters:
query += " AND (" + " AND ".join(filters) + ")"

Expand Down Expand Up @@ -201,6 +207,7 @@ async def create_result_dto(
list_dto.active_mappers = await Project.get_active_mappers(project.id, db)
list_dto.total_contributors = total_contributors
list_dto.country = project.country
list_dto.author = project.author_name
list_dto.organisation_name = project.organisation_name
list_dto.organisation_logo = project.organisation_logo
list_dto.campaigns = await Project.get_project_campaigns(project.id, db)
Expand Down Expand Up @@ -249,6 +256,8 @@ async def search_projects_as_csv(
row["total_contributors"] = await Project.get_project_total_contributions(
row["id"], db
)
row["author"] = row["author_name"] or row["author_username"]

project_name_query = """
SELECT COALESCE(
(SELECT pi.name FROM project_info pi WHERE pi.project_id = :project_id AND pi.locale = :locale LIMIT 1),
Expand Down Expand Up @@ -286,6 +295,8 @@ async def search_projects_as_csv(
"tasks_mapped",
"tasks_validated",
"total_tasks",
"author_name",
"author_username",
]

colummns_to_rename = {
Expand All @@ -298,6 +309,7 @@ async def search_projects_as_csv(
"total_area": "totalArea",
"total_contributors": "totalContributors",
"partner_names": "partnerNames",
"author_name": "author",
"project_name": "name",
}

Expand Down

0 comments on commit 1f4dc8d

Please sign in to comment.