File: utils.py

package info (click to toggle)
python-django-adminsortable 2.0.10-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 548 kB
  • sloc: python: 501; javascript: 198; sh: 24; makefile: 3
file content (35 lines) | stat: -rw-r--r-- 876 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from .models import SortableMixin, SortableForeignKey


def check_inheritance(cls):
    return issubclass(type(cls), SortableMixin)


def get_is_sortable(objects):
    if objects.count() < 2:
        return False

    if not check_inheritance(objects[:1][0]):
        return False

    return True


def is_self_referential(cls):
    cls_type = type(cls)
    sortable_subclass = check_inheritance(cls_type)
    sortable_foreign_key_subclass = issubclass(cls_type, SortableForeignKey)
    if sortable_foreign_key_subclass and not sortable_subclass:
        return True
    return False


def check_model_is_sortable(cls):
    if cls:
        if check_inheritance(cls):
            if is_self_referential(cls):
                objects = cls.model.objects
            else:
                objects = cls.objects
            return get_is_sortable(objects.all())
    return False