Skip to content

Commit

Permalink
#293 Finishing up the get and create functions in series_service
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Arrieta committed Sep 21, 2016
1 parent 324ae0e commit cc7709d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 118 deletions.
64 changes: 20 additions & 44 deletions odmtools/odmservices/cv_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def __init__(self, connection_string="", debug=False):
self.read_service = ReadODM2(session_factory=self._session_factory, debug=self._debug)

# Controlled Vocabulary get methods
# Returns a list of all terms in the Controller Vocabulary (CV)

# From ODM1 -> ODM2 Qualifier was changed to Annotations
def get_annotations(self, type):
Expand All @@ -48,14 +47,21 @@ def get_general_category_cvs(self):
return result

def get_offset_type_cvs(self):
# result = self._edit_session.query(OffsetType).order_by(OffsetType.id).all()
# return result
return self.read_service.getCVs(type="Spatial Offset Type")

# From ODM1 -> ODM2 Quality Controlled Level was changed to Processing Level
def get_all_processing_levels(self):
self.read_service.getProcessingLevels()

def get_all_method(self): # Rename to get_method_all
return self.read_service.getMethods(ids=None, codes=None, type=None)

def get_method_by_id(self, method_id):
return self.read_service.getMethods(ids=method_id)

def get_method_by_description(self, code):
return self.read_service.getMethods(codes=code)

def get_processing_level_by_id(self, id):
self.read_service.getProcessingLevels(ids=id)

Expand All @@ -67,27 +73,29 @@ def get_samples(self):
return result

def get_sample_medium_cvs(self):
# result = self._edit_session.query(SampleMediumCV).order_by(SampleMediumCV.term).all()
# return result
return self.read_service.getCVs(type="Medium")

def get_site_type_cvs(self):
# result = self._edit_session.query(SiteTypeCV).order_by(SiteTypeCV.term).all()
# return result
return self.read_service.getCVs(type="Site Type")

def get_speciation_cvs(self):
# result = self._edit_session.query(SpeciationCV).order_by(SpeciationCV.term).all()
# return result
return self.read_service.getCVs(type="Speciation")

def get_sample_type_cvs(self):
result = self._edit_session.query(SampleTypeCV).order_by(SampleTypeCV.term).all()
return result

# From ODM1 -> ODM2 Site was changed to Sampling Feature
def get_all_sites(self):
return self.read_service.getSamplingFeatures(ids=None, codes=None, uuids=None, type=None, wkt=None)

def get_site_by_id(self, site_id):
return self.read_service.getSamplingFeatures(ids=site_id)

def get_timeseries_result_values(self, type):
return self.read_service.getAnnotations(type=type)

def get_units(self):
# result = self._edit_session.query(Unit).all()
# return result
self.read_service.getUnits(ids=None, name=None, type=None)

def get_units_not_uni(self):
Expand All @@ -100,27 +108,19 @@ def get_units_names(self):

# return a single cv
def get_unit_by_name(self, unit_name):
# result = self._edit_session.query(Unit).filter_by(name=unit_name).first()
# return result
return self.read_service.getUnits(name=unit_name)

def get_unit_by_id(self, unit_id):
# result = self._edit_session.query(Unit).filter_by(id=unit_id).first()
# return result
return self.read_service.getUnits(ids=unit_id)

def get_value_type_cvs(self):
result = self._edit_session.query(ValueTypeCV).order_by(ValueTypeCV.term).all()
return result

def get_variable_name_cvs(self):
# result = self._edit_session.query(VariableNameCV).order_by(VariableNameCV.term).all()
# return result
return self.read_service.getCVs(type="Variable Name")

def get_vertical_datum_cvs(self):
# result = self._edit_session.query(VerticalDatumCV).order_by(VerticalDatumCV.term).all()
# return result
return self.read_service.getCVs("Elevation Datum")

def get_all_variables(self):
Expand All @@ -132,31 +132,7 @@ def get_variable_by_id(self, id):
def get_variable_by_code(self, code):
return self.read_service.getVariables(codes=code)

# def get_all_qualifiers(self):
# """
#
# :return: List[Qualifiers]
# """
# result = self._edit_session.query(Qualifier).order_by(Qualifier.code).all()
# return result
#
# def get_qualifier_by_code(self, code):
# """
#
# :return: Qualifiers
# """
# result = self._edit_session.query(Qualifier).filter(Qualifier.code==code).first()
# return result
#
# def get_qualifiers_by_series_id(self, series_id):
# """
#
# :param series_id:
# :return:
# """
# subquery = self._edit_session.query(DataValue.qualifier_id).outerjoin(
# Series.data_values).filter(Series.id == series_id, DataValue.qualifier_id != None).distinct().subquery()
# return self._edit_session.query(Qualifier).join(subquery).distinct().all()




107 changes: 33 additions & 74 deletions odmtools/odmservices/series_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __init__(self, connection_string="", debug=False):
self.create_service = CreateODM2(session_factory=self._session_factory, debug=self._debug)

def reset_session(self):
self._edit_session = self._session_factory.getSession() # Reset the session in order to prevent memory leaks
# self._edit_session = self._session_factory.getSession() # Reset the session in order to prevent memory leaks
self._edit_session = self.create_service.getSession()

def get_db_version(self):
return self._edit_session.query(ODMVersion).first().version_number
Expand All @@ -40,15 +41,6 @@ def get_db_version(self):
#
#####################

# Site methods
def get_all_sites(self):
"""
:return: List[Sites]
"""
return self._edit_session.query(Site).order_by(Site.code).all()


def get_used_sites(self):
"""
Return a list of all sites that are being referenced in the Series Catalog Table
Expand All @@ -68,18 +60,6 @@ def get_used_sites(self):

return Sites


def get_site_by_id(self, site_id):
"""
return a Site object that has an id=site_id
:param site_id: integer- the identification number of the site
:return: Sites
"""
try:
return self._edit_session.query(Site).filter_by(id=site_id).first()
except:
return None

# Variables methods
def get_used_variables(self):
"""
Expand All @@ -103,43 +83,6 @@ def get_used_variables(self):

return Variables

def get_variables_by_site_code(self, site_code): # covers NoDV, VarUnits, TimeUnits
"""
Finds all of variables at a site
:param site_code: str
:return: List[Variables]
"""
try:
var_ids = [x[0] for x in self._edit_session.query(distinct(Series.variable_id)).filter_by(
site_code=site_code).all()]
except:
var_ids = None

variables = []
for var_id in var_ids:
variables.append(self._edit_session.query(Variable).filter_by(id=var_id).first())

return variables

# Method methods
def get_all_methods(self):
return self._edit_session.query(Method).all()

def get_method_by_id(self, method_id):
try:
result = self._edit_session.query(Method).filter_by(id=method_id).first()
except:
result = None
return result

def get_method_by_description(self, method_code):
try:
result = self._edit_session.query(Method).filter_by(description=method_code).first()
except:
result = None
logger.error("method not found")
return result

def get_offset_types_by_series_id(self, series_id):
"""
Expand Down Expand Up @@ -215,6 +158,31 @@ def get_series_from_filter(self):
# Pass in probably a Series object, match it against the database
pass

def get_all_qualifiers(self):
"""
:return: List[Qualifiers]
"""
result = self._edit_session.query(Qualifier).order_by(Qualifier.code).all()
return result

def get_qualifier_by_code(self, code):
"""
:return: Qualifiers
"""
result = self._edit_session.query(Qualifier).filter(Qualifier.code==code).first()
return result

def get_qualifiers_by_series_id(self, series_id):
"""
:param series_id:
:return:
"""
subquery = self._edit_session.query(DataValue.qualifier_id).outerjoin(
Series.data_values).filter(Series.id == series_id, DataValue.qualifier_id != None).distinct().subquery()
return self._edit_session.query(Qualifier).join(subquery).distinct().all()

#Data Value Methods
def get_values_by_series(self, series_id):
Expand Down Expand Up @@ -430,7 +398,6 @@ def save_values(self, values):

def create_new_series(self, data_values, site_id, variable_id, method_id, source_id, qcl_id):
"""
:param data_values:
:param site_id:
:param variable_id:
Expand All @@ -451,9 +418,8 @@ def create_new_series(self, data_values, site_id, variable_id, method_id, source
self._edit_session.commit()
return series

def create_method(self, description, link):
def create_method(self, description, link): # DONE
"""
:param description:
:param link:
:return:
Expand All @@ -467,25 +433,19 @@ def create_method(self, description, link):

return meth

def create_variable_by_var(self, var):
def create_variable_by_var(self, var): # DONE
"""
:param var: Variable Object
:return:
"""
# try:
# self._edit_session.add(var)
# self._edit_session.commit()
# return var
# except:
# return None

self.create_service.createVariable(var)
return var

def create_variable(
self, code, name, speciation, variable_unit_id, sample_medium,
value_type, is_regular, time_support, time_unit_id, data_type,
general_category, no_data_value):
general_category, no_data_value): # DONE
"""
:param code:
Expand Down Expand Up @@ -519,7 +479,7 @@ def create_variable(
self.create_variable_by_var(var)
return var

def create_qcl(self, code, definition, explanation):
def create_qcl(self, code, definition, explanation): # DONE
"""
:param code:
Expand All @@ -532,8 +492,7 @@ def create_qcl(self, code, definition, explanation):
qcl.definition = definition
qcl.explanation = explanation

self._edit_session.add(qcl)
self._edit_session.commit()
self.create_service.createProcessingLevel(qcl)
return qcl


Expand Down

0 comments on commit cc7709d

Please sign in to comment.