Skip to content

Commit

Permalink
Fix language query to fetch only based on codename (#98)
Browse files Browse the repository at this point in the history
* Fix black formatting

* Query language only by primary key + update values if not matching
  • Loading branch information
benoit74 authored Oct 5, 2023
1 parent 942be8e commit f41546d
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 20 deletions.
1 change: 0 additions & 1 deletion backend/src/backend/exporters/kiwix_public_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class KiwixPublicExporter(ExporterInterface):
"""Upload XML Kiwix Library to `UPLOAD_URI`, using `PRIVATE_KEY"""

async def export():

src_path = pathlib.Path(tempfile.NamedTemporaryFile(delete=False).name)

try:
Expand Down
1 change: 0 additions & 1 deletion backend/src/backend/formatters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class FormaterInterface:

slug: str
description: str

Expand Down
1 change: 0 additions & 1 deletion backend/src/backend/formatters/kiwixlibraryxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class KiwixLibraryXml(FormaterInterface):

description: str = "Kiwix Library XML"
slug = "kiwixlibraryxml"

Expand Down
16 changes: 15 additions & 1 deletion backend/src/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pydantic
import sqlalchemy

from backend.constants import BackendConf
from backend.constants import BackendConf, logger

METADATA_MAX_LEN = 2048
KIND_TEXT: str = "text"
Expand Down Expand Up @@ -75,6 +75,20 @@ class Meta(BaseMeta):
def __repr__(self):
return f"Language(code={self.code}, name={self.name})"

async def get_create_or_update(code: str, name: str, native: str):
language, created = await Language.objects.get_or_create(
code=code, _defaults={"name": name, "native": native}
)
if created is False and (language.name != name or language.native != native):
logger.warning(
f"Updating language values for {language.code} from {language.name}/"
f"{language.native} to {name}/{native}"
)
language.name = name
language.native = native
await language.update()
return language


class TagMixin:
"""ZIM Tag is a text slug stored along others in `Tags` metadata
Expand Down
2 changes: 1 addition & 1 deletion backend/src/backend/routes/books.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def create_book(book_payload: BookAddSchema):

for lang_code in book_payload.metadata["Language"].split(","):
native_name, english_name = find_language_names(lang_code)
language, _ = await Language.objects.get_or_create(
language = await Language.get_create_or_update(
code=lang_code, name=english_name, native=native_name
)
await book.languages.add(language)
Expand Down
2 changes: 0 additions & 2 deletions backend/src/backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def get_for(
with_tags: bool = False,
with_metadata: Union[List[str], bool] = False,
):

return getattr(
cls,
cls._name_for(
Expand All @@ -129,7 +128,6 @@ def get_for(


class TitleSendSchema(BaseModel):

ident: str
languages: List[str]
tags: List[str]
Expand Down
3 changes: 1 addition & 2 deletions backend/src/demo/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async def inner():

@transaction
async def load_fixture():

lang_codes = [
"lg",
"hi",
Expand Down Expand Up @@ -154,7 +153,7 @@ async def load_fixture():

iso_639_3_lang_code = get_language_details(lang_code)["iso-639-3"]

language, _ = await Language.objects.get_or_create(
language = await Language.get_create_or_update(
code=iso_639_3_lang_code, name=english_name, native=native_name
)
await book.languages.add(language)
Expand Down
4 changes: 0 additions & 4 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def book_dict(base64_png):

@pytest.fixture(scope="function")
async def title_without_book(book_dict):

title = await Title.objects.create(ident="wikipedia_ar_mathematics")

yield title
Expand All @@ -166,7 +165,6 @@ async def title_without_book(book_dict):

@pytest.fixture(scope="function")
async def book_dict_with_logs(book_dict):

book_dict["zimcheck"].update(
{
"logs": [
Expand Down Expand Up @@ -510,7 +508,6 @@ async def titles_with_metadata(language_fra, language_eng):
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def titles_with_metadata_books(client, book_dict):

ids = []
book_dict["metadata"]["Name"] = "wikipedia_en_all"
book_dict["metadata"]["Flavour"] = "maxi"
Expand All @@ -534,7 +531,6 @@ async def titles_with_metadata_books(client, book_dict):
@pytest.fixture(scope="function")
@pytest.mark.asyncio
async def title_with_three_books(client, book_dict):

title = await Title.objects.create(ident="wikipedia_ar_mathematics")
ids = []
for period in ["2021-04-08", "2021-05-08", "2021-06-08"]:
Expand Down
19 changes: 17 additions & 2 deletions backend/tests/test_books_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
KIND_TEXT,
Book,
BookMetadata,
Language,
Title,
TitleMetadata,
)
from backend.utils import get_ident_from_name


@pytest.mark.asyncio
async def test_add_book(client, book_dict, clear_book_dict):
async def test_add_book_create_language(client, book_dict, clear_book_dict):
response = await client.post("/v1/books/add", json=book_dict)
assert response.status_code == 201
assert response.headers.get("Content-Type") == "application/json"
Expand All @@ -39,8 +40,22 @@ async def test_add_book(client, book_dict, clear_book_dict):


@pytest.mark.asyncio
async def test_of_rollback(client, book_dict):
async def test_add_book_update_language(
client, book_dict, clear_book_dict, language_eng
):
# language_eng is already created in DB but its name and native properties are wrong
# since they have been updated in Babel
response = await client.post("/v1/books/add", json=book_dict)
assert response.status_code == 201
assert response.headers.get("Content-Type") == "application/json"

language = await Language.objects.get(code="eng")
assert language.native == "English (United States)"
assert language.name == "English (United States)"


@pytest.mark.asyncio
async def test_of_rollback(client, book_dict):
ident = get_ident_from_name(book_dict["metadata"]["Name"])
book_dict["metadata"]["Language"] = ""
response = await client.post("/v1/books/add", json=book_dict)
Expand Down
2 changes: 0 additions & 2 deletions backend/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@pytest.mark.asyncio
async def test_collection_interface():

with pytest.raises(NotImplementedError):
for _ in await CollectionInterface().get_book_ids():
...
Expand All @@ -19,7 +18,6 @@ async def test_collection_interface():

@pytest.mark.asyncio
async def test_kiwix_public(titles_with_metadata_books):

books_ids = KiwixPublicCollection().get_book_ids()
async for book_id in books_ids:
assert await Book.objects.filter(id=book_id).exists()
1 change: 0 additions & 1 deletion backend/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

@pytest.mark.asyncio
async def test_exporter_interface():

with pytest.raises(NotImplementedError):
for _ in await ExporterInterface().export():
...
Expand Down
1 change: 0 additions & 1 deletion backend/tests/test_remove_obsolete_books.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@pytest.mark.asyncio
async def test_remove_obsolete_books(client, book_dict, title_with_three_books):

title = (
await Title.objects.exclude_fields(["tags", "languages"])
.select_related("books")
Expand Down
1 change: 0 additions & 1 deletion backend/tests/test_zimcheck_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

@pytest.mark.asyncio
async def test_zim_check_dashboard(client, book_dict_with_logs, title_without_book):

response = await client.get("/v1/zimcheck")
assert response.status_code == 200
assert response.headers.get("Content-Type") == "application/json"
Expand Down

0 comments on commit f41546d

Please sign in to comment.