Skip to content

Commit 3a6c382

Browse files
author
sreeder
committed
fix function where I generate a list of ids to check if an id list already exists and return the intersection of the lists
1 parent 1f03f05 commit 3a6c382

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def resultExists(self, result):
7878
# Annotations
7979
# ################################################################################
8080

81-
def getAnnotations(self, type=None, codes = None, ids = None):
81+
def getAnnotations(self, type=None, codes=None, ids=None):
8282
"""
8383
def getAnnotations(self, type=None, codes = None, ids = None):
8484
@@ -279,21 +279,30 @@ def getVariables(self, ids=None, codes=None, sitecode=None, results= False):
279279

280280
if sitecode:
281281
try:
282-
ids = [x[0] for x in
282+
vars = [x[0] for x in
283283
self._session.query(distinct(Results.VariableID))
284284
.filter(Results.FeatureActionID == FeatureActions.FeatureActionID)
285285
.filter(FeatureActions.SamplingFeatureID == SamplingFeatures.SamplingFeatureID)
286286
.filter(SamplingFeatures.SamplingFeatureCode == sitecode).all()
287287
]
288+
289+
if ids:
290+
ids = list(set(ids).intersection(vars))
291+
else:
292+
ids = vars
288293
except:
289-
ids = None
294+
pass
290295

291296

292297
if results:
293298
try:
294-
ids = [x[0] for x in self._session.query(distinct(Results.VariableID)).all()]
299+
vars = [x[0] for x in self._session.query(distinct(Results.VariableID)).all()]
300+
if ids:
301+
ids = list(set(ids).intersection(vars))
302+
else:
303+
ids = vars
295304
except:
296-
ids = None
305+
pass
297306

298307
query = self._session.query(Variables)
299308
if ids: query = query.filter(Variables.VariableID.in_(ids))
@@ -362,18 +371,20 @@ def getSamplingFeatures(self, ids=None, codes=None, uuids=None, type=None, wkt=N
362371
* Pass a list of SamplingFeatureUUID - returns a single sampling feature object for the given UUID's
363372
* Pass a SamplingFeatureType - returns a list of sampling feature objects of the type passed in
364373
* Pass a SamplingFeature Well Known Text - return a list of sampling feature objects
365-
* Pass whether or not you want to return the sampling features that have results associated with them
374+
* Pass whether or not you want to return only the sampling features that have results associated with them
366375
"""
367376
if results:
368377
try:
369378
fas = [x[0] for x in self._session.query(distinct(Results.FeatureActionID)).all()]
370379
except:
371380
return None
372-
373381
sf = [x[0] for x in self._session.query(distinct(FeatureActions.SamplingFeatureID))
374-
.filter(FeatureActions.FeatureActionID.in_(fas)).all()]
382+
.filter(FeatureActions.FeatureActionID.in_(fas)).all()]
383+
if ids:
384+
ids = list(set(ids).intersection(sf))
385+
else:
386+
ids = sf
375387

376-
ids = sf
377388
q = self._session.query(SamplingFeatures)
378389

379390
if type: q = q.filter_by(SamplingFeatureTypeCV=type)
@@ -391,7 +402,7 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid = None, relationshiptype=No
391402
#TODO: add functionality to filter by code
392403
"""
393404
getRelatedSamplingFeatures(self, sfid=None, rfid = None, relationshiptype=None):
394-
* Pass a SamplingFeatureID - get a list of sampling feature objects related to the input sampling feature along with the relationship type
405+
* Pass a SamplingFeatureID - get a list of sampling feature objects related to the input sampling feature
395406
* Pass a RelatedFeatureID - get a list of Sampling features objects through the related feature
396407
* Pass a RelationshipTypeCV - get a list of sampling feature objects with the given type
397408
@@ -400,19 +411,19 @@ def getRelatedSamplingFeatures(self, sfid=None, rfid = None, relationshiptype=No
400411
# q = session.query(Address).select_from(User). \
401412
# join(User.addresses). \
402413
# filter(User.name == 'ed')
403-
#throws an error when joining entire samplingfeature, works fine when just getting an element. this is being caused by the sampling feature inheritance
414+
#throws an error when joining entire samplingfeature, works fine when just getting an element. this is being
415+
# caused by the sampling feature inheritance
404416

405417
sf = self._session.query(distinct(SamplingFeatures.SamplingFeatureID))\
406-
.select_from(RelatedFeatures)
407-
418+
.select_from(RelatedFeatures)
408419

409420
if sfid: sf = sf.join(RelatedFeatures.RelatedFeatureObj).filter(RelatedFeatures.SamplingFeatureID == sfid)
410421
if rfid: sf = sf.join(RelatedFeatures.SamplingFeatureObj).filter(RelatedFeatures.RelatedFeatureID == rfid)
411422
if relationshiptype: sf = sf.filter(RelatedFeatures.RelationshipTypeCV == relationshiptype)
412423
try:
413-
sfids =[x[0] for x in sf.all()]
414-
if len(sfids)>0:
415-
sflist= self.getSamplingFeatures(ids=sfids)
424+
sfids = [x[0] for x in sf.all()]
425+
if len(sfids) > 0:
426+
sflist = self.getSamplingFeatures(ids=sfids)
416427
return sflist
417428

418429
except Exception as e:
@@ -577,7 +588,6 @@ def getResults(self, ids=None, type=None, uuids=None, actionid=None, simulationi
577588

578589
query = self._session.query(Results)
579590

580-
581591
if type: query = query.filter_by(ResultTypeCV=type)
582592
if variableid: query = query.filter_by(VariableID=variableid)
583593
if ids: query = query.filter(Results.ResultID.in_(ids))
@@ -624,7 +634,7 @@ def getDataSets(self, codes=None, uuids=None):
624634
try:
625635
return q.all()
626636
except Exception as e:
627-
print("Error running Query %s"%e)
637+
print("Error running Query %s" % e)
628638
return None
629639

630640
# ################################################################################

0 commit comments

Comments
 (0)