Skip to content

Commit

Permalink
refactor: remove last traces of 'update_uncontained'
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad committed Oct 3, 2023
1 parent b1808fe commit 25eac2f
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 77 deletions.
6 changes: 0 additions & 6 deletions src/features/document/document_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def update(
id_address: str,
data: Json = Form(...),
files: Optional[List[UploadFile]] = File(None),
update_uncontained: Optional[bool] = False,
user: User = Depends(auth_w_jwt_or_pat),
):
"""Update an Existing Document in the Database.
Expand All @@ -71,7 +70,6 @@ def update(
- The PROTOCOL is optional, and the default is dmss.
- document (dict): The document to replace the previous version.
- files: Optional list of files to be stored as part of this document.
- update_uncontained (bool): Optional flag specifying whether to also update uncontained attributes in the document.
- user (User): The authenticated user accessing the endpoint, automatically generated from provided bearer token or Access-Key.
Returns:
Expand All @@ -89,7 +87,6 @@ def update(
address=Address.from_absolute(id_address),
data=data,
files=files,
update_uncontained=update_uncontained,
document_service=document_service,
)

Expand All @@ -100,7 +97,6 @@ def add_document(
address: str,
document: Json = Form(...),
files: Optional[List[UploadFile]] = File(None),
update_uncontained: Optional[bool] = False,
user: User = Depends(auth_w_jwt_or_pat),
):
"""Add a document to a package or a data source using an address.
Expand All @@ -118,7 +114,6 @@ def add_document(
- The PROTOCOL is optional, and the default is dmss.
- document (dict): The document that is to be stored.
- files: Optional list of files to be stored as part of this document.
- update_uncontained (bool): Optional flag specifying whether to also update uncontained attributes in the document.
- user (User): The authenticated user accessing the endpoint, automatically generated from provided bearer token or Access-Key.
Returns:
Expand All @@ -137,7 +132,6 @@ def add_document(
address=Address.from_absolute(address),
document=document,
files=files,
update_uncontained=update_uncontained,
document_service=document_service,
)

Expand Down
28 changes: 8 additions & 20 deletions src/features/document/use_cases/add_document_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
from services.document_service import DocumentService


def _add_document_to_data_source(
data_source_id: str, document: dict, update_uncontained: Optional[bool], document_service: DocumentService
) -> dict:
def _add_document_to_data_source(data_source_id: str, document: dict, document_service: DocumentService) -> dict:
"""Add the document to an existing data source.
Args:
data_source_id: The data source ID
document: The entity to be added
update_uncontained: Whether to update uncontained children (deprecated)
document_service: The document service
Returns:
Expand All @@ -53,7 +50,7 @@ def _add_document_to_data_source(

new_node.set_uid(new_node.generate_id())

document_service.save(new_node, data_source_id, update_uncontained=update_uncontained)
document_service.save(new_node, data_source_id)

return {"uid": new_node.node_id}

Expand All @@ -62,7 +59,6 @@ def _add_document_to_entity_or_list(
address: Address,
document: dict,
files: dict[str, BinaryIO] | None,
update_uncontained: Optional[bool],
document_service: DocumentService,
) -> dict:
"""Add the document to an existing entity.
Expand All @@ -71,7 +67,6 @@ def _add_document_to_entity_or_list(
address: Reference to an existing entity or to an attribute (complex or list attribute) inside an entity.
document: The entity to be added
files: Dict with names and files of the files contained in the document
update_uncontained: Whether to update uncontained children (deprecated)
Returns:
A dict that contains the ID of the added document.
Expand Down Expand Up @@ -131,16 +126,12 @@ def _add_document_to_entity_or_list(
)
parent_node.add_child(list_node)
list_node.add_child(new_node)
document_service.save(
parent_node, address.data_source, update_uncontained=update_uncontained, initial=True
)
document_service.save(parent_node, address.data_source, initial=True)
return {"uid": f"{list_node.node_id}"}
else:
parent_node.add_child(new_node)

document_service.save(
parent_node, address.data_source, update_uncontained=update_uncontained, initial=True
)
document_service.save(parent_node, address.data_source, initial=True)
return {"uid": f"{new_node.node_id}"}

if target.type != SIMOS.PACKAGE.value and target.type != "object":
Expand Down Expand Up @@ -180,13 +171,13 @@ def _add_document_to_entity_or_list(
if new_node.should_have_id():
new_node.set_uid(new_node.generate_id())
target.add_child(new_node)
document_service.save(target.find_parent(), address.data_source, update_uncontained=False)
document_service.save(target.find_parent(), address.data_source)
else:
new_node.parent = target.parent
target.parent.replace(new_node.node_id, new_node)
document_service.save(target.find_parent(), address.data_source, update_uncontained=False)
document_service.save(target.find_parent(), address.data_source)

document_service.save(new_node, address.data_source, update_uncontained=update_uncontained)
document_service.save(new_node, address.data_source)

return {"uid": new_node.node_id}

Expand All @@ -196,7 +187,6 @@ def add_document_use_case(
address: Address,
document_service: DocumentService,
files: Optional[List[UploadFile]] = None,
update_uncontained: Optional[bool] = False,
) -> dict:
"""Add document to a data source or existing entity. Can also be used to add (complex) items to a list.
Expand All @@ -205,20 +195,18 @@ def add_document_use_case(
address: Reference to a package, attribute inside an entity (either a list or a complex attribute) or a data source
document_service: The document service
files: Dict with names and files of the files contained in the document
update_uncontained: Whether to update uncontained children (deprecated)
Returns:
A dict that contains the ID of the added document.
"""
validate_entity_against_self(document, document_service.get_blueprint)

if not address.path:
return _add_document_to_data_source(address.data_source, document, update_uncontained, document_service)
return _add_document_to_data_source(address.data_source, document, document_service)

return _add_document_to_entity_or_list(
address=address,
document=document,
files={f.filename: f.file for f in files} if files else None,
update_uncontained=update_uncontained,
document_service=document_service,
)
6 changes: 1 addition & 5 deletions src/features/document/use_cases/update_document_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def _update_document(
data: Union[dict, list],
document_service: DocumentService,
files: dict[str, BinaryIO] | None,
update_uncontained: Optional[bool],
):
"""
Update a document.
Expand Down Expand Up @@ -50,7 +49,7 @@ def _update_document(
if files:
merge_entity_and_files(node, files)

document_service.save(node, address.data_source, update_uncontained=update_uncontained, initial=True)
document_service.save(node, address.data_source, initial=True)
logger.info(f"Updated entity '{address}'")
return {"data": tree_node_to_dict(node)}

Expand All @@ -60,7 +59,6 @@ def update_document_use_case(
data: Union[dict, list],
document_service: DocumentService,
files: Optional[List[UploadFile]] = None,
update_uncontained: Optional[bool] = True,
):
"""Update document.
Expand All @@ -69,7 +67,6 @@ def update_document_use_case(
data: The data to be updated
document_service: The document service
files: Dict with names and files of the files contained in the document
update_uncontained: Whether to update uncontained children (deprecated)
Returns:
A dict that contains the updated document.
"""
Expand All @@ -79,7 +76,6 @@ def update_document_use_case(
data=data,
document_service=document_service,
files={f.filename: f.file for f in files} if files else None,
update_uncontained=update_uncontained,
)
# Do not invalidate the blueprint cache if it was not a blueprint that was changed
if "type" in document["data"] and document["data"]["type"] == SIMOS.BLUEPRINT.value:
Expand Down
18 changes: 11 additions & 7 deletions src/features/export/use_cases/export_use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ def save_node_to_zipfile(
if document_meta:
document_node.entity["_meta_"] = document_meta
storage_client = ZipFileClient(zip_file)
document_service.save(
document_node,
data_source_id,
storage_client,
update_uncontained=True,
combined_document_meta=document_meta,
)
path = "" # Path here is used to get the proper file structure in the zip file
for node in document_node.traverse():
if not node.storage_contained and not node.is_array():
path = f"{path}/{node.entity['name']}/" if path else f"{node.entity['name']}"
document_service.save(
node=node,
data_source_id=data_source_id,
path=path,
repository=storage_client,
combined_document_meta=document_meta,
)


def create_zip_export(document_service: DocumentService, address: Address, user: User) -> str:
Expand Down
21 changes: 3 additions & 18 deletions src/services/document_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ def save(
data_source_id: str,
repository=None,
path="",
update_uncontained: bool = False,
combined_document_meta: dict | None = None,
initial: bool = False,
) -> Dict:
"""
Recursively saves a Node.
Digs down to the leaf children, and based on storageContained,
either saves the entity and returns the Reference, OR returns the entire entity.
Saves a Node.
Converting uncontained child entities to references.
node: The Node to save
path: A Filesystem equivalent path for this node. Used when writing zip-files.
Expand All @@ -132,9 +130,7 @@ def save(
"""
if initial and node.storage_contained:
self.save(
node.parent, data_source_id, repository, path, update_uncontained, combined_document_meta, initial
)
self.save(node.parent, data_source_id, repository, path, combined_document_meta, initial)
if not node.entity:
return {}
# If not passed a custom repository to save into, use the DocumentService's storage
Expand All @@ -144,7 +140,6 @@ def save(
# If the node is a package, we build the path string to be used by filesystem like repositories.
# Also, check for duplicate names in the package.
if node.type == SIMOS.PACKAGE.value:
path = f"{path}/{node.entity['name']}/" if path else f"{node.entity['name']}"
if len(node.children) > 0:
packageContent = node.children[0]
contentListNames = []
Expand All @@ -158,16 +153,6 @@ def save(

contentListNames.append(child.entity["name"])

if update_uncontained: # If flag is set, dig down and save uncontained documents
for child in node.children:
if child.is_array():
[
self.save(x, data_source_id, repository, path, update_uncontained, combined_document_meta)
for x in child.children
]
else:
self.save(child, data_source_id, repository, path, update_uncontained, combined_document_meta)

if node.type == SIMOS.BLOB.value:
node.entity = self.save_blob_data(node, repository)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Set logged in user as owner when creating an entity
"""
Given the logged in user is "johndoe" with roles "dmss-admin"
Given authentication is enabled
Given i access the resource url "/api/documents/test-DS/$2.content?update_uncontained=False"
Given i access the resource url "/api/documents/test-DS/$2.content"
When i make a form-data "POST" request
"""
{
Expand Down
16 changes: 8 additions & 8 deletions src/tests/bdd/document/add_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Feature: Explorer - Add file
"""

Scenario: Add file - not contained
Given i access the resource url "/api/documents/test-DS/$1.content?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/$1.content"
When i make a form-data "POST" request
"""
{
Expand Down Expand Up @@ -502,7 +502,7 @@ Feature: Explorer - Add file
Then the response status should be "OK"

Scenario: Add Parent entity without a name attribute with -by-path endpoint
Given i access the resource url "/api/documents/test-DS/root_package?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/root_package"
When i make a "POST" request with "1" files
"""
{
Expand Down Expand Up @@ -557,7 +557,7 @@ Feature: Explorer - Add file
And the response should have valid uid

Scenario: Add Comment entity without a name attribute with -by-path endpoint
Given i access the resource url "/api/documents/test-DS/root_package?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/root_package"
When i make a "POST" request with "1" files
"""
{
Expand All @@ -572,7 +572,7 @@ Feature: Explorer - Add file
Then the response status should be "OK"

Scenario: Add blueprint without a name attribute with -by-path endpoint should fail
Given i access the resource url "/api/documents/test-DS/root_package?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/root_package"
When i make a "POST" request with "1" files
"""
{
Expand All @@ -596,7 +596,7 @@ Feature: Explorer - Add file
"""

Scenario: Add package without a name attribute with -by-path endpoint should fail
Given i access the resource url "/api/documents/test-DS/root_package?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/root_package"
When i make a "POST" request with "1" files
"""
{
Expand All @@ -620,7 +620,7 @@ Feature: Explorer - Add file
"""

Scenario: Add parent entity without a name attribute with add_by_parent_id endpoint
Given i access the resource url "/api/documents/test-DS/1.content?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/1.content"
When i make a form-data "POST" request
"""
{
Expand All @@ -643,7 +643,7 @@ Feature: Explorer - Add file
"""

Scenario: Add comment entity without a name attribute with add_by_parent_id endpoint
Given i access the resource url "/api/documents/test-DS/$1.content?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/$1.content"
When i make a form-data "POST" request
"""
{
Expand Down Expand Up @@ -703,7 +703,7 @@ Feature: Explorer - Add file
"""

Scenario: Add file with multiple PDFs
Given i access the resource url "/api/documents/test-DS/root_package?update_uncontained=True"
Given i access the resource url "/api/documents/test-DS/root_package"
When i make a "POST" request with "4" files
"""
{
Expand Down
2 changes: 1 addition & 1 deletion src/tests/bdd/document/remove.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Explorer - Remove
| 4 | 1 | sub_package_2 | | dmss://system/SIMOS/Package |
| 3 | 2 | document_1 | | dmss://system/SIMOS/Blueprint |

Scenario: Remove root package
Scenario: Remove root package by id
Given i access the resource url "/api/documents/data-source-name/$1"
When i make a "DELETE" request
Then the response status should be "OK"
Expand Down
2 changes: 1 addition & 1 deletion src/tests/bdd/document/remove_2.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Explorer - Remove by path
| 4 | 1 | sub_package_2 | | dmss://system/SIMOS/Package |
| 3 | 2 | document_1 | | dmss://system/SIMOS/Blueprint |

Scenario: Remove root package
Scenario: Remove root package by name
Given i access the resource url "/api/documents/data-source-name/blueprints"
When i make a "DELETE" request
Then the response status should be "OK"
Expand Down
4 changes: 3 additions & 1 deletion src/tests/bdd/steps/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def step_impl_documents(context, data_source_id: str, collection: str):
document_service = DocumentService(get_data_source, user=context.user)
tree: Node = generate_tree(data_source_id, context.table, document_service)
tree.show_tree()
document_service.save(node=tree, data_source_id=data_source_id, update_uncontained=True)
for node in tree.traverse():
if not node.storage_contained and not node.is_array():
document_service.save(node=node, data_source_id=data_source_id)


@given('AccessControlList for document "{document_id}" in data-source "{data_source}" is')
Expand Down
Loading

0 comments on commit 25eac2f

Please sign in to comment.