+class TreeForm(forms.ModelForm):
+ def __init__(self, *args, **kwargs):
+ super(TreeForm, self).__init__(*args, **kwargs)
+ instance = self.instance
+ instance_class = self.get_instance_class()
+
+ if instance_class is not None:
+ try:
+ self.fields['parent'].queryset = instance_class.objects.exclude(id=instance.id)
+ except ObjectDoesNotExist:
+ pass
+
+ self.fields['parent'].validators = [TreeParentValidator(*self.get_validator_args())]
+
+ def get_instance_class(self):
+ return self.instance.__class__
+
+ def get_validator_args(self):
+ return [self.instance]
+
+ def clean(self):
+ cleaned_data = self.cleaned_data
+
+ try:
+ parent = cleaned_data['parent']
+ slug = cleaned_data['slug']
+ obj_class = self.get_instance_class()
+ tpv = TreePositionValidator(parent, slug, obj_class)
+ tpv(self.instance)
+ except KeyError:
+ pass
+
+ return cleaned_data
+
+
+class NodeForm(TreeForm):
+ def get_instance_class(self):
+ return Node
+
+ def get_validator_args(self):
+ return [self.instance, 'instance']
+
+
+class PageAdminForm(NodeForm):
+ class Meta:
+ model = Page
+
+
+class RedirectAdminForm(NodeForm):
+ class Meta:
+ model = Redirect
+
+
+class FileAdminForm(NodeForm):
+ class Meta:
+ model = File
+
+