Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions polymorphic_tree/admin/parentadmin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json, django
import json
import django
from django.utils.six import integer_types
from future.builtins import str, int
from distutils.version import StrictVersion
from django.conf import settings
Expand Down Expand Up @@ -178,10 +180,22 @@ def api_node_moved_view(self, request):
Update the position of a node, from a API request.
"""
try:
moved_id = int(request.POST['moved_id'])
target_id = int(request.POST['target_id'])
try:
moved_id = int(request.POST['moved_id'])
target_id = int(request.POST['target_id'])
except ValueError:
moved_id = request.POST['moved_id']
target_id = request.POST['target_id']

position = request.POST['position']
previous_parent_id = int(request.POST['previous_parent_id']) or None

if request.POST.get('previous_parent_id'):
if isinstance(moved_id, integer_types) and isinstance(target_id, integer_types):
previous_parent_id = int(request.POST['previous_parent_id'])
else:
previous_parent_id = request.POST['previous_parent_id']
else:
previous_parent_id = None

# Not using .non_polymorphic() so all models are downcasted to the derived model.
# This causes the signal below to be emitted from the proper class as well.
Expand All @@ -198,7 +212,7 @@ def api_node_moved_view(self, request):
'moved_id': moved_id,
'error': _(u'Cannot place \u2018{0}\u2019 below \u2018{1}\u2019; a {2} does not allow children!').format(moved, target, target._meta.verbose_name)
}), content_type='application/json', status=409) # Conflict
if moved.parent_id != previous_parent_id:
if str(moved.parent_id) != str(previous_parent_id):
return HttpResponse(json.dumps({
'action': 'reload',
'error': 'Client seems to be out-of-sync, please reload!'
Expand Down Expand Up @@ -253,4 +267,3 @@ def _get_opt(model):
return model._meta.app_label, model._meta.model_name # Django 1.7 format
except AttributeError:
return model._meta.app_label, model._meta.module_name

3 changes: 2 additions & 1 deletion polymorphic_tree/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Model that inherits from both Polymorphic and MPTT.
"""
import uuid
import django
from future.utils import with_metaclass
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -52,7 +53,7 @@ def clean(self, value, model_instance):
def _validate_parent(self, parent, model_instance):
if not parent:
return
elif isinstance(parent, integer_types):
elif isinstance(parent, integer_types) or isinstance(parent, uuid.UUID):
# TODO: Improve this code, it's a bit of a hack now because the base model is not known in the NodeTypePool.
base_model = _get_base_polymorphic_model(model_instance.__class__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*/

var data = [{% adminlist_recursetree cl %}{% with is_leaf=node.is_leaf_node %}
{id: {{ node.pk }}, classes: 'nodetype-{{ node|real_model_name|lower }}', can_have_children: {{ node.can_have_children|yesno:'true,false' }}, label: '<div class="col-primary{% if is_leaf %} leaf{% endif %}"><div class="col first-column"><a href="{{ change_url }}"{% if cl.is_popup %} onclick="opener.dismissRelatedLookupPopup(window, {{ node.pk }}); return false;"{% endif %}>{{ first_column|escapejs }}</a></div></div>{% if other_columns %}<div class="col-metadata">{% for name, repr in other_columns %}<div class="col col-{{ name }}">{{ repr|escapejs }}</div>{% endfor %}</div>{% endif %}'{% if not is_leaf %},
{id: '{{ node.pk }}', classes: 'nodetype-{{ node|real_model_name|lower }}', can_have_children: {{ node.can_have_children|yesno:'true,false' }}, label: '<div class="col-primary{% if is_leaf %} leaf{% endif %}"><div class="col first-column"><a href="{{ change_url }}"{% if cl.is_popup %} onclick="opener.dismissRelatedLookupPopup(window, {{ node.pk }}); return false;"{% endif %}>{{ first_column|escapejs }}</a></div></div>{% if other_columns %}<div class="col-metadata">{% for name, repr in other_columns %}<div class="col col-{{ name }}">{{ repr|escapejs }}</div>{% endfor %}</div>{% endif %}'{% if not is_leaf %},
children: [ {{ children }}
]{% endif %}},{% endwith %}{% endadminlist_recursetree %}
null // for MSIE
Expand Down Expand Up @@ -88,9 +88,9 @@
url: '{{ cl.model_admin.api_node_moved_view_url }}',
dataType: 'json',
data: {
'moved_id': parseInt(move_info.moved_node.id),
'target_id': parseInt(move_info.target_node.id),
'previous_parent_id': parseInt(move_info.previous_parent.id || 0),
'moved_id': move_info.moved_node.id,
'target_id': move_info.target_node.id,
'previous_parent_id': move_info.previous_parent.id,
'position': move_info.position,
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
Expand Down