2015-08-24 02:25:55 +00:00
|
|
|
from .models import SortableMixin, SortableForeignKey
|
2014-10-25 00:28:36 +00:00
|
|
|
|
|
|
|
|
|
2015-12-23 21:39:45 +00:00
|
|
|
def check_inheritance(cls):
|
|
|
|
|
return issubclass(type(cls), SortableMixin)
|
2014-11-19 15:58:55 +00:00
|
|
|
|
|
|
|
|
|
2013-04-28 02:58:02 +00:00
|
|
|
def get_is_sortable(objects):
|
2015-11-11 15:03:19 +00:00
|
|
|
if objects.count() < 2:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if not check_inheritance(objects[:1][0]):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
2014-10-25 00:28:36 +00:00
|
|
|
|
|
|
|
|
|
2014-11-03 15:43:41 +00:00
|
|
|
def is_self_referential(cls):
|
|
|
|
|
cls_type = type(cls)
|
2014-11-19 15:58:55 +00:00
|
|
|
sortable_subclass = check_inheritance(cls_type)
|
2014-11-03 15:43:41 +00:00
|
|
|
sortable_foreign_key_subclass = issubclass(cls_type, SortableForeignKey)
|
|
|
|
|
if sortable_foreign_key_subclass and not sortable_subclass:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2014-10-25 00:28:36 +00:00
|
|
|
def check_model_is_sortable(cls):
|
|
|
|
|
if cls:
|
2014-11-19 15:58:55 +00:00
|
|
|
if check_inheritance(cls):
|
|
|
|
|
if is_self_referential(cls):
|
|
|
|
|
objects = cls.model.objects
|
|
|
|
|
else:
|
|
|
|
|
objects = cls.objects
|
2015-01-12 15:40:33 +00:00
|
|
|
return get_is_sortable(objects.all())
|
2013-04-28 02:58:02 +00:00
|
|
|
return False
|