Modified classmethod queries that determine if a Model is sortable to only retieve one record and not use count.
Fixed jQueryUI highlight method call in success handler of ajax function when dragging stops. Fixed missing script resource for jquery.effects.core.jsmaster
parent
638f26df27
commit
8365043e0e
|
|
@ -4,3 +4,4 @@ django_admin_sortable.egg-info
|
||||||
.pydevproject
|
.pydevproject
|
||||||
*.pyc
|
*.pyc
|
||||||
*.pyo
|
*.pyo
|
||||||
|
.idea
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,30 @@ from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class Sortable(models.Model):
|
class Sortable(models.Model):
|
||||||
|
"""
|
||||||
|
Unfortunately, Django doesn't support using more than one AutoField in a model
|
||||||
|
or this class could be simplified.
|
||||||
|
|
||||||
|
`is_sortable` determines whether or not the Model is sortable by determining
|
||||||
|
if the last value of `order` is greater than the default of 1, which should be
|
||||||
|
present if there is only one object.
|
||||||
|
|
||||||
|
`model_type_id` returns the ContentType.id for the Model that inherits Sortable
|
||||||
|
|
||||||
|
`save` the override of save increments the last/highest value of order by 1
|
||||||
|
"""
|
||||||
order = models.PositiveIntegerField(editable=False, default=1, db_index=True)
|
order = models.PositiveIntegerField(editable=False, default=1, db_index=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
ordering = ['order', 'id']
|
ordering = ['order']
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_sortable(cls):
|
def is_sortable(cls):
|
||||||
return True if cls.objects.count() > 1 else False
|
try:
|
||||||
|
return True if cls.objects.order_by('-order')[:1][0].order > 1 else False
|
||||||
|
except IndexError:
|
||||||
|
return False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def model_type_id(cls):
|
def model_type_id(cls):
|
||||||
|
|
@ -19,5 +34,10 @@ class Sortable(models.Model):
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.id:
|
if not self.id:
|
||||||
self.order = self.__class__.objects.count() + 1
|
try:
|
||||||
|
#increment the order field by adding one to the last value of order
|
||||||
|
self.order = self.__class__.objects.order_by('-order')[:1][0].order + 1
|
||||||
|
except IndexError:
|
||||||
|
#order defaults to 1
|
||||||
|
pass
|
||||||
super(Sortable, self).save(*args, **kwargs)
|
super(Sortable, self).save(*args, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ jQuery(function($){
|
||||||
data: { indexes : indexes.join(',') },
|
data: { indexes : indexes.join(',') },
|
||||||
success: function()
|
success: function()
|
||||||
{
|
{
|
||||||
ui.effect('highlight');
|
ui.item.effect('highlight', {}, 1000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -34,7 +34,7 @@ class SortableTestCase(TestCase):
|
||||||
self.user.save()
|
self.user.save()
|
||||||
|
|
||||||
def create_category(self, title='Category 1'):
|
def create_category(self, title='Category 1'):
|
||||||
category = Category.objects.create(title=title)
|
category, _ = Category.objects.get_or_create(title=title)
|
||||||
return category
|
return category
|
||||||
|
|
||||||
def test_new_user_is_authenticated(self):
|
def test_new_user_is_authenticated(self):
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,7 @@ MIDDLEWARE_CLASSES = (
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
|
'debug_toolbar.middleware.DebugToolbarMiddleware',
|
||||||
)
|
)
|
||||||
|
|
||||||
TEMPLATE_CONTEXT_PROCESSORS = (
|
TEMPLATE_CONTEXT_PROCESSORS = (
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,10 @@ jQuery(function($){
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: ui.item.find('a.admin_sorting_url').attr('href'),
|
url: ui.item.find('a.admin_sorting_url').attr('href'),
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: { indexes: indexes.join(',') }
|
data: { indexes: indexes.join(',') },
|
||||||
|
success: function(){
|
||||||
|
ui.item.effect('highlight', {}, 1000);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue