From 6aa1aa78a81dc22d7c04ed17a705458f727dcc07 Mon Sep 17 00:00:00 2001 From: irinaschubert Date: Tue, 14 Dec 2021 14:49:43 +0100 Subject: [PATCH] fix(groups): make groups optional (DEV-138) (#135) * fix error in test-data.xml * optionalize groups in ontology creation * refactor group handling * Update documentation --- docs/dsp-tools-create.md | 2 -- knora/dsplib/utils/onto_create_ontology.py | 20 +++++++++++--------- knora/dsplib/utils/xml_upload.py | 14 ++++++++++---- testdata/test-data.xml | 1 - 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/docs/dsp-tools-create.md b/docs/dsp-tools-create.md index 4209cfa07..d743ce695 100644 --- a/docs/dsp-tools-create.md +++ b/docs/dsp-tools-create.md @@ -383,8 +383,6 @@ Example: } ``` -The `groups` element is optional. If not used, it should be omitted. It is currently not recommended using it. - ### Users (optional) diff --git a/knora/dsplib/utils/onto_create_ontology.py b/knora/dsplib/utils/onto_create_ontology.py index a4ec1d0ea..124da5b21 100644 --- a/knora/dsplib/utils/onto_create_ontology.py +++ b/knora/dsplib/utils/onto_create_ontology.py @@ -131,22 +131,24 @@ def create_ontology(input_file: str, new_groups = {} groups = data_model["project"].get('groups') - if groups is not None: + if groups: for group in groups: try: new_group = Group(con=con, name=group["name"], descriptions=LangString(group["descriptions"]), project=project, - status=group["status"] if group.get("status") is not None else True, - selfjoin=group["selfjoin"] if group.get("selfjoin") is not None else False).create() + status=group["status"] if group.get("status") else True, + selfjoin=group["selfjoin"] if group.get("selfjoin") else False).create() + new_groups[new_group.name] = new_group + if verbose: + print("Created group:") + new_group.print() # project.set_default_permissions(new_group.id) + except BaseError as err: - print("Creating group has failed: ", err.message) - return False - new_groups[new_group.name] = new_group - if verbose: - print("Groups:") - new_group.print() # project.set_default_permissions(new_group.id) + print(f"ERROR while trying to create group '{group.name}'. The error message was: {err.message}") + except Exception as exception: + print(f"ERROR while trying to create group '{group.name}'. The error message was: {exception}") # create the user(s) if verbose: diff --git a/knora/dsplib/utils/xml_upload.py b/knora/dsplib/utils/xml_upload.py index 50089cef9..c4d339d54 100644 --- a/knora/dsplib/utils/xml_upload.py +++ b/knora/dsplib/utils/xml_upload.py @@ -36,8 +36,8 @@ class ProjectContext: _projects: list[Project] _project_map: Dict[str, str] # dictionary of (project name:project IRI) pairs _inv_project_map: Dict[str, str] # dictionary of (project IRI:project name) pairs - _groups: list[Group] - _group_map: Dict[str, str] + _groups: Optional[list[Group]] + _group_map: Optional[Dict[str, str]] _shortcode: Optional[str] _project_name: Optional[str] @@ -46,8 +46,14 @@ def __init__(self, con: Connection, shortcode: Optional[str] = None): self._projects = Project.getAllProjects(con=con) self._project_map: Dict[str, str] = {x.shortname: x.id for x in self._projects} self._inv_project_map: Dict[str, str] = {x.id: x.shortname for x in self._projects} - self._groups = Group.getAllGroups(con=con) - self._group_map: Dict[str, str] = {self._inv_project_map[x.project] + ':' + x.name: x.id for x in self._groups} + try: + self._groups = Group.getAllGroups(con=con) + except BaseError: + self._groups = None + if self._groups: + self._group_map: Dict[str, str] = {self._inv_project_map[x.project] + ':' + x.name: x.id for x in self._groups} + else: + self._group_map = None self._project_name = None # get the project name from the shortcode if self._shortcode: diff --git a/testdata/test-data.xml b/testdata/test-data.xml index 9aa64b61c..28cad1e4c 100644 --- a/testdata/test-data.xml +++ b/testdata/test-data.xml @@ -96,7 +96,6 @@ b2 - b2 obj_0000