Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

* `staff()` gains `status="lent"` (API term `"efetivo-cedido"`): the Assembly's
own permanent staff lent to other bodies. The API publishes them with
`vinculo` `"Efetivo"`, so this server-side filter is the only way to single
them out. `positions()` refuses it, because `/cargos` ignores the value and
returns every status.

## 0.1.1

* `set_cache_ttl(0)` now always re-fetches. Stale entries were detected by
Expand Down
6 changes: 4 additions & 2 deletions docs/portugues.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ alepe.servidores(status="efetivo")
alepe.staff(status="permanent")
```

Os valores originais da API são `efetivo`, `comissionado` e `a-disposicao`; os
equivalentes em inglês são `permanent`, `commissioned` e `seconded`.
Os valores originais da API são `efetivo`, `comissionado`, `a-disposicao` e
`efetivo-cedido`; os equivalentes em inglês são `permanent`, `commissioned`,
`seconded` e `lent`. `efetivo-cedido` (efetivos da Alepe cedidos a outros
órgãos) só existe em `servidores()`: a API o ignora em `/cargos`.

## Nomes de coluna

Expand Down
34 changes: 27 additions & 7 deletions src/alepe/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,26 @@
"a-disposicao": "a-disposicao",
}

# Only /servidores honours efetivo-cedido; /cargos ignores it and returns every
# status, as it does for any unknown value, so positions() must refuse it.
_STAFF_STATUS = {**_STATUS, "lent": "efetivo-cedido", "efetivo-cedido": "efetivo-cedido"}

def map_status(status: str | None) -> str | None:

def map_status(status: str | None, lent: bool = False) -> str | None:
"""Translate an employment-status filter to the API's ``vinculo`` value.

Accepts the English vocabulary and the original API terms alike, so
``"permanent"`` and ``"efetivo"`` are the same query.
``"permanent"`` and ``"efetivo"`` are the same query. ``lent`` also allows
``"lent"`` / ``"efetivo-cedido"``, which only the staff endpoint supports.
"""
if status is None:
return None
table = _STAFF_STATUS if lent else _STATUS
try:
return _STATUS[status]
return table[status]
except KeyError:
raise ValueError(
f"Unknown status {status!r}. Use one of: {', '.join(sorted(_STATUS))}."
f"Unknown status {status!r}. Use one of: {', '.join(sorted(table))}."
) from None


Expand All @@ -64,13 +70,27 @@ def representatives(refresh: bool = False) -> pd.DataFrame:


def staff(status: str | None = None, refresh: bool = False) -> pd.DataFrame:
"""The Assembly's staff roster, optionally filtered by employment status."""
records = _client.fetch_json("servidores", {"vinculo": map_status(status)}, refresh=refresh)
"""The Assembly's staff roster, optionally filtered by employment status.

``status`` is ``"permanent"``, ``"commissioned"``, ``"seconded"`` (staff from
other bodies placed at the Assembly's disposal) or ``"lent"`` (the Assembly's
own permanent staff lent to other bodies, a subset of ``"permanent"``); the
API terms ``"efetivo"``, ``"comissionado"``, ``"a-disposicao"`` and
``"efetivo-cedido"`` work too. Lent staff are published with ``vinculo``
``"Efetivo"``, so this filter is the only way to tell them apart.
"""
records = _client.fetch_json(
"servidores", {"vinculo": map_status(status, lent=True)}, refresh=refresh
)
return to_frame(records, STAFF_SCHEMA)


def positions(status: str | None = None, refresh: bool = False) -> pd.DataFrame:
"""Staff counts per position and level."""
"""Staff counts per position and level.

``status`` takes the same values as :func:`staff` except ``"lent"``, which
the API ignores for this endpoint.
"""
records = _client.fetch_json("cargos", {"vinculo": map_status(status)}, refresh=refresh)
return to_frame(records, POSITIONS_SCHEMA)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ def test_status_filter_accepts_both_vocabularies(serve_json):
alepe.staff(status="nope")


def test_lent_status_reaches_staff_but_not_positions(monkeypatch, fixture_json):
sent = {}

def fake_fetch(endpoint, params=None, **kwargs):
sent[endpoint] = (params or {}).get("vinculo")
return fixture_json("servidores.json")

monkeypatch.setattr(_client, "fetch_json", fake_fetch)
alepe.staff(status="lent")
assert sent["servidores"] == "efetivo-cedido"
alepe.servidores(status="efetivo-cedido")
assert sent["servidores"] == "efetivo-cedido"

with pytest.raises(ValueError):
alepe.positions(status="lent")
assert "cargos" not in sent


def test_portuguese_aliases_reach_the_same_functions(serve_json, serve_text):
serve_json("servidores.json")
assert alepe.servidores().equals(alepe.staff())
Expand Down
Loading