29
29
30
30
31
31
def search_relationship (session , instance , relation , filters = None , sort = None ,
32
- group_by = None ):
32
+ group_by = None , ignorecase = False ):
33
33
"""Returns a filtered, sorted, and grouped SQLAlchemy query
34
34
restricted to those objects related to a given instance.
35
35
@@ -40,8 +40,8 @@ def search_relationship(session, instance, relation, filters=None, sort=None,
40
40
41
41
` `relation` is a string naming a to-many relationship of `instance`.
42
42
43
- `filters`, `sort`, and `group_by ` are identical to the corresponding
44
- arguments of :func:`.search`.
43
+ `filters`, `sort`, `group_by`, and `ignorecase ` are identical to the
44
+ corresponding arguments of :func:`.search`.
45
45
46
46
"""
47
47
model = get_model (instance )
@@ -60,11 +60,12 @@ def search_relationship(session, instance, relation, filters=None, sort=None,
60
60
query = query .filter (primary_key_value (related_model ).in_ (primary_keys ))
61
61
62
62
return search (session , related_model , filters = filters , sort = sort ,
63
- group_by = group_by , _initial_query = query )
63
+ group_by = group_by , ignorecase = ignorecase ,
64
+ _initial_query = query )
64
65
65
66
66
67
def search (session , model , filters = None , sort = None , group_by = None ,
67
- _initial_query = None ):
68
+ ignorecase = False , _initial_query = None ):
68
69
"""Returns a filtered, sorted, and grouped SQLAlchemy query.
69
70
70
71
`session` is the SQLAlchemy session in which to create the query.
@@ -80,7 +81,9 @@ def search(session, model, filters=None, sort=None, group_by=None,
80
81
`sort` is a list of pairs of the form ``(direction, fieldname)``,
81
82
where ``direction`` is either '+' or '-' and ``fieldname`` is a
82
83
string representing an attribute of the model or a dot-separated
83
- relationship path (for example, 'owner.name').
84
+ relationship path (for example, 'owner.name'). If `ignorecase` is
85
+ True, the sorting will be case-insensitive (so 'a' will precede 'B'
86
+ instead of the default behavior in which 'B' precedes 'a').
84
87
85
88
`group_by` is a list of dot-separated relationship paths on which to
86
89
group the query results.
@@ -113,11 +116,15 @@ def search(session, model, filters=None, sort=None, group_by=None,
113
116
field_name , field_name_in_relation = field_name .split ('.' )
114
117
relation_model = aliased (get_related_model (model , field_name ))
115
118
field = getattr (relation_model , field_name_in_relation )
119
+ if ignorecase :
120
+ field = field .collate ('NOCASE' )
116
121
direction = getattr (field , direction_name )
117
122
query = query .join (relation_model )
118
123
query = query .order_by (direction ())
119
124
else :
120
125
field = getattr (model , field_name )
126
+ if ignorecase :
127
+ field = field .collate ('NOCASE' )
121
128
direction = getattr (field , direction_name )
122
129
query = query .order_by (direction ())
123
130
else :
0 commit comments