Skip to content

Commit

Permalink
Create relation only if there is no existing one with same …
Browse files Browse the repository at this point in the history
source, target, relationname.
But mark source as modified.
  • Loading branch information
ksuess committed May 18, 2023
1 parent 512e28f commit e967c53
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/plone/api/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ def create(source=None, target=None, relationship=None):
"from_id": from_id,
"to_id": to_id,
}
has_relation = False
for rel in relation_catalog.findRelations(query):
relation_catalog.unindex(rel)
has_relation = True

if from_attribute == referencedRelationship:
# Don't mess with linkintegrity-relations!
Expand Down Expand Up @@ -212,9 +213,10 @@ def create(source=None, target=None, relationship=None):
source.absolute_url(),
target.absolute_url(),
)
existing_relations = getattr(source, from_attribute, [])
existing_relations.append(RelationValue(to_id))
setattr(source, from_attribute, existing_relations)
if not has_relation:
existing_relations = getattr(source, from_attribute, [])
existing_relations.append(RelationValue(to_id))
setattr(source, from_attribute, existing_relations)
modified(source)
return

Expand All @@ -225,7 +227,8 @@ def create(source=None, target=None, relationship=None):
source.absolute_url(),
target.absolute_url(),
)
setattr(source, from_attribute, RelationValue(to_id))
if not has_relation:
setattr(source, from_attribute, RelationValue(to_id))
modified(source)
return

Expand Down

0 comments on commit e967c53

Please sign in to comment.