From ee6b859a96af8f1bd4e69b7ac98f81fab27f739c Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Tue, 12 Apr 2016 16:58:06 -0600 Subject: [PATCH 1/2] add lists to all id and code fields --- odm2api/ODM2/services/createService.py | 62 +------ odm2api/ODM2/services/readService.py | 246 +++++++++++-------------- 2 files changed, 121 insertions(+), 187 deletions(-) diff --git a/odm2api/ODM2/services/createService.py b/odm2api/ODM2/services/createService.py index 166e190..255191d 100644 --- a/odm2api/ODM2/services/createService.py +++ b/odm2api/ODM2/services/createService.py @@ -17,6 +17,13 @@ def __init__(self, session): # Annotations # ################################################################################ + def create(self, values): + if len(values)>1: + self._session.add_all(values) + else: + self._session.add(values) + self._session.commit() + def createVariable(self, var): self._session.add(var) self._session.commit() @@ -92,7 +99,8 @@ def createSimulation(self, simulation): def createTimeSeriesResultValues(self, datavalues): try: - + tablename = TimeSeriesResultValues.__tablename__ + print "I am TS saving name the table name"+ tablename datavalues.to_sql(name="TimeSeriesResultValues", schema=TimeSeriesResultValues.__table_args__['schema'], if_exists='append', @@ -106,10 +114,6 @@ def createTimeSeriesResultValues(self, datavalues): print e return None -# ################################################################################ -# CV -# ################################################################################ - @@ -448,53 +452,7 @@ def createTimeSeriesResultValues(self, datavalues): # # return result # -# -# # ################################################################################ -# # Data Quality -# # ################################################################################ -# -# -# -# # ################################################################################ -# # Equipment -# # ################################################################################ -# -# -# -# -# # ################################################################################ -# # ExtensionProperties -# # ################################################################################ -# -# -# -# -# -# # ################################################################################ -# # External Identifiers -# # ################################################################################ -# -# -# -# -# # ################################################################################ -# # Lab Analyses -# # ################################################################################ -# -# -# -# -# # ################################################################################ -# # Provenance -# # ################################################################################ -# -# -# -# -# # ################################################################################ -# # Results -# # ################################################################################ -# + # # # def createTimeSeriesResult(self, result, aggregationstatistic, xloc=None, xloc_unitid=None, yloc=None, diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index daf6f40..9a54032 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -200,16 +200,17 @@ def getTaxonomicClassifiers(self): Variable """ - def getVariables(self, id=None, code=None): + def getVariables(self, ids=None, code=None): """ getVariables() * Pass nothing - returns full list of variable objects - * Pass a VariableID - returns a single variable object - * Pass a VariableCode - returns a single variable object + * Pass a list of VariableID - returns a single variable object + * Pass a list of VariableCode - returns a single variable object """ + query = self._session.query(Variables) - if id: query = query.filter_by(VariableID=id) - if code: query = query.filter_by(VariableCode=code) + if ids: query = query.filter(Variables.VariableID.in_(ids)) + if code: query = query.filter(Variables.VariableCode.in_(code)) try: return query.all() except: @@ -219,10 +220,18 @@ def getVariables(self, id=None, code=None): Method """ - def getMethods(self, id=None, code=None, type=None): + def getMethods(self, ids=None, code=None, type=None): + + """ + getMethods() + * Pass nothing - returns full list of method objects + * Pass a list of MethodID - returns a single method object + * Pass a list of MethodCode - returns a single method object + * Pass a MethodType - returns a list of method objects + """ q = self._session.query(Methods) - if id: q = q.filter_by(MethodID=id) - if code: q = q.filter_by(MethodCode=str(code)) + if ids: q = q.filter(Methods.MethodID.in_(ids)) + if code: q = q.filter(Methods.MethodCode.in_(code)) if type: q = q.filter_by(MethodTypeCV=type) try: @@ -234,11 +243,16 @@ def getMethods(self, id=None, code=None, type=None): ProcessingLevel """ - def getProcessingLevels(self, id=None, code=None): - + def getProcessingLevels(self, ids=None, code=None): + """ + getProcessingLevels() + * Pass nothing - returns full list of ProcessingLevel objects + * Pass a list of ProcessingLevelID - returns a single processingLevel object + * Pass a list of ProcessingLevelCode - returns a single processingLevel object + """ q = self._session.query(ProcessingLevels) - if id: q = q.filter_by(ProcessingLevelID=id) - if code: q = q.filter_by(ProcessingLevelCode=str(code)) + if ids: q = q.filter(ProcessingLevels.ProcessingLevels.in_(ids)) + if code: q = q.filter(ProcessingLevels.ProcessingLevelCode.in_(code)) try: return q.all() @@ -249,22 +263,21 @@ def getProcessingLevels(self, id=None, code=None): Sampling Feature """ - def getSamplingFeatures(self, id=None, code=None, uuid=None, type=None, wkt=None): + def getSamplingFeatures(self, ids=None, code=None, uuid=None, type=None, wkt=None): """ + getSamplingFeatures * Pass nothing - returns a list of all sampling feature objects with each object of type specific to that sampling feature - * Pass a SamplingFeatureID - returns a single sampling feature object - * Pass a SamplingFeatureCode - returns a single sampling feature object + * Pass a list of SamplingFeatureID - returns a single sampling feature object + * Pass a list of SamplingFeatureCode - returns a single sampling feature object * Pass a SamplingFeatureType - returns a list of sampling feature objects of the type passed in * Pass a SamplingFeatureGeometry(TYPE????) - return a list of sampling feature objects """ - - q = self._session.query(SamplingFeatures) if type: q = q.filter_by(SamplingFeatureTypeCV=type) - if id: q = q.filter_by(SamplingFeatureID=id) - if code: q = q.filter_by(SamplingFeatureCode=code) + if ids: q = q.filter(SamplingFeatures.SamplingFeatureID.in_(ids)) + if code: q = q.filter(SamplingFeatures.SamplingFeatureCode.in_(code)) if wkt: q = q.filter_by(FeatureGeometryWKT=wkt) try: return q.all() @@ -287,7 +300,15 @@ def getRelatedSamplingFeatures(self, id): Action """ - def getActions(self, id=None, type=None, sfid=None): + def getActions(self, ids=None, type=None, sfid=None): + + """ + getSamplingFeatures + * Pass nothing - returns a list of all Actions + * Pass a SamplingFeatureID - returns a single Action object + * Pass a list of ActionIDs - returns a single Action object + * Pass a ActionType - returns a list of Action objects of the type passed in + """ a = Actions if type == "equipment": a = EquipmentActions @@ -296,16 +317,11 @@ def getActions(self, id=None, type=None, sfid=None): elif type == "maintenance": a = MaintenanceActions - q = self._session.query(Actions) - if id: q = q.filter_by(ActionID=id) - # if type: q=q.filter_by(Actions.ActionTypeCV.ilike(type)) + q = self._session.query(a) + if ids: q = q.filter(a.ActionID.in_(ids)) if sfid: q = q.join(FeatureActions).filter(FeatureActions.SamplingFeatureID == sfid) - # return self._session.query(Results) \ - # .join(FeatureActions) \ - # .join(Actions) \ - # .join(Simulations) \ - # .filter(Simulations.SimulationID == simulationid).all() + try: return q.all() except: @@ -327,16 +343,16 @@ def getRelatedActions(self, actionid=None): Unit """ - def getUnits(self, id=None, name=None, type=None): + def getUnits(self, ids=None, name=None, type=None): """ getUnits() - * Pass nothing - returns a list of all units objects - * Pass UnitsID - returns a single units object - * Pass UnitsName - returns a single units object + * Pass nothing - returns a list of all units objects + * Pass a list of UnitsID - returns a single units object + * Pass UnitsName - returns a single units object """ q = self._session.query(Units) - if id: q = q.filter_by(UnitsID=id) + if ids: q = q.filter(Units.UnitsID.in_(ids)) if name: q = q.filter(Units.UnitsName.ilike(name)) if type: q = q.filter(Units.UnitsTypeCV.ilike(type)) try: @@ -348,15 +364,16 @@ def getUnits(self, id=None, name=None, type=None): Organization """ - def getOrganizations(self, id=None, code=None): - """Select all on Organization - - :return Organization Objects: - :type list: + def getOrganizations(self, ids=None, code=None): + """ + getOrganizations() + * Pass nothing - returns a list of all organization objects + * Pass a list of OrganizationID - returns a single organization object + * Pass a list of OrganizationCode - returns a single organization object """ q = self._session.query(Organizations) - if id: q = q.filter_by(OrganizationID=id) - if code: q = q.filter_by(OrganizationCode=code) + if ids: q = q.filter(Organizations.OrganizationID.in_(ids)) + if code: q = q.filter(Organizations.OrganizationCode.in_(code)) try: return q.all() except: @@ -366,14 +383,16 @@ def getOrganizations(self, id=None, code=None): Person """ - def getPeople(self, id=None, firstname=None, lastname=None): - """Select all on Person - - :return Person Objects: - :type list: + def getPeople(self, ids=None, firstname=None, lastname=None): + """ + getPeople() + * Pass nothing - returns a list of all People objects + * Pass a list of PeopleID - returns a single People object + * Pass a First Name - returns a single People object + * Pass a Last Name - returns a single People object """ q = self._session.query(People) - if id: q = q.filter_by(PersonID=id) + if ids: q = q.filter(People.PersonID.in_(ids)) if firstname: q = q.filter(People.PersonFirstName.ilike(firstname)) if lastname: q = q.filter(People.PersonLastName.ilike(lastname)) try: @@ -381,16 +400,17 @@ def getPeople(self, id=None, firstname=None, lastname=None): except: return None - def getAffiliations(self, id=None, personfirst=None, personlast=None, orgcode=None): + def getAffiliations(self, ids=None, personfirst=None, personlast=None, orgcode=None): """ - Select all affiliation of person - :param personfirst: first name of person - :param personlast: last name of person - :param orgcode: organization code (e.g. uwrl) - :return: ODM2.Affiliation + getAffiliations() + * Pass nothing - returns a list of all Affiliation objects + * Pass a list of AffiliationID - returns a single Affiliation object + * Pass a First Name - returns a single Affiliation object + * Pass a Last Name - returns a single Affiliation object + * Pass an OrganizationCode - returns a Affiliation object """ q = self._session.query(Affiliations) - if id: q = q.filter(AffiliationID=id) + if ids: q = q.filter(Affiliations.AffiliationID.in_(ids)) if orgcode: q = q.filter(Organizations.OrganizationCode.ilike(orgcode)) if personfirst: q = q.filter(People.PersonFirstName.ilike(personfirst)) if personlast: q = q.filter(People.PersonLastName.ilike(personlast)).first() @@ -403,24 +423,21 @@ def getAffiliations(self, id=None, personfirst=None, personlast=None, orgcode=No Results """ - def getResults(self, id=None, actionid=None, type=None): + def getResults(self, ids=None, actionid=None): # TODO what if user sends in both type and actionid vs just actionid - """Select by variableId - - :param id: - :type Integer: - :return Return matching Variable object filtered by variableId: - :type Variable: - """ - + """ + getResults() + * Pass nothing - returns a list of all Results objects + * Pass a list of ResultID - returns a single Results object + * Pass an ActionID - returns a single Results object + """ query = self._session.query(Results) - if type: query = query.filter_by(ResultTypeCV=type) + if actionid: query = query.join(FeatureActions).filter_by(ActionID=actionid) - if id: query = query.filter_by(ResultID=id) + if ids: query = query.filter(Results.ResultID.in_(ids)) - # if type: query=query.filter_by(ResultTypeCV=type) try: return query.all() except: @@ -458,9 +475,15 @@ def getResults(self, id=None, actionid=None, type=None): """ def getDataSets(self, code=None): + """ + getDataSets() + * Pass nothing - returns a list of all DataSet objects + * Pass a list of DataSetCode - returns a single DataSet object for each code + + """ q = self._session.query(DataSets) if code: - q = q.filter(DataSets.DataSetCode.ilike(code)) + q = q.filter(DataSets.DataSetCode.in_(code)) try: return q.all() except: @@ -495,9 +518,15 @@ def getResultsDataQuality(self): # Equipment # ################################################################################ - # TODO Equipment Schema Queries def getEquipment(self, code=None, type=None, sfid=None, actionid=None): + """ + getEquipment() + * Pass nothing - returns a list of all Equipment objects + * Pass a EquipmentType - returns a single Equipment object + * Pass a SamplingFeatureID - returns a single Equipment object + * Pass an ActionID - returns a single Equipment object + """ e = self._session.query(Equipment) if sfid: e = e.join(EquipmentUsed) \ .join(Actions) \ @@ -639,7 +668,6 @@ def getResultDerivationEquations(self): # Results # ################################################################################ - """ ResultValues """ @@ -650,11 +678,6 @@ def getResultValues(self, resultid, starttime=None, endtime=None): getResultValues() * Pass a ResultID - Returns a result values object of type that is specific to the result type * Pass a ResultID and a date range - returns a result values object of type that is specific to the result type with values between the input date range - NOTE: Another option here would be to put a flag on getResults that specifies whether values should be returned - :return TimeSeriesResultsValue Objects: - :type list: - - """ type= self._session.query(Results).filter_by(ResultID=resultid).first().ResultTypeCV Result = TimeSeriesResults @@ -668,7 +691,6 @@ def getResultValues(self, resultid, starttime=None, endtime=None): elif "trajectory" in type.lower():Result = TrajectoryResultValues elif "transect" in type.lower():Result = TransectResultValues - q = self._session.query(Result).filter_by(ResultID=resultid) if starttime: q = q.filter(Result.ValueDateTime >= starttime) if endtime: q = q.filter(Result.ValueDateTime <= endtime) @@ -687,53 +709,19 @@ def getResultValues(self, resultid, starttime=None, endtime=None): """ Site """ - # ToDo function for get sampling features - def getSites(self): - """Select all on Sites - :return Site Objects: - :type list: + def getSpatialReference(self, srsCode=None): """ - return self._session.query(Sites).all() - - # def getSiteBySFId(self, siteId): - # """Select by siteId - # - # :param siteId: - # :type Integer: - # :return Return matching Site Object filtered by siteId: - # :type Site: - # """ - # try: - # return self._session.query(Sites).filter_by(SamplingFeatureID=siteId).one() - # except: - # return None - # - # def getSiteBySFCode(self, siteCode): - # """Select by siteCode - # - # :param siteCode: - # :type String: - # :return Return matching Samplingfeature Object filtered by siteCode: - # :type Samplingfeature: - # """ - # - # sf = self._session.query(SamplingFeatures).filter_by(SamplingFeatureCode=siteCode).one() - # return self._session.query(Sites).filter_by(SamplingFeatureID=sf.SamplingFeatureID).one() - - # def getSpatialReferenceByCode(self, srsCode): - # - # try: - # return self._session.query(SpatialReferences).filter(SpatialReferences.SRSCode.ilike(srsCode)).first() - # except: - # return None - - - # ################################################################################ - # Equipment - # ################################################################################ - - + getSpatialReference() + * Pass a ResultID - Returns a result values object of type that is specific to the result type + * Pass a ResultID and a date range - returns a result values object of type that is specific to the result type with values between the input date range + """ + q = self._session.query(SpatialReferences) + if srsCode: q.filter(SpatialReferences.SRSCode.ilike(srsCode)) + try: + return q.first() + except: + return None # ################################################################################ @@ -743,15 +731,10 @@ def getSites(self): def getSimulations(self, name=None, actionid=None): """ getSimulations() -* Pass nothing - get a list of all model simuation objects -* Pass a SimulationName - get a single simulation object -* Pass an ActionID - get a single simulation object - :param name: - :type name: - :param actionid: - :type actionid: - :return: - :rtype: + * Pass nothing - get a list of all model simuation objects + * Pass a SimulationName - get a single simulation object + * Pass an ActionID - get a single simulation object + """ s = self._session.query(Simulations) if name: s = s.filter(Simulations.SimulationName.ilike(name)) @@ -805,10 +788,3 @@ def getRelatedModels(self, id=None, code=None): return None -# ################################################################################ -# ODM2 -# ################################################################################ - -class readODM2(object): - def test(self): - return None From d476fe871fc8de179d012cfa59ffd9e3dcb884b8 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Tue, 12 Apr 2016 17:09:50 -0600 Subject: [PATCH 2/2] rename singular code to plural codes --- Examples/Sample.py | 10 ++++---- odm2api/ODM2/services/readService.py | 37 ++++++++++++++-------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Examples/Sample.py b/Examples/Sample.py index 38c3592..7734f94 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -33,7 +33,7 @@ # ------------------------------ # Get all of the variables from the database and print their names to the console allVars = read.getVariables() - +print "\n-------- Information about Variables ---------" for x in allVars: print x.VariableCode + ": " + x.VariableNameCV @@ -41,7 +41,7 @@ # Get all of the people from the database allPeople = read.getPeople() - +print "\n-------- Information about People ---------" for x in allPeople: print x.PersonFirstName + " " + x.PersonLastName @@ -55,6 +55,7 @@ # Get all of the SamplingFeatures from the database that are Sites try: + print "\n-------- Information about Sites ---------" siteFeatures = read.getSamplingFeatures(type='Site') numSites = len(siteFeatures) @@ -66,8 +67,7 @@ # Now get the SamplingFeature object for a SamplingFeature code try: - sf = read.getSamplingFeatures(code='USU-LBR-Mendon')[0] - print sf + sf = read.getSamplingFeatures(code=['USU-LBR-Mendon'])[0] print "\n-------- Information about an individual SamplingFeature ---------" print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeature(code = x): \n" print "SamplingFeatureCode: " + sf.SamplingFeatureCode @@ -118,7 +118,7 @@ # Now get a particular Result using a ResultID print "\n------- Example of Retrieving Attributes of a Time Series Result -------" try: - tsResult = read.getResults(id = 1)[0] + tsResult = read.getResults(ids = [1])[0] print ( "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" + "ResultTypeCV: " + tsResult.ResultTypeCV + "\n" + diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 9a54032..dddb009 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -200,7 +200,7 @@ def getTaxonomicClassifiers(self): Variable """ - def getVariables(self, ids=None, code=None): + def getVariables(self, ids=None, codes=None): """ getVariables() * Pass nothing - returns full list of variable objects @@ -210,7 +210,7 @@ def getVariables(self, ids=None, code=None): query = self._session.query(Variables) if ids: query = query.filter(Variables.VariableID.in_(ids)) - if code: query = query.filter(Variables.VariableCode.in_(code)) + if codes: query = query.filter(Variables.VariableCode.in_(codes)) try: return query.all() except: @@ -220,7 +220,7 @@ def getVariables(self, ids=None, code=None): Method """ - def getMethods(self, ids=None, code=None, type=None): + def getMethods(self, ids=None, codes=None, type=None): """ getMethods() @@ -231,7 +231,7 @@ def getMethods(self, ids=None, code=None, type=None): """ q = self._session.query(Methods) if ids: q = q.filter(Methods.MethodID.in_(ids)) - if code: q = q.filter(Methods.MethodCode.in_(code)) + if codes: q = q.filter(Methods.MethodCode.in_(codes)) if type: q = q.filter_by(MethodTypeCV=type) try: @@ -243,7 +243,7 @@ def getMethods(self, ids=None, code=None, type=None): ProcessingLevel """ - def getProcessingLevels(self, ids=None, code=None): + def getProcessingLevels(self, ids=None, codes=None): """ getProcessingLevels() * Pass nothing - returns full list of ProcessingLevel objects @@ -252,7 +252,7 @@ def getProcessingLevels(self, ids=None, code=None): """ q = self._session.query(ProcessingLevels) if ids: q = q.filter(ProcessingLevels.ProcessingLevels.in_(ids)) - if code: q = q.filter(ProcessingLevels.ProcessingLevelCode.in_(code)) + if codes: q = q.filter(ProcessingLevels.ProcessingLevelCode.in_(codes)) try: return q.all() @@ -263,7 +263,7 @@ def getProcessingLevels(self, ids=None, code=None): Sampling Feature """ - def getSamplingFeatures(self, ids=None, code=None, uuid=None, type=None, wkt=None): + def getSamplingFeatures(self, ids=None, codes=None, uuid=None, type=None, wkt=None): """ getSamplingFeatures * Pass nothing - returns a list of all sampling feature objects with each object of type specific to that sampling feature @@ -277,7 +277,7 @@ def getSamplingFeatures(self, ids=None, code=None, uuid=None, type=None, wkt=Non if type: q = q.filter_by(SamplingFeatureTypeCV=type) if ids: q = q.filter(SamplingFeatures.SamplingFeatureID.in_(ids)) - if code: q = q.filter(SamplingFeatures.SamplingFeatureCode.in_(code)) + if codes: q = q.filter(SamplingFeatures.SamplingFeatureCode.in_(codes)) if wkt: q = q.filter_by(FeatureGeometryWKT=wkt) try: return q.all() @@ -364,7 +364,7 @@ def getUnits(self, ids=None, name=None, type=None): Organization """ - def getOrganizations(self, ids=None, code=None): + def getOrganizations(self, ids=None, codes=None): """ getOrganizations() * Pass nothing - returns a list of all organization objects @@ -373,7 +373,7 @@ def getOrganizations(self, ids=None, code=None): """ q = self._session.query(Organizations) if ids: q = q.filter(Organizations.OrganizationID.in_(ids)) - if code: q = q.filter(Organizations.OrganizationCode.in_(code)) + if codes: q = q.filter(Organizations.OrganizationCode.in_(codes)) try: return q.all() except: @@ -474,7 +474,7 @@ def getResults(self, ids=None, actionid=None): Datasets """ - def getDataSets(self, code=None): + def getDataSets(self, codes=None): """ getDataSets() * Pass nothing - returns a list of all DataSet objects @@ -482,8 +482,8 @@ def getDataSets(self, code=None): """ q = self._session.query(DataSets) - if code: - q = q.filter(DataSets.DataSetCode.in_(code)) + if codes: + q = q.filter(DataSets.DataSetCode.in_(codes)) try: return q.all() except: @@ -519,7 +519,7 @@ def getResultsDataQuality(self): # ################################################################################ # TODO Equipment Schema Queries - def getEquipment(self, code=None, type=None, sfid=None, actionid=None): + def getEquipment(self, codes=None, type=None, sfid=None, actionid=None): """ getEquipment() * Pass nothing - returns a list of all Equipment objects @@ -532,6 +532,7 @@ def getEquipment(self, code=None, type=None, sfid=None, actionid=None): .join(Actions) \ .join(FeatureActions) \ .filter(FeatureActions.SamplingFeatureID == sfid) + if codes: e = e.filter(Equipment.EquipmentCode.in_(codes)) if actionid: e = e.join(EquipmentUsed).join(Actions) \ .filter(Actions.ActionID == actionid) return e.all() @@ -710,14 +711,14 @@ def getResultValues(self, resultid, starttime=None, endtime=None): Site """ - def getSpatialReference(self, srsCode=None): + def getSpatialReference(self, srsCodes=None): """ getSpatialReference() * Pass a ResultID - Returns a result values object of type that is specific to the result type * Pass a ResultID and a date range - returns a result values object of type that is specific to the result type with values between the input date range """ q = self._session.query(SpatialReferences) - if srsCode: q.filter(SpatialReferences.SRSCode.ilike(srsCode)) + if srsCodes: q.filter(SpatialReferences.SRSCode.in_(srsCodes)) try: return q.first() except: @@ -756,9 +757,9 @@ def getSimulations(self, name=None, actionid=None): # print e # return None - def getModels(self, code=None): + def getModels(self, codes=None): m = self._session.query(Models) - if code: m = m.filter(Models.ModelCode.ilike(code)) + if codes: m = m.filter(Models.ModelCode.in_(codes)) try: return m.all() except: