From: Emilie Date: Fri, 30 Sep 2016 15:52:00 +0000 (+0200) Subject: saving > test on order X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=68b483a0af6ba87ad1d1c3202460d90ebbde299e;p=mezzo.git saving > test on order --- 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/0003_auto_20160930_1039.py deleted file mode 100644 index 0caaa913..00000000 --- a/app/organization/media/migrations/0003_auto_20160930_1039.py +++ /dev/null @@ -1,29 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.9.7 on 2016-09-30 08:39 -from __future__ import unicode_literals - -import datetime -from django.db import migrations, models -from django.utils.timezone import utc - - -class Migration(migrations.Migration): - - dependencies = [ - ('organization-media', '0002_auto_20160929_1310'), - ] - - operations = [ - migrations.AddField( - model_name='audio', - name='created_at', - field=models.DateTimeField(auto_now=True, default=datetime.datetime(2016, 9, 30, 8, 39, 34, 557510, tzinfo=utc)), - preserve_default=False, - ), - migrations.AddField( - model_name='video', - name='created_at', - field=models.DateTimeField(auto_now=True, default=datetime.datetime(2016, 9, 30, 8, 39, 38, 786940, tzinfo=utc)), - preserve_default=False, - ), - ] diff --git a/app/organization/media/migrations/0004_auto_20160930_1039.py b/app/organization/media/migrations/0004_auto_20160930_1039.py new file mode 100644 index 00000000..309a1946 --- /dev/null +++ b/app/organization/media/migrations/0004_auto_20160930_1039.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.7 on 2016-09-30 08:39 +from __future__ import unicode_literals + +import datetime +from django.db import migrations, models +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + + dependencies = [ + ('organization-media', '0003_auto_20160929_1835'), + ] + + operations = [ + migrations.AddField( + model_name='audio', + name='created_at', + field=models.DateTimeField(auto_now=True, default=datetime.datetime(2016, 9, 30, 8, 39, 34, 557510, tzinfo=utc)), + preserve_default=False, + ), + migrations.AddField( + model_name='video', + name='created_at', + field=models.DateTimeField(auto_now=True, default=datetime.datetime(2016, 9, 30, 8, 39, 38, 786940, tzinfo=utc)), + preserve_default=False, + ), + ] 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