Skip to content

Commit 7bc87b6

Browse files
committed
add check for if link exists during import and imrove the error handling
1 parent ffe6589 commit 7bc87b6

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

application/defs/cre_defs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ def todict(self) -> Dict[str, Union[Dict[str, str], List[Any], Set[str], str]]:
362362
def __repr__(self):
363363
return f"{self.todict()}"
364364

365+
def has_link(self, link: Link) -> bool:
366+
return link.document.id in [l.document.id for l in self.links]
367+
365368
def add_link(self, link: Link) -> "Document":
366369
if not self.links:
367370
self.links = []
@@ -381,9 +384,6 @@ def add_link(self, link: Link) -> "Document":
381384
self.links.append(link)
382385
return self
383386

384-
def link_exists(self, doc: "Document") -> bool:
385-
return doc.id in [l.document.id for l in self.links]
386-
387387
def __post_init__(self):
388388
if not len(self.name) > 1:
389389
raise cre_exceptions.InvalidDocumentNameException(self)

application/utils/spreadsheet.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,14 @@ def prepare_spreadsheet(
211211
for link in doc.links:
212212
if link.document.doctype == defs.Credoctypes.CRE:
213213
newCRE = self.input_cres.get(link.document.id)
214+
214215
if not newCRE:
215216
newCRE = link.document
216-
if not newCRE.link_exists(doc):
217-
newCRE.add_link(
218-
defs.Link(document=doc.shallow_copy(), ltype=link.ltype)
219-
)
217+
218+
lnk = defs.Link(document=doc.shallow_copy(), ltype=link.ltype)
219+
if not newCRE.has_link(lnk):
220+
newCRE.add_link(lnk)
221+
220222
self.input_cres[newCRE.id] = newCRE
221223
if not newCRE:
222224
non_cre_docs.append(doc)

application/utils/spreadsheet_parsers.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,12 @@ def parse_export_format(lfile: List[Dict[str, Any]]) -> Dict[str, List[defs.Docu
187187
)
188188
)
189189
elif highest_index < i: # we found a higher hierarchy CRE
190-
if not highest_cre.link_exists(working_cre):
191-
cres[highest_cre.id] = highest_cre.add_link(
192-
defs.Link(
193-
document=working_cre.shallow_copy(),
194-
ltype=defs.LinkTypes.Contains,
195-
)
196-
)
190+
lnk = defs.Link(
191+
document=working_cre.shallow_copy(),
192+
ltype=defs.LinkTypes.Contains,
193+
)
194+
if not highest_cre.has_link(lnk):
195+
cres[highest_cre.id] = highest_cre.add_link(lnk)
197196
else:
198197
logger.warning(
199198
f"Link between {highest_cre.name} and {working_cre.name} already exists"
@@ -442,12 +441,15 @@ def parse_hierarchical_export_format(
442441
)
443442
new_cre = cre_dict[other_cre.strip()]
444443
# we only need a shallow copy here
445-
cre = cre.add_link(
446-
defs.Link(
447-
ltype=defs.LinkTypes.Related,
448-
document=new_cre.shallow_copy(),
449-
)
444+
lnk = defs.Link(
445+
ltype=defs.LinkTypes.Related,
446+
document=new_cre.shallow_copy(),
450447
)
448+
if cre.has_link(lnk):
449+
indx = cre.links.index(lnk)
450+
cre.links[indx].document = new_cre.shallow_copy()
451+
else:
452+
cre = cre.add_link(lnk)
451453

452454
for link in parse_standards(mapping):
453455
doc = link.document

application/web/web_main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def extend_cre_with_tag_links(
7878
others = list(frozenset(others))
7979
for o in others:
8080
o.links = []
81-
if not cre.link_exists(o) and o.id != cre.id:
82-
cre.add_link(defs.Link(ltype=defs.LinkTypes.Related, document=o))
81+
lnk = defs.Link(ltype=defs.LinkTypes.Related, document=o)
82+
if not cre.has_link(lnk) and o.id != cre.id:
83+
cre.add_link(lnk)
8384
return cre
8485

8586

0 commit comments

Comments
 (0)