Преглед на файлове

Solves #96 order with different direction on different fields in MongoDB Datasource

Yann Weber преди 8 години
родител
ревизия
89beb80eca
променени са 1 файла, в които са добавени 16 реда и са изтрити 13 реда
  1. 16
    13
      plugins/mongodb_datasource/datasource.py

+ 16
- 13
plugins/mongodb_datasource/datasource.py Целия файл

@@ -3,7 +3,7 @@
3 3
 import re
4 4
 import warnings
5 5
 import copy
6
-import operator
6
+import functools
7 7
 from bson.son import SON
8 8
 from collections import OrderedDict
9 9
 import pymongo
@@ -107,18 +107,9 @@ class MongoDbDatasource(object):
107 107
             #Here we may implement the group
108 108
             #If sorted query we have to sort again
109 109
             if order is not None:
110
-                sort_itemgetter_args = list()
111
-                sort_dir = None
112
-                for fname, csort_dir in order:
113
-                    sort_itemgetter_args.append(fname)
114
-                    if sort_dir is None:
115
-                        sort_dir = csort_dir
116
-                    elif sort_dir != csort_dir:
117
-                        raise NotImplementedError("Multiple direction for \
118
-ordering is not implemented yet")
119
-                results = sorted(
120
-                    results, key=operator.itemgetter(*sort_itemgetter_args),
121
-                    reverse=False if sort_dir == 'ASC' else True)
110
+                results = sorted(results,
111
+                    key=functools.cmp_to_key(
112
+                        self.__generate_lambda_cmp_order(order)))
122 113
             #If limit given apply limit again
123 114
             if offset > len(results):
124 115
                 results = list()
@@ -501,3 +492,15 @@ field/operator couple in a query. We will keep only the first one")
501 492
                 result[mongop] = mongoval
502 493
         return result
503 494
 
495
+    ##@brief Generate a comparison function for post reccursion sorting in
496
+    #select
497
+    @classmethod
498
+    def __generate_lambda_cmp_order(cls, order):
499
+        if len(order) == 0:
500
+            return lambda a,b: False
501
+        fname, cmpdir = order[0]
502
+        order = order[1:]
503
+        return lambda a,b: 0 if a[fname] == b[fname] else (\
504
+            1 if (a[fname]>b[fname] if cmpdir == 'ASC' else a[fname]<b[fname])\
505
+            else -1)
506
+

Loading…
Отказ
Запис