1
1
import logging
2
2
3
- from sqlalchemy import or_ , String , Date , and_
3
+ from sqlalchemy import or_ , String , Date , and_ , func
4
4
from sqlalchemy .inspection import inspect
5
5
from sqlalchemy .orm import joinedload , contains_eager , aliased
6
6
from sqlalchemy .orm .exc import NoResultFound
32
32
33
33
34
34
def cruved_ds_filter (model , role , scope ):
35
+ # TODO check if not used elsewhere (not found in major module of Geonature)
35
36
if scope not in (1 , 2 , 3 ):
36
37
raise Unauthorized ("Not a valid cruved value" )
37
38
elif scope == 3 :
38
39
return True
39
40
elif scope in (1 , 2 ):
40
- sub_q = DB .select (TDatasets ).join (
41
- CorDatasetActor , TDatasets .id_dataset == CorDatasetActor .id_dataset
41
+ sub_q = (
42
+ DB .select (func .count ("*" ))
43
+ .select_from (TDatasets )
44
+ .join (CorDatasetActor , TDatasets .id_dataset == CorDatasetActor .id_dataset )
42
45
)
43
46
44
47
or_filter = [
@@ -49,10 +52,8 @@ def cruved_ds_filter(model, role, scope):
49
52
# if organism is None => do not filter on id_organism even if level = 2
50
53
if scope == 2 and role .id_organisme is not None :
51
54
or_filter .append (CorDatasetActor .id_organism == role .id_organisme )
52
- sub_q = sub_q .filter (and_ (or_ (* or_filter ), model .id_dataset == TDatasets .id_dataset ))
53
- return sub_q .exists ()
54
-
55
- return True
55
+ sub_q = sub_q .where (and_ (or_ (* or_filter ), model .id_dataset == TDatasets .id_dataset ))
56
+ return DB .session .execute (sub_q ).scalar_one () > 0
56
57
57
58
58
59
def cruved_af_filter (model , role , scope ):
@@ -61,10 +62,14 @@ def cruved_af_filter(model, role, scope):
61
62
elif scope == 3 :
62
63
return True
63
64
elif scope in (1 , 2 ):
64
- sub_q = DB .select (TAcquisitionFramework ).join (
65
- CorAcquisitionFrameworkActor ,
66
- TAcquisitionFramework .id_acquisition_framework
67
- == CorAcquisitionFrameworkActor .id_acquisition_framework ,
65
+ sub_q = (
66
+ DB .select (func .count ("*" ))
67
+ .select_from (TAcquisitionFramework )
68
+ .join (
69
+ CorAcquisitionFrameworkActor ,
70
+ TAcquisitionFramework .id_acquisition_framework
71
+ == CorAcquisitionFrameworkActor .id_acquisition_framework ,
72
+ )
68
73
)
69
74
70
75
or_filter = [
@@ -81,33 +86,23 @@ def cruved_af_filter(model, role, scope):
81
86
model .id_acquisition_framework == TAcquisitionFramework .id_acquisition_framework ,
82
87
)
83
88
)
84
- return sub_q . exists ()
89
+ return DB . session . execute ( sub_q ). scalar_one () > 0
85
90
86
91
87
92
def get_metadata_list (role , scope , args , exclude_cols ):
88
- num = args .get ("num" )
89
- uuid = args .get ("uuid" )
90
- name = args .get ("name" )
91
- date = args .get ("date" )
92
- organisme = args .get ("organism" )
93
- person = args .get ("person" )
93
+ id_acquisition_framework = args .get ("num" )
94
+ unique_acquisition_framework_id = args .get ("uuid" )
95
+ acquisition_framework_name = args .get ("name" )
96
+ meta_create_date = args .get ("date" )
97
+ id_organism = args .get ("organism" )
98
+ id_role = args .get ("person" )
94
99
selector = args .get ("selector" )
95
100
is_parent = args .get ("is_parent" )
101
+ order_by = args .get ("orderby" , None )
96
102
97
- # @TODO : replace by select
98
- query = DB .session .query (TAcquisitionFramework )
99
-
100
- if is_parent is not None :
101
- query = query .where (TAcquisitionFramework .is_parent )
102
-
103
- if selector == "af" and set (["organism" , "person" ]).intersection (args ):
104
- query = query .join (
105
- CorAcquisitionFrameworkActor ,
106
- TAcquisitionFramework .id_acquisition_framework
107
- == CorAcquisitionFrameworkActor .id_acquisition_framework ,
108
- )
109
- # remove cor_af_actor from joined load because already joined
110
- exclude_cols .append ("cor_af_actor" )
103
+ query = DB .select (TAcquisitionFramework ).where_if (
104
+ is_parent is not None , TAcquisitionFramework .is_parent
105
+ )
111
106
112
107
if selector == "ds" :
113
108
query = query .join (
@@ -132,44 +127,74 @@ def get_metadata_list(role, scope, args, exclude_cols):
132
127
cruved_ds_filter (TDatasets , role , scope ),
133
128
)
134
129
)
135
- if args .get ("selector" ) == "af" :
130
+ if selector == "af" :
131
+ if set (["organism" , "person" ]).intersection (args ):
132
+ query = query .join (
133
+ CorAcquisitionFrameworkActor ,
134
+ TAcquisitionFramework .id_acquisition_framework
135
+ == CorAcquisitionFrameworkActor .id_acquisition_framework ,
136
+ )
137
+ # remove cor_af_actor from joined load because already joined
138
+ exclude_cols .append ("cor_af_actor" )
136
139
query = (
137
- query .where (TAcquisitionFramework .id_acquisition_framework == num if num else True )
140
+ query .where (
141
+ TAcquisitionFramework .id_acquisition_framework == id_acquisition_framework
142
+ if id_acquisition_framework
143
+ else True
144
+ )
138
145
.where (
139
146
cast (TAcquisitionFramework .unique_acquisition_framework_id , String ).ilike (
140
- f"%{ uuid .strip ()} %"
147
+ f"%{ unique_acquisition_framework_id .strip ()} %"
141
148
)
142
- if uuid
149
+ if unique_acquisition_framework_id
143
150
else True
144
151
)
145
152
.where (
146
- TAcquisitionFramework .acquisition_framework_name .ilike (f"%{ name } %" )
147
- if name
153
+ TAcquisitionFramework .acquisition_framework_name .ilike (
154
+ f"%{ acquisition_framework_name } %"
155
+ )
156
+ if acquisition_framework_name
148
157
else True
149
158
)
150
- .where (CorAcquisitionFrameworkActor .id_organism == organisme if organisme else True )
151
- .where (CorAcquisitionFrameworkActor .id_role == person if person else True )
159
+ .where (
160
+ CorAcquisitionFrameworkActor .id_organism == id_organism if id_organism else True
161
+ )
162
+ .where (CorAcquisitionFrameworkActor .id_role == id_role if id_role else True )
152
163
)
153
164
154
- elif args . get ( " selector" ) == "ds" :
165
+ elif selector == "ds" :
155
166
query = (
156
- query .where (TDatasets .id_dataset == num if num else True )
167
+ query .where (
168
+ TDatasets .id_dataset == id_acquisition_framework
169
+ if id_acquisition_framework
170
+ else True
171
+ )
172
+ .where (
173
+ cast (TDatasets .unique_dataset_id , String ).ilike (
174
+ f"%{ unique_acquisition_framework_id .strip ()} %"
175
+ )
176
+ if unique_acquisition_framework_id
177
+ else True
178
+ )
179
+ .where (
180
+ TAcquisitionFramework .datasets .any (dataset_name = acquisition_framework_name )
181
+ if acquisition_framework_name
182
+ else True
183
+ )
157
184
.where (
158
- cast (TDatasets .unique_dataset_id , String ). ilike ( f"% { uuid . strip () } %" )
159
- if uuid
185
+ cast (TDatasets .meta_create_date , Date ) == meta_create_date
186
+ if meta_create_date
160
187
else True
161
188
)
162
- .where (TAcquisitionFramework .datasets .any (dataset_name = name ) if name else True )
163
- .where (cast (TDatasets .meta_create_date , Date ) == date if date else True )
164
- .where (CorDatasetActor .id_organism == organisme if organisme else True )
165
- .where (CorDatasetActor .id_role == person if person else True )
189
+ .where (CorDatasetActor .id_organism == id_organism if id_organism else True )
190
+ .where (CorDatasetActor .id_role == id_role if id_role else True )
166
191
)
167
192
168
- if args . get ( "orderby" , None ) :
193
+ if order_by :
169
194
try :
170
- query = query .order_by (getattr (TAcquisitionFramework , args . get ( "orderby" ) ).asc ())
195
+ query = query .order_by (getattr (TAcquisitionFramework , order_by ).asc ())
171
196
except :
172
- query = query .order_by (getattr (TDatasets , args . get ( "orderby" ) ).asc ())
197
+ query = query .order_by (getattr (TDatasets , order_by ).asc ())
173
198
finally :
174
199
pass
175
200
return query
0 commit comments