From 68b483a0af6ba87ad1d1c3202460d90ebbde299e Mon Sep 17 00:00:00 2001 From: Emilie Date: Fri, 30 Sep 2016 17:52:00 +0200 Subject: [PATCH] saving > test on order --- app/organization/core/utils.py | 33 +++++++++++++++---- ...930_1039.py => 0004_auto_20160930_1039.py} | 2 +- app/organization/media/views.py | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) rename app/organization/media/migrations/{0003_auto_20160930_1039.py => 0004_auto_20160930_1039.py} (93%) diff --git a/app/organization/core/utils.py b/app/organization/core/utils.py index 5c2c918f..e13a9fe8 100644 --- a/app/organization/core/utils.py +++ b/app/organization/core/utils.py @@ -144,7 +144,7 @@ class IableSequence(object): items[-1] = "...(remaining elements truncated)..." return repr(items) - +# http://stackoverflow.com/questions/1516249/python-list-sorting-with-multiple-attributes-and-mixed-order class QuerySetSequence(IableSequence): ''' Wrapper for the query sets sequence without the restriction on the identity @@ -173,19 +173,18 @@ class QuerySetSequence(IableSequence): field_names[i] = field_name[1:] # wanna iterable and attrgetter returns single item if 1 arg supplied fields_getter = lambda i: chain_sing(attrgetter(*field_names)(i)) + print("************************") + print(fields_getter) + print("************************") # comparator gets the first non-zero value of the field comparison # results taking into account reverse order for fields prefixed with '-' comparator = lambda i1, i2:\ dropwhile(__not__, - mul_it(map(cmp, fields_getter(i1), fields_getter(i2)), reverses) + mul_it(map(key, fields_getter(i1), fields_getter(i2)), reverses) ).__next__() - print("-----------------------------------------"); - print(comparator); - print("******************************************"); - print("comparator"); # return new sorted list - return sorted(self.collapse(), cmp=comparator) + return sorted(self.collapse(), key=fields_getter) def filter(self, *args, **kwargs): """ @@ -235,3 +234,23 @@ class QuerySetSequence(IableSequence): if qs.exists(): return True return False + + +def cmp_to_key(mycmp): + 'Convert a cmp= function into a key= function' + class K(object): + def __init__(self, obj, *args): + self.obj = obj + def __lt__(self, other): + return mycmp(self.obj, other.obj) < 0 + def __gt__(self, other): + return mycmp(self.obj, other.obj) > 0 + def __eq__(self, other): + return mycmp(self.obj, other.obj) == 0 + def __le__(self, other): + return mycmp(self.obj, other.obj) <= 0 + def __ge__(self, other): + return mycmp(self.obj, other.obj) >= 0 + def __ne__(self, other): + return mycmp(self.obj, other.obj) != 0 + return K diff --git a/app/organization/media/migrations/0003_auto_20160930_1039.py b/app/organization/media/migrations/0004_auto_20160930_1039.py similarity index 93% rename from app/organization/media/migrations/0003_auto_20160930_1039.py rename to app/organization/media/migrations/0004_auto_20160930_1039.py index 0caaa913..309a1946 100644 --- a/app/organization/media/migrations/0003_auto_20160930_1039.py +++ b/app/organization/media/migrations/0004_auto_20160930_1039.py @@ -10,7 +10,7 @@ from django.utils.timezone import utc class Migration(migrations.Migration): dependencies = [ - ('organization-media', '0002_auto_20160929_1310'), + ('organization-media', '0003_auto_20160929_1835'), ] operations = [ diff --git a/app/organization/media/views.py b/app/organization/media/views.py index cbdce938..525895f8 100644 --- a/app/organization/media/views.py +++ b/app/organization/media/views.py @@ -55,7 +55,7 @@ class MediaListView(ListView): audios = Audio.objects.all() videos = Video.objects.all() qsseq = QuerySetSequence(audios, videos) - qsseq.order_by('blog.name','-title') + qsseq.order_by('created_at',) print("----------------------------------") print(len(qsseq)) return qsseq -- 2.39.5