storage: add support for sorting disparate types (#36515)

This commit is contained in:
Frédéric Péters 2019-11-14 15:06:26 +01:00
parent c089fbe70c
commit ca35deba02
1 changed files with 12 additions and 1 deletions

View File

@ -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):