-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix delete bug, add beginnings of config endpoint# (#12)
- Loading branch information
1 parent
dfa3079
commit c8ad81e
Showing
14 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
|
||
from fastapi import APIRouter, HTTPException, Request, status | ||
import app.service.config as config_service | ||
from app.errors import RepositoryError | ||
|
||
config_router = r = APIRouter() | ||
|
||
_LOGGER = logging.getLogger(__file__) | ||
|
||
|
||
@r.get("/config") | ||
async def get_config(request: Request): | ||
user = request.state.user | ||
_LOGGER.info(f"User {user.email} is getting config") | ||
try: | ||
config = config_service.get() | ||
except RepositoryError as e: | ||
raise HTTPException( | ||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=e.message | ||
) | ||
|
||
if config is None: | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, detail="Config not found" | ||
) | ||
|
||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import Mapping, Sequence, Union | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
TaxonomyData = Mapping[str, Mapping[str, Union[bool, str, Sequence[str]]]] | ||
|
||
|
||
class ConfigReadDTO(BaseModel): | ||
"""Definition of the new Config which just includes taxonomy.""" | ||
|
||
geographies: Sequence[dict] | ||
taxonomies: Mapping[str, TaxonomyData] | ||
languages: Mapping[str, str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import logging | ||
from typing import Any | ||
from sqlalchemy.orm import Session | ||
from app.clients.db.models.app.users import Organisation | ||
from app.clients.db.models.document.physical_document import Language | ||
from app.clients.db.models.law_policy.geography import Geography | ||
from app.clients.db.models.law_policy.metadata import ( | ||
MetadataOrganisation, | ||
MetadataTaxonomy, | ||
) | ||
from app.clients.db.session import AnyModel | ||
from app.model.config import ConfigReadDTO, TaxonomyData | ||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def _tree_table_to_json( | ||
table: AnyModel, | ||
db: Session, | ||
) -> list[dict]: | ||
json_out = [] | ||
child_list_map: dict[int, Any] = {} | ||
|
||
for row in db.query(table).order_by(table.id).all(): | ||
row_object = {col.name: getattr(row, col.name) for col in row.__table__.columns} | ||
row_children: list[dict[str, Any]] = [] | ||
child_list_map[row_object["id"]] = row_children | ||
|
||
# No parent indicates a top level element | ||
node_row_object = {"node": row_object, "children": row_children} | ||
node_id = row_object["parent_id"] | ||
if node_id is None: | ||
json_out.append(node_row_object) | ||
else: | ||
append_list = child_list_map.get(node_id) | ||
if append_list is None: | ||
raise RuntimeError(f"Could not locate parent node with id {node_id}") | ||
append_list.append(node_row_object) | ||
|
||
return json_out | ||
|
||
|
||
def _get_organisation_taxonomy_by_name(db: Session, org_name: str) -> TaxonomyData: | ||
""" | ||
Returns the TaxonomyConfig for the named organisation | ||
:param Session db: connection to the database | ||
:return TaxonomyConfig: the TaxonomyConfig from the db | ||
""" | ||
return ( | ||
db.query(MetadataTaxonomy.valid_metadata) | ||
.join( | ||
MetadataOrganisation, | ||
MetadataOrganisation.taxonomy_id == MetadataTaxonomy.id, | ||
) | ||
.join(Organisation, Organisation.id == MetadataOrganisation.organisation_id) | ||
.filter_by(name=org_name) | ||
.one()[0] | ||
) | ||
|
||
|
||
def get(db: Session) -> ConfigReadDTO: | ||
""" | ||
Returns the configuration for the admin service. | ||
:param Session db: connection to the database | ||
:return ConfigReadDTO: The config data | ||
""" | ||
|
||
# TODO: Return the event types too | ||
geographies = _tree_table_to_json(table=Geography, db=db) | ||
taxonomies = { | ||
org.name: _get_organisation_taxonomy_by_name(db=db, org_name=org.name) | ||
for org in db.query(Organisation).all() | ||
} | ||
languages = {lang.language_code: lang.name for lang in db.query(Language).all()} | ||
return ConfigReadDTO( | ||
geographies=geographies, taxonomies=taxonomies, languages=languages | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import logging | ||
from app.errors import RepositoryError | ||
import app.repository.config as config_repo | ||
|
||
from sqlalchemy import exc | ||
import app.clients.db.session as db_session | ||
|
||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def get(): | ||
try: | ||
with db_session.get_db() as db: | ||
return config_repo.get(db) | ||
except exc.SQLAlchemyError: | ||
_LOGGER.exception("Error while getting config") | ||
raise RepositoryError("Could not get the config") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from fastapi.testclient import TestClient | ||
from fastapi import status | ||
from sqlalchemy.orm import Session | ||
from integration_tests.setup_db import setup_db | ||
|
||
|
||
def test_get_config(client: TestClient, test_db: Session, user_header_token): | ||
setup_db(test_db) | ||
|
||
response = client.get( | ||
"/api/v1/config", | ||
headers=user_header_token, | ||
) | ||
assert response.status_code == status.HTTP_200_OK | ||
data = response.json() | ||
keys = data.keys() | ||
assert "geographies" in keys | ||
assert "taxonomies" in keys | ||
assert "languages" in keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
def test_get_config(client: TestClient, user_header_token): | ||
# TODO: Finish the config tests by adding mock for repo | ||
pass |