Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pdct 981/admin api if language idempotent language is removed from document #100

Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ run:

start: build_dev run

restart:
docker stop navigator-admin-backend && docker rm navigator-admin-backend && make start && docker logs -f navigator-admin-backend

start_local: build
# - docker stop navigator-admin-backend
docker run -p 8888:8888 \
--name navigator-admin-backend \
--network=navigator-backend_default \
-e ADMIN_POSTGRES_HOST=backend_db \
-e SECRET_KEY="secret_test_key" \
-d navigator-admin-backend

restart: build
docker stop navigator-admin-backend && docker rm navigator-admin-backend && make start_local && docker logs -f navigator-admin-backend

show_logs:
- docker compose logs -f
119 changes: 72 additions & 47 deletions app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,9 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
return False

# User Language changed?
existing_language = (
db.query(PhysicalDocumentLanguage)
.filter(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
.filter(PhysicalDocumentLanguage.source == LanguageSource.USER)
.one_or_none()
)
new_language = _get_new_language(db, new_values, existing_language)
existing_language = _get_existing_language(db, original_fd)
new_language = _get_requested_language(db, new_values)
lang_idempotent = _is_language_idempotent(existing_language, new_language)
katybaulch marked this conversation as resolved.
Show resolved Hide resolved

update_slug = original_pd.title != new_values["title"]

Expand All @@ -245,9 +239,11 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
.where(PhysicalDocument.id == original_pd.id)
.values(
title=new_values["title"],
source_url=str(new_values["source_url"])
if new_values["source_url"] is not None
else None,
source_url=(
str(new_values["source_url"])
if new_values["source_url"] is not None
else None
),
),
db_update(FamilyDocument)
.where(FamilyDocument.import_id == original_fd.import_id)
Expand All @@ -258,33 +254,37 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
),
]

if new_language is not None:
if existing_language is not None:
command = (
db_update(PhysicalDocumentLanguage)
.where(
and_(
PhysicalDocumentLanguage.document_id
== original_fd.physical_document_id,
PhysicalDocumentLanguage.source == LanguageSource.USER,
# Update logic to only perform update if not idempotent.
if not lang_idempotent:
if new_language is not None:
if existing_language is not None:
command = (
db_update(PhysicalDocumentLanguage)
.where(
and_(
PhysicalDocumentLanguage.document_id
== original_fd.physical_document_id,
PhysicalDocumentLanguage.source == LanguageSource.USER,
)
)
.values(language_id=new_language.id)
)
else:
command = db_insert(PhysicalDocumentLanguage).values(
document_id=original_fd.physical_document_id,
language_id=new_language.id,
source=LanguageSource.USER,
)
.values(language_id=new_language.id)
)
else:
command = db_insert(PhysicalDocumentLanguage).values(
document_id=original_fd.physical_document_id,
language_id=new_language.id,
source=LanguageSource.USER,
)
commands.append(command)
else:
if existing_language is not None:
command = db_delete(PhysicalDocumentLanguage).where(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
commands.append(command)

else:
if existing_language is not None:
command = db_delete(PhysicalDocumentLanguage).where(
PhysicalDocumentLanguage.document_id
== original_fd.physical_document_id
)
commands.append(command)

for c in commands:
result = db.execute(c)

Expand All @@ -303,24 +303,49 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
return True


def _get_new_language(
db: Session, new_values: dict, pdl: PhysicalDocumentLanguage
def _get_existing_language(
db: Session, original_fd: FamilyDocument
) -> Optional[Language]:
existing_language = (
db.query(PhysicalDocumentLanguage)
.filter(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
.filter(PhysicalDocumentLanguage.source == LanguageSource.USER)
.one_or_none()
)
return existing_language


def _get_requested_language(db: Session, new_values: dict) -> Optional[Language]:
requested_language = new_values["user_language_name"]
if requested_language is None:
return None
else:
new_language = (
db.query(Language)
.filter(Language.name == new_values["user_language_name"])
.one()
)
update_language = (
pdl.language_id != new_language.id if pdl is not None else True

new_language = (
db.query(Language)
.filter(Language.name == new_values["user_language_name"])
.one()
)
return new_language


def _is_language_idempotent(
katybaulch marked this conversation as resolved.
Show resolved Hide resolved
existing_language: Optional[Language],
requested_language: Optional[Language],
) -> bool:
if (existing_language == requested_language) is None:
return True

if requested_language is not None:
is_idempotent = bool(
existing_language.language_id == requested_language.id
if existing_language is not None
else False
)
return is_idempotent

if update_language:
return new_language
return False


def create(db: Session, document: DocumentCreateDTO) -> str:
Expand Down
Loading
Loading