|  | 
| 1 |  | -import json, django | 
| 2 |  | - | 
| 3 |  | -from django.core.exceptions import ValidationError | 
|  | 1 | +import json | 
|  | 2 | +import django | 
| 4 | 3 | from future.builtins import str, int | 
| 5 | 4 | from distutils.version import StrictVersion | 
| 6 | 5 | from django.conf import settings | 
| 7 | 6 | from django.core.urlresolvers import reverse | 
|  | 7 | +from django.core.exceptions import ValidationError | 
| 8 | 8 | from django.db import transaction | 
| 9 | 9 | from django.http import HttpResponseNotFound, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect | 
|  | 10 | +from django.utils.six import integer_types | 
| 10 | 11 | from django.utils.translation import ugettext_lazy as _ | 
| 11 | 12 | from mptt.exceptions import InvalidMove | 
| 12 | 13 | from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicModelChoiceForm | 
| @@ -189,10 +190,22 @@ def api_node_moved_view(self, request): | 
| 189 | 190 |  Update the position of a node, from a API request. | 
| 190 | 191 |  """ | 
| 191 | 192 |  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 | + | 
| 194 | 200 |  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 | 
| 196 | 209 | 
 | 
| 197 | 210 |  # Not using .non_polymorphic() so all models are downcasted to the derived model. | 
| 198 | 211 |  # 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): | 
| 227 | 240 |  'error': error | 
| 228 | 241 |  }), content_type='application/json', status=409) # Conflict | 
| 229 | 242 | 
 | 
| 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): | 
| 231 | 244 |  return HttpResponse(json.dumps({ | 
| 232 | 245 |  'action': 'reload', | 
| 233 | 246 |  'error': 'Client seems to be out-of-sync, please reload!' | 
| @@ -294,4 +307,3 @@ def _get_opt(model): | 
| 294 | 307 |  return model._meta.app_label, model._meta.model_name # Django 1.7 format | 
| 295 | 308 |  except AttributeError: | 
| 296 | 309 |  return model._meta.app_label, model._meta.module_name | 
| 297 |  | - | 
|  | 
0 commit comments