Skip to content

Commit

Permalink
fix: set first table id as published if available (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Sep 21, 2023
1 parent 3d1a869 commit fe08597
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions basedosdados_api/api/v1/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down
2 changes: 1 addition & 1 deletion basedosdados_api/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down

0 comments on commit fe08597

Please sign in to comment.