Skip to content

Commit

Permalink
Add corpus info to config (#134)
Browse files Browse the repository at this point in the history
* Add corpus info to config

* Push new lockfile to fix dependency error

* Test corpora in config keys

* Readd tests for checking corpora and taxonomy

* Bump to 2.6.0

* Fix tests
  • Loading branch information
katybaulch authored May 9, 2024
1 parent 6507a49 commit 7959e55
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/api/api_v1/routers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def get_config(request: Request) -> ConfigReadDTO:
user = request.state.user
_LOGGER.info(f"User {user.email} is getting config")
try:
config = config_service.get()
config = config_service.get(user.email)
except RepositoryError as e:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=e.message
Expand Down
16 changes: 15 additions & 1 deletion app/model/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ class DocumentConfig(BaseModel):
variants: Sequence[str]


class CorpusData(BaseModel):
"""Contains the Corpus and CorpusType info"""

corpus_import_id: str
title: str
description: str
corpus_type: str
corpus_type_description: str
taxonomy: TaxonomyData


class ConfigReadDTO(BaseModel):
"""Definition of the new Config which just includes taxonomy."""

geographies: Sequence[dict]
taxonomies: Mapping[str, TaxonomyData]
taxonomies: Mapping[
str, TaxonomyData
] # TODO: Will be Mapping[str, Sequence[CorpusData]] after PDCT-1052 finished
corpora: Sequence[CorpusData]
languages: Mapping[str, str]
document: DocumentConfig
event: EventConfig
59 changes: 56 additions & 3 deletions app/repository/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import Any, Optional
from dataclasses import asdict
from typing import Any, Optional, Sequence

from db_client.models.base import AnyModel
from db_client.models.dfce.family import (
Expand All @@ -9,11 +10,18 @@
Variant,
)
from db_client.models.dfce.geography import Geography
from db_client.models.dfce.taxonomy_entry import TaxonomyEntry
from db_client.models.document.physical_document import Language
from db_client.models.organisation import Corpus, CorpusType, Organisation
from sqlalchemy.orm import Session

from app.model.config import ConfigReadDTO, DocumentConfig, EventConfig, TaxonomyData
from app.model.config import (
ConfigReadDTO,
CorpusData,
DocumentConfig,
EventConfig,
TaxonomyData,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -67,7 +75,49 @@ def _get_organisation_taxonomy_by_name(
return metadata[0]


def get(db: Session) -> ConfigReadDTO:
def _to_corpus_data(row, event_types) -> CorpusData:
return CorpusData(
corpus_import_id=row.corpus_import_id,
title=row.title,
description=row.description,
corpus_type=row.corpus_type,
corpus_type_description=row.corpus_type_description,
taxonomy={
**row.taxonomy,
"event_types": asdict(event_types),
},
)


def get_corpora_for_org(db: Session, org_id: int) -> Sequence[CorpusData]:
corpora = (
db.query(
Corpus.import_id.label("corpus_import_id"),
Corpus.title.label("title"),
Corpus.description.label("description"),
Corpus.corpus_type_name.label("corpus_type"),
CorpusType.description.label("corpus_type_description"),
CorpusType.valid_metadata.label("taxonomy"),
)
.join(
Corpus,
Corpus.corpus_type_name == CorpusType.name,
)
.join(Organisation, Organisation.id == Corpus.organisation_id)
.filter(Organisation.id == org_id)
.all()
)

event_types = db.query(FamilyEventType).all()
entry = TaxonomyEntry(
allow_blanks=False,
allowed_values=[r.name for r in event_types],
allow_any=False,
)
return [_to_corpus_data(row, entry) for row in corpora]


def get(db: Session, org_id: int) -> ConfigReadDTO:
"""
Returns the configuration for the admin service.
Expand All @@ -84,6 +134,8 @@ def get(db: Session) -> ConfigReadDTO:
if tax is not None:
taxonomies[org.name] = tax

corpora = get_corpora_for_org(db, org_id)

languages = {lang.language_code: lang.name for lang in db.query(Language).all()}

# Now Document config
Expand Down Expand Up @@ -118,6 +170,7 @@ def get(db: Session) -> ConfigReadDTO:
return ConfigReadDTO(
geographies=geographies,
taxonomies=taxonomies,
corpora=corpora,
languages=languages,
document=doc_config,
event=event_config,
Expand Down
4 changes: 4 additions & 0 deletions app/repository/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

def get_id_from_name(db: Session, org_name: str) -> Optional[int]:
return db.query(Organisation.id).filter_by(name=org_name).scalar()


def get_name_from_id(db: Session, org_id: int) -> Optional[str]:
return db.query(Organisation.name).filter_by(id=org_id).scalar()
12 changes: 10 additions & 2 deletions app/service/config.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import logging

from pydantic import ConfigDict, validate_call
from sqlalchemy import exc

import app.clients.db.session as db_session
import app.repository.config as config_repo
from app.errors import RepositoryError
from app.model.config import ConfigReadDTO
from app.service import app_user

_LOGGER = logging.getLogger(__name__)


def get() -> ConfigReadDTO:
@validate_call(config=ConfigDict(arbitrary_types_allowed=True))
def get(user_email: str) -> ConfigReadDTO:
"""
Gets the config
:raises RepositoryError: If there is an issue getting the config
:return ConfigReadDTO: The config for the application
"""

try:
with db_session.get_db() as db:
return config_repo.get(db)
# Get the organisation from the user's email
org_id = app_user.get_organisation(db, user_email)

return config_repo.get(db, org_id)

except exc.SQLAlchemyError:
_LOGGER.exception("Error while getting config")
raise RepositoryError("Could not get the config")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "admin_backend"
version = "2.5.1"
version = "2.6.0"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down
Loading

0 comments on commit 7959e55

Please sign in to comment.