Skip to content

Commit

Permalink
Merge pull request #130 from ecmwf-projects/COPDS-2055-licences-blocks
Browse files Browse the repository at this point in the history
fixing id check during layout management + style
  • Loading branch information
alex75 authored Sep 13, 2024
2 parents 6662e55 + 74bb454 commit ac6d209
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 77 deletions.
67 changes: 37 additions & 30 deletions cads_catalogue/layout_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ def transform_image_blocks(
return new_data


def build_licence_blocks(
def build_required_licence_blocks(
licence: database.Licence, doc_storage_url: str
) -> List[dict[str, str]]:
"""
Build a list of new licence blocks to be inserted inside the layout data.
Build a list of blocks related to required licences to be inserted inside the layout data.
Parameters
----------
Expand Down Expand Up @@ -189,13 +189,13 @@ def build_licence_blocks(
return new_blocks


def manage_licence_section(
def manage_required_licence_section(
all_licences: Sequence[database.Licence],
section: dict[str, Any],
doc_storage_url: str,
):
"""
Look for licence blocks and modify accordingly with urls of object storage files.
Look for required licence blocks and modify accordingly with urls of object storage files.
Parameters
----------
Expand All @@ -215,25 +215,25 @@ def manage_licence_section(
][0]
except IndexError:
raise ValueError(f"not found licence {licence_uid}")
new_blocks = build_licence_blocks(licence, doc_storage_url)
new_blocks = build_required_licence_blocks(licence, doc_storage_url)
del blocks[i + 2 * replacements]
blocks.insert(i + 2 * replacements, new_blocks[0])
blocks.insert(i + 1 + 2 * replacements, new_blocks[1])
blocks.insert(i + 2 + 2 * replacements, new_blocks[2])
replacements += 1
elif block.get("type") in ("section", "accordion"):
blocks[i + 2 * replacements] = manage_licence_section(
blocks[i + 2 * replacements] = manage_required_licence_section(
all_licences, block, doc_storage_url
)
return new_section


def transform_licences_blocks(
def transform_licence_required_blocks(
session: sa.orm.session.Session,
layout_data: dict[str, Any],
storage_settings: config.ObjectStorageSettings,
):
"""Transform layout.json data processing uploads of referenced licences.
"""Transform layout.json replacing blocks related to required licences.
Parameters
----------
Expand All @@ -254,23 +254,25 @@ def transform_licences_blocks(
body_main = body.get("main", {})
sections = body_main.get("sections", [])
for i, section in enumerate(copy.deepcopy(sections)):
sections[i] = manage_licence_section(all_licences, section, doc_storage_url)
sections[i] = manage_required_licence_section(
all_licences, section, doc_storage_url
)
# search all the images inside body/aside:
aside_section = body.get("aside", {})
if aside_section:
new_data["body"]["aside"] = manage_licence_section(
new_data["body"]["aside"] = manage_required_licence_section(
all_licences,
aside_section,
doc_storage_url,
)
return new_data


def build_licence_block2(
def build_licence_acceptance_block(
licence_objs: List[database.Licence], doc_storage_url: str
) -> dict[str, Any]:
"""
Build a list of new licence blocks to be inserted inside the layout data.
Build a new licence acceptance block to be inserted inside the layout data.
Parameters
----------
Expand All @@ -297,20 +299,18 @@ def build_licence_block2(
licence_blocks.append(licence_block)
new_block = {
"type": "licences_acceptance",
"id": "licences_section",
"title": "Licence",
"details": {"licences": licence_blocks},
}
return new_block


def manage_licence_section2(
def manage_licence_acceptance_section(
all_licences: Sequence[database.Licence],
section: dict[str, Any],
doc_storage_url: str,
):
"""
Look for licence blocks and modify accordingly with urls of object storage files.
Look for licence acceptance blocks and modify accordingly with urls of object storage files.
Parameters
----------
Expand All @@ -321,11 +321,7 @@ def manage_licence_section2(
new_section = copy.deepcopy(section)
blocks = new_section.get("blocks", [])
for i, block in enumerate(copy.deepcopy(blocks)):
if (
block.get("type") == "licences_acceptance"
and block.get("id") == "licences_section"
and "details" in block
):
if block.get("type") == "licences_acceptance" and "details" in block:
licence_objs = []
for licence_block in block["details"]["licences"]:
licence_uid = licence_block["licence-id"]
Expand All @@ -338,21 +334,26 @@ def manage_licence_section2(
except IndexError:
raise ValueError(f"not found licence {licence_uid}")
licence_objs.append(licence_obj)
new_block = build_licence_block2(licence_objs, doc_storage_url)
new_block["title"] = blocks[i].get("title", new_block["title"])
new_block = build_licence_acceptance_block(licence_objs, doc_storage_url)
for attr in ("id", "title"):
attr_value = block.get(attr)
if attr_value:
new_block[attr] = attr_value
del blocks[i]
blocks.insert(i, new_block)
elif block.get("type") in ("section", "accordion"):
blocks[i] = manage_licence_section2(all_licences, block, doc_storage_url)
blocks[i] = manage_licence_acceptance_section(
all_licences, block, doc_storage_url
)
return new_section


def transform_licences_blocks2(
def transform_licence_acceptance_blocks(
session: sa.orm.session.Session,
layout_data: dict[str, Any],
storage_settings: config.ObjectStorageSettings,
):
"""Transform layout.json data processing uploads of referenced licences (block_id = "licences_section").
"""Transform layout.json replacing blocks related to licence acceptance.
Parameters
----------
Expand All @@ -373,11 +374,13 @@ def transform_licences_blocks2(
body_main = body.get("main", {})
sections = body_main.get("sections", [])
for i, section in enumerate(copy.deepcopy(sections)):
sections[i] = manage_licence_section2(all_licences, section, doc_storage_url)
sections[i] = manage_licence_acceptance_section(
all_licences, section, doc_storage_url
)
# search all the images inside body/aside:
aside_section = body.get("aside", {})
if aside_section:
new_data["body"]["aside"] = manage_licence_section2(
new_data["body"]["aside"] = manage_licence_acceptance_section(
all_licences,
aside_section,
doc_storage_url,
Expand Down Expand Up @@ -536,8 +539,12 @@ def transform_layout(
layout_data = transform_image_blocks(
layout_data, resource_folder_path, resource, storage_settings
)
layout_data = transform_licences_blocks(session, layout_data, storage_settings)
layout_data = transform_licences_blocks2(session, layout_data, storage_settings)
layout_data = transform_licence_required_blocks(
session, layout_data, storage_settings
)
layout_data = transform_licence_acceptance_blocks(
session, layout_data, storage_settings
)
logger.debug(f"output layout_data: {layout_data}")
if resource["qa_flag"]:
resource["qa_flag"] = has_section_id(layout_data, "quality_assurance_tab")
Expand Down
20 changes: 2 additions & 18 deletions tests/data/layout1.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
},
{
"type": "licences_acceptance",
"id": "licences_section",
"title": "a title",
"id": "arandomid",
"title": "arandom title",
"details": {
"licences": [
{
Expand All @@ -152,22 +152,6 @@
"title": "Download 2",
"id": "d_download",
"blocks": [
{
"title": "A title to not override",
"id": "licence",
"type": "licences_acceptance",
"details": {
"licences": [
{
"id": "licence-to-use-copernicus-products",
"revision": 12,
"label": "Licence to use Copernicus Products",
"contents_url": "https://s3.cds.ecmwf.int/swift/v1/AUTH_3e237111c3a144df8e0e0980577062b4/bopen-cds2-dev-catalogue/licences/licence-to-use-copernicus-products/licence-to-use-copernicus-productsv12_c742a3ce47ae53486a6bc9012f0496e655cd94f19fb2d01b4f6e32af69276ed1.md",
"attachment_url": "https://s3.cds.ecmwf.int/swift/v1/AUTH_3e237111c3a144df8e0e0980577062b4/bopen-cds2-dev-catalogue/licences/licence-to-use-copernicus-products/licence-to-use-copernicus-products_b4b9451f54cffa16ecef5c912c9cebd6979925a956e3fa677976e0cf198c2c18.pdf"
}
]
}
},
{
"id": "download1",
"type": "markdown",
Expand Down
20 changes: 2 additions & 18 deletions tests/data/layout2.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@
},
{
"type": "licences_acceptance",
"id": "licences_section",
"title": "a title",
"id": "arandomid",
"title": "arandom title",
"details": {
"licences": [
{
Expand Down Expand Up @@ -160,22 +160,6 @@
"title": "Download 2",
"id": "d_download",
"blocks": [
{
"title": "A title to not override",
"id": "licence",
"type": "licences_acceptance",
"details": {
"licences": [
{
"id": "licence-to-use-copernicus-products",
"revision": 12,
"label": "Licence to use Copernicus Products",
"contents_url": "https://s3.cds.ecmwf.int/swift/v1/AUTH_3e237111c3a144df8e0e0980577062b4/bopen-cds2-dev-catalogue/licences/licence-to-use-copernicus-products/licence-to-use-copernicus-productsv12_c742a3ce47ae53486a6bc9012f0496e655cd94f19fb2d01b4f6e32af69276ed1.md",
"attachment_url": "https://s3.cds.ecmwf.int/swift/v1/AUTH_3e237111c3a144df8e0e0980577062b4/bopen-cds2-dev-catalogue/licences/licence-to-use-copernicus-products/licence-to-use-copernicus-products_b4b9451f54cffa16ecef5c912c9cebd6979925a956e3fa677976e0cf198c2c18.pdf"
}
]
}
},
{
"id": "download1",
"type": "markdown",
Expand Down
22 changes: 11 additions & 11 deletions tests/test_30_layout_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create_layout_for_test(path, sections=[], aside={}):
return base_data


def test_transform_licences_blocks(tmpdir, session_obj: sa.orm.sessionmaker):
def test_transform_licence_required_blocks(tmpdir, session_obj: sa.orm.sessionmaker):
my_settings_dict = {
"object_storage_url": "object/storage/url",
"storage_admin": "admin1",
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_transform_licences_blocks(tmpdir, session_obj: sa.orm.sessionmaker):

layout_data = create_layout_for_test(layout_path, sections=sections, aside=aside)

new_layout_data = layout_manager.transform_licences_blocks(
new_layout_data = layout_manager.transform_licence_required_blocks(
session, layout_data, storage_settings
)
assert new_layout_data == {
Expand All @@ -78,25 +78,25 @@ def test_transform_licences_blocks(tmpdir, session_obj: sa.orm.sessionmaker):
"sections": [
{
"id": "overview",
"blocks": layout_manager.build_licence_blocks(
"blocks": layout_manager.build_required_licence_blocks(
all_licences[0], storage_settings.document_storage_url
)
+ [block2]
+ layout_manager.build_licence_blocks(
+ layout_manager.build_required_licence_blocks(
all_licences[1], storage_settings.document_storage_url
),
},
{
"id": "overview2",
"blocks": [block2]
+ layout_manager.build_licence_blocks(
+ layout_manager.build_required_licence_blocks(
all_licences[1], storage_settings.document_storage_url
),
},
]
},
"aside": {
"blocks": layout_manager.build_licence_blocks(
"blocks": layout_manager.build_required_licence_blocks(
all_licences[0], storage_settings.document_storage_url
)
+ [block2]
Expand All @@ -123,15 +123,15 @@ def test_transform_licences_blocks(tmpdir, session_obj: sa.orm.sessionmaker):
}
sections = [section1, section4, section2]
aside = {"blocks": [block1, block2, section4, block3]}
new_block1 = layout_manager.build_licence_blocks(
new_block1 = layout_manager.build_required_licence_blocks(
all_licences[0], storage_settings.document_storage_url
)
new_block3 = layout_manager.build_licence_blocks(
new_block3 = layout_manager.build_required_licence_blocks(
all_licences[1], storage_settings.document_storage_url
)
layout_data = create_layout_for_test(layout_path, sections=sections, aside=aside)

new_layout_data = layout_manager.transform_licences_blocks(
new_layout_data = layout_manager.transform_licence_required_blocks(
session, layout_data, storage_settings
)
expected = {
Expand Down Expand Up @@ -872,7 +872,7 @@ def test_has_section_id(tmpdir):
assert layout_manager.has_section_id(layout_data, "overview4") is False


def test_transform_licences_blocks2(session_obj: sa.orm.sessionmaker):
def test_transform_licence_acceptance_blocks(session_obj: sa.orm.sessionmaker):
my_settings_dict = {
"object_storage_url": "https://object/storage/url/",
"storage_admin": "admin1",
Expand Down Expand Up @@ -906,7 +906,7 @@ def test_transform_licences_blocks2(session_obj: sa.orm.sessionmaker):
input_layout_path = os.path.join(TESTDATA_PATH, "layout1.json")
with open(input_layout_path) as fp:
input_layout_data = json.load(fp)
out_layout_data = layout_manager.transform_licences_blocks2(
out_layout_data = layout_manager.transform_licence_acceptance_blocks(
session, input_layout_data, storage_settings
)
expected_layout_path = os.path.join(TESTDATA_PATH, "layout2.json")
Expand Down

0 comments on commit ac6d209

Please sign in to comment.