From 35511cfdc1a2b70c3ac98f951b933e9fe5c076bf Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 7 Feb 2020 11:59:32 -0500 Subject: [PATCH] Remove NaturalOrderingManager --- netbox/utilities/managers.py | 45 ------------------------------------ 1 file changed, 45 deletions(-) delete mode 100644 netbox/utilities/managers.py diff --git a/netbox/utilities/managers.py b/netbox/utilities/managers.py deleted file mode 100644 index ad646a78e..000000000 --- a/netbox/utilities/managers.py +++ /dev/null @@ -1,45 +0,0 @@ -from django.db.models import Manager -from django.db.models.expressions import RawSQL - -NAT1 = r"CAST(SUBSTRING({}.{} FROM '^(\d{{1,9}})') AS integer)" -NAT2 = r"SUBSTRING({}.{} FROM '^\d*(.*?)\d*$')" -NAT3 = r"CAST(SUBSTRING({}.{} FROM '(\d{{1,9}})$') AS integer)" - - -class NaturalOrderingManager(Manager): - """ - Order objects naturally by a designated field (defaults to 'name'). Leading and/or trailing digits of values within - this field will be cast as independent integers and sorted accordingly. For example, "Foo2" will be ordered before - "Foo10", even though the digit 1 is normally ordered before the digit 2. - """ - natural_order_field = 'name' - - def get_queryset(self): - - queryset = super().get_queryset() - - db_table = self.model._meta.db_table - db_field = self.natural_order_field - - # Append the three subfields derived from the designated natural ordering field - queryset = ( - queryset.annotate(_nat1=RawSQL(NAT1.format(db_table, db_field), ())) - .annotate(_nat2=RawSQL(NAT2.format(db_table, db_field), ())) - .annotate(_nat3=RawSQL(NAT3.format(db_table, db_field), ())) - ) - - # Replace any instance of the designated natural ordering field with its three subfields - ordering = [] - for field in self.model._meta.ordering: - if field == self.natural_order_field: - ordering.append('_nat1') - ordering.append('_nat2') - ordering.append('_nat3') - else: - ordering.append(field) - - # Default to using the _nat indexes if Meta.ordering is empty - if not ordering: - ordering = ('_nat1', '_nat2', '_nat3') - - return queryset.order_by(*ordering)