diff --git a/basedosdados_api/api/v1/search_indexes.py b/basedosdados_api/api/v1/search_indexes.py index 82952a4f..fc5f9051 100644 --- a/basedosdados_api/api/v1/search_indexes.py +++ b/basedosdados_api/api/v1/search_indexes.py @@ -64,20 +64,18 @@ def index_queryset(self, using=None): def prepare(self, obj): data = super().prepare(obj) - # table ids - table_ids = data.get("table_ids", []) - data["first_table_id"] = table_ids[0] if table_ids else "" - if table_ids: + if table_ids := data.get("table_ids", []): + published_tables = obj.tables.exclude( + status__slug__in=["under_review"] + ) # fmt: skip closed_tables = obj.tables.filter(is_closed=True).exclude( status__slug__in=["under_review"] ) - published_tables = obj.tables.exclude( - status__slug__in=[ - "under_review", - ] - ) - data["n_closed_tables"] = closed_tables.count() data["n_tables"] = published_tables.count() + data["n_closed_tables"] = closed_tables.count() + data["first_table_id"] = table_ids[0] + if published_tables.first(): + data["first_table_id"] = published_tables.first().id # organization organization_id = data.get("organization_id", "") diff --git a/basedosdados_api/api/v1/views.py b/basedosdados_api/api/v1/views.py index 79fcdeb7..dc98a3d1 100644 --- a/basedosdados_api/api/v1/views.py +++ b/basedosdados_api/api/v1/views.py @@ -324,11 +324,11 @@ def get(self, request, *args, **kwargs): cleaned_results["n_closed_tables"] = r.get("n_closed_tables") or 0 if r.get("tables"): if len(tables := r.get("tables")) > 0: - cleaned_results["first_table_id"] = tables[0]["id"] cleaned_results["n_tables"] = r.get("n_tables") cleaned_results["n_open_tables"] = ( cleaned_results["n_tables"] - cleaned_results["n_closed_tables"] ) + cleaned_results["first_table_id"] = r.get("first_table_id") cleaned_results["first_closed_table_id"] = None for table in tables: if table["is_closed"]: