Skip to content

Commit e87e3eb

Browse files
committed
Added circular parent relationship detection in the Group.is_decendant_of() method and in the group edit form data cleaning.
- Legacy-Id: 13686
1 parent eb610d2 commit e87e3eb

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

ietf/group/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ def has_role(self, user, role_names):
9494

9595
def is_decendant_of(self, sought_parent):
9696
p = self.parent
97-
while ((p != None) and (p != self)):
97+
seen = set()
98+
while ((p != None) and (p != self) and (p not in seen)):
99+
seen.add(p)
98100
if p.acronym == sought_parent:
99101
return True
100102
p = p.parent

ietf/group/views_edit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ def clean_delegates(self):
165165
MAX_GROUP_DELEGATES, len(self.cleaned_data["delegates"]) - MAX_GROUP_DELEGATES))
166166
return self.cleaned_data["delegates"]
167167

168+
def clean_parent(self):
169+
p = self.cleaned_data["parent"]
170+
seen = set()
171+
if self.group:
172+
seen.add(self.group)
173+
while p != None and p not in seen:
174+
seen.add(p)
175+
p = p.parent
176+
if p is None:
177+
return self.cleaned_data["parent"]
178+
else:
179+
raise forms.ValidationError("A group cannot be its own ancestor. "
180+
"Found that the group '%s' would end up being the ancestor of (%s)" % (p.acronym, ', '.join([g.acronym for g in seen])))
181+
168182
def clean(self):
169183
cleaned_data = super(GroupForm, self).clean()
170184
state = cleaned_data.get('state', None)

0 commit comments

Comments
 (0)