Skip to content

Commit b76be87

Browse files
committed
Improve PR django-polymorphic#24, also perform a parent test for left/right movements
1 parent 453ef11 commit b76be87

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

polymorphic_tree/admin/parentadmin.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,26 @@ def api_node_moved_view(self, request):
217217
'error': _('You do not have permission to move this node.')
218218
}), content_type='application/json', status=409)
219219

220+
# Find out which parent the node will reside under.
221+
parent_attr_id = '{}_id'.format(moved._mptt_meta.parent_attr)
222+
220223
if position == 'inside':
224+
test_new_parent = target
225+
else:
226+
# left/right of an other node
227+
if getattr(target, parent_attr_id) != getattr(moved, parent_attr_id):
228+
test_new_parent = getattr(target, moved._mptt_meta.parent_attr)
229+
else:
230+
test_new_parent = None # kept inside the same parent.
231+
232+
# Test whether the parent allows this node to be a child.
233+
if test_new_parent is not None:
221234
error = None
222-
if not self.can_have_children(target):
235+
if not self.can_have_children(test_new_parent):
223236
error = _(u'Cannot place \u2018{0}\u2019 below \u2018{1}\u2019;'
224237
u' a {2} does not allow children!').format(moved, target,
225238
target._meta.verbose_name)
226-
elif not self.can_have_children(target, moved):
239+
elif not self.can_have_children(test_new_parent, child=moved):
227240
error = _(u'Cannot place \u2018{0}\u2019 below \u2018{1}\u2019;'
228241
u' a {2} does not allow {3} as a child!').format(moved,
229242
target, target._meta.verbose_name, moved._meta.verbose_name)
@@ -235,7 +248,6 @@ def api_node_moved_view(self, request):
235248
}), content_type='application/json', status=409) # Conflict
236249

237250
# Compare on strings to support UUID fields.
238-
parent_attr_id = '{}_id'.format(moved._mptt_meta.parent_attr)
239251
if str(getattr(moved, parent_attr_id)) != str(previous_parent_id):
240252
return HttpResponse(json.dumps({
241253
'action': 'reload',

0 commit comments

Comments
 (0)