Skip to content

Commit df8ad04

Browse files
committed
Merge branch 'njamaleddine-uuid-support' of https://github.com/njamaleddine/django-polymorphic-tree
2 parents d65e7b3 + 48dde42 commit df8ad04

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

polymorphic_tree/admin/parentadmin.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import json, django
2-
3-
from django.core.exceptions import ValidationError
1+
import json
2+
import django
43
from future.builtins import str, int
54
from distutils.version import StrictVersion
65
from django.conf import settings
76
from django.core.urlresolvers import reverse
7+
from django.core.exceptions import ValidationError
88
from django.db import transaction
99
from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
10+
from django.utils.six import integer_types
1011
from django.utils.translation import ugettext_lazy as _
1112
from mptt.exceptions import InvalidMove
1213
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicModelChoiceForm
@@ -189,10 +190,22 @@ def api_node_moved_view(self, request):
189190
Update the position of a node, from a API request.
190191
"""
191192
try:
192-
moved_id = int(request.POST['moved_id'])
193-
target_id = int(request.POST['target_id'])
193+
try:
194+
moved_id = int(request.POST['moved_id'])
195+
target_id = int(request.POST['target_id'])
196+
except ValueError:
197+
moved_id = request.POST['moved_id']
198+
target_id = request.POST['target_id']
199+
194200
position = request.POST['position']
195-
previous_parent_id = int(request.POST['previous_parent_id']) or None
201+
202+
if request.POST.get('previous_parent_id'):
203+
if isinstance(moved_id, integer_types) and isinstance(target_id, integer_types):
204+
previous_parent_id = int(request.POST['previous_parent_id'])
205+
else:
206+
previous_parent_id = request.POST['previous_parent_id']
207+
else:
208+
previous_parent_id = None
196209

197210
# Not using .non_polymorphic() so all models are downcasted to the derived model.
198211
# This causes the signal below to be emitted from the proper class as well.
@@ -227,7 +240,7 @@ def api_node_moved_view(self, request):
227240
'error': error
228241
}), content_type='application/json', status=409) # Conflict
229242

230-
if getattr(moved, '{}_id'.format(moved._mptt_meta.parent_attr)) != previous_parent_id:
243+
if str(getattr(moved, '{}_id'.format(moved._mptt_meta.parent_attr))) != str(previous_parent_id):
231244
return HttpResponse(json.dumps({
232245
'action': 'reload',
233246
'error': 'Client seems to be out-of-sync, please reload!'
@@ -294,4 +307,3 @@ def _get_opt(model):
294307
return model._meta.app_label, model._meta.model_name # Django 1.7 format
295308
except AttributeError:
296309
return model._meta.app_label, model._meta.module_name
297-

polymorphic_tree/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Model that inherits from both Polymorphic and MPTT.
33
"""
4+
import uuid
45
import django
56
from six import integer_types, string_types
67
from future.utils import with_metaclass
@@ -53,7 +54,7 @@ def clean(self, value, model_instance):
5354
def _validate_parent(self, parent, model_instance):
5455
if not parent:
5556
return
56-
elif isinstance(parent, integer_types):
57+
elif isinstance(parent, integer_types) or isinstance(parent, uuid.UUID):
5758
# TODO: Improve this code, it's a bit of a hack now because the base model is not known in the NodeTypePool.
5859
base_model = _get_base_polymorphic_model(model_instance.__class__)
5960
parent = base_model.objects.get(pk=parent)

polymorphic_tree/templates/admin/polymorphic_tree/jstree_list_results.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
*/
4848

4949
var data = [{% adminlist_recursetree cl %}{% with is_leaf=node.is_leaf_node %}
50+
<<<<<<< HEAD
5051
{id: {{ node.pk }}, ct: {{ node.polymorphic_ctype_id }}, classes: 'nodetype-{{ ct.model|lower }}', can_have_children: {{ node.can_have_children|yesno:"true,false" }}, child_types: {% firstof node.get_child_types "[]" %}, 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 %},
52+
=======
53+
{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 %},
54+
>>>>>>> 48dde42912c3484f84c7abb9f11b39287233b93f
5155
children: [ {{ children }}
5256
]{% endif %}},{% endwith %}{% endadminlist_recursetree %}
5357
null // for MSIE
@@ -92,9 +96,9 @@
9296
url: '{{ cl.model_admin.api_node_moved_view_url }}',
9397
dataType: 'json',
9498
data: {
95-
'moved_id': parseInt(move_info.moved_node.id),
96-
'target_id': parseInt(move_info.target_node.id),
97-
'previous_parent_id': parseInt(move_info.previous_parent.id || 0),
99+
'moved_id': move_info.moved_node.id,
100+
'target_id': move_info.target_node.id,
101+
'previous_parent_id': move_info.previous_parent.id,
98102
'position': move_info.position,
99103
'csrfmiddlewaretoken': '{{ csrf_token }}'
100104
},

0 commit comments

Comments
 (0)