def clean_parent(self): parent = self.cleaned_data.get('parent') if parent: # tree node cannot have itself as a parent if parent.id and self._instance.id and parent.id == self._instance.id: raise forms.ValidationError(self.ERROR_ITSELF_AS_PARENT) # parent node cannot be a child of the node we are editing if is_any_child_of(parent, self._instance): raise forms.ValidationError(self.ERROR_PARENT_AS_CHILD) return parent
def clean_parent(self): parent = self.cleaned_data.get('parent') if parent: # tree node cannot have itself as a parent if parent.id and self.instance.id and parent.id == self.instance.id: raise forms.ValidationError('Cannot have itself as parent.') # new parent node cannot be a child of the node we are editing if is_any_child_of(parent, self.instance): raise forms.ValidationError( 'New parent cannot be a child of this category.') return parent
def test_should_return_true_if_indirect_parent(self): self.assertTrue(is_any_child_of(self.a1, self.root))
def test_should_return_false_if_not_a_child(self): self.assertFalse(is_any_child_of(self.a, self.a1)) self.assertFalse(is_any_child_of(self.a1, self.c))
def test_should_return_false_if_no_id(self): self.assertFalse(is_any_child_of(self.a, MediaFolder())) self.assertFalse(is_any_child_of(MediaFolder(), self.root))
def test_should_return_false_if_none(self): self.assertFalse(is_any_child_of(None, None)) self.assertFalse(is_any_child_of(self.a, None)) self.assertFalse(is_any_child_of(None, self.root))