storage: add support for sorting disparate types (#36515)
This commit is contained in:
parent
c089fbe70c
commit
ca35deba02
|
@ -24,6 +24,7 @@ import shutil
|
|||
import sys
|
||||
import tempfile
|
||||
|
||||
from django.utils import six
|
||||
from django.utils.six.moves import _thread
|
||||
|
||||
from .vendor import locket
|
||||
|
@ -94,9 +95,19 @@ class Criteria(object):
|
|||
def __init__(self, attribute, value):
|
||||
self.attribute = attribute
|
||||
self.value = value
|
||||
# Python 3 requires comparisons to disparate types, this means we need
|
||||
# to create a null value of the appropriate type, so None values can
|
||||
# still be sorted.
|
||||
self.typed_none = ''
|
||||
if isinstance(self.value, bool):
|
||||
self.typed_none = False
|
||||
elif isinstance(self.value, six.integer_types + (float,)):
|
||||
self.typed_none = -sys.maxsize
|
||||
elif isinstance(self.value, time.struct_time):
|
||||
self.typed_none = time.gmtime(-10**10) # 1653
|
||||
|
||||
def build_lambda(self):
|
||||
return lambda x: self.op(getattr(x, self.attribute, None), self.value)
|
||||
return lambda x: self.op(getattr(x, self.attribute, None) or self.typed_none, self.value)
|
||||
|
||||
|
||||
class Less(Criteria):
|
||||
|
|
Loading…
Reference in New Issue