Commit ddba6dc8 authored by sarsonl's avatar sarsonl
Browse files

Minor updates to docstrings, addition of test SQLite db

The SQLite db and the code that generates the content for this
populate_db_test_cases.py has been added.

Some small changes made to comments and docstrings in files.

Addition of manage.py to manage migration within the postgresql db.
This is not finished at present.
parent 5a66f4a7
......@@ -6,12 +6,14 @@ import datetime
from flask import Flask, make_response, g
from flask_sqlalchemy import SQLAlchemy
from webargs.flaskparser import parser
from flask_migrate import Migrate
from hydws import __version__
from hydws.server import settings, errors
db = SQLAlchemy()
migrate = Migrate()
def create_app(config_dict={}):
......@@ -24,6 +26,7 @@ def create_app(config_dict={}):
app.config.update(config_dict)
db.init_app(app)
migrate.init_app(app, db)
# XXX(damb): Avoid circular imports.
from hydws.server.v1 import blueprint as api_v1_bp, API_VERSION_V1
......
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from hydws.server import settings, create_app
manager = Manager(create_app)
manager.add_option("-c", "--config", dest="config_module", required=False)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
......@@ -22,7 +22,7 @@ from hydws.db.orm import Borehole, BoreholeSection, HydraulicSample
#
# input comparison value can be list or a string.
# operator must belong in orm attr as op, op_, __op__
filter_hydraulics = [
filter_hydraulics = [# capital.
('datetime_value', 'ge', 'starttime'),
('datetime_value', 'le', 'endtime'),
('toptemperature_value', 'ge', 'mintoptemperature'),
......@@ -113,6 +113,7 @@ class DynamicQuery(object):
if existing_methods:
return existing_methods[0]
else:
# define a specific error here
raise Exception(f"Invalid operator: {op}")
def filter_query(self, query_params, filter_level):
......@@ -139,7 +140,7 @@ class DynamicQuery(object):
for filter_tuple in filter_condition:
try:
key, op, param_name = filter_tuple
except ValueError:
except ValueError as err:
raise Exception(f"Invalid filter input: {filter_tuple}")
param_value = query_params.get(param_name)
......
"""Create and populate db with possible cases for combinations of boreholes,
sections and hydraulics samples. It's purpose is to test correctly
returned messages on request when this db is used in a session.
Example Usage:
python populate_db_test_cases --db_url sqlite:///test.db
for creatiion of a db in the directory where the code is being run.
"""
import datetime
import argparse
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from hydws.db import orm
bh0 = orm.Borehole(
publicid='smi:ch.ethz.sed/bh/11111111-e4a0-4692-bf29-33b5591eb2d43',
depth_value=1000,
latitude_value=10.66320713,
latitude_uncertainty=0.5368853227,
longitude_value=10.66320713,
longitude_uncertainty=0.7947170871,
bedrockdepth_value=0,
literaturesource_author='Charles Dickens',
literaturesource_creator_mbox_resourceid='123456'
)
bh1 = orm.Borehole(
publicid='smi:ch.ethz.sed/bh/11111111-e4a0-4692-bf29-33b5591eb798',
depth_value=1000,
latitude_value=10.66320713,
latitude_uncertainty=0.5368853227,
longitude_value=10.66320713,
longitude_uncertainty=0.7947170871,
bedrockdepth_value=0)
bh1_section1 = orm.BoreholeSection(
publicid='smi:ch.ethz.sed/bh/section/'
'11111111-8d89-4f13-95e7-526ade73cc8b',
starttime=datetime.datetime(2010, 1, 10),
endtime=datetime.datetime(2010, 12, 10),
topclosed=False,
bottomclosed=False,
toplatitude_value=10.66320713,
toplatitude_uncertainty=0.5368853227,
toplongitude_value=10.66320713,
toplongitude_uncertainty=0.7947170871,
topdepth_value=0,
bottomlatitude_value=10.66320713,
bottomlatitude_uncertainty=0.5368853227,
bottomlongitude_value=10.66320713,
bottomlongitude_uncertainty=0.7947170871,
bottomdepth_value=1000,
holediameter_value=0.3,
casingdiameter_value=0.28, )
bh1_section2 = orm.BoreholeSection(
publicid='smi:ch.ethz.sed/bh/section/'
'11111111-8d89-4f13-95e7-526ade73cc5y',
starttime=datetime.datetime(2010, 1, 10),
topclosed=False,
bottomclosed=True,
toplatitude_value=10.66320713,
toplatitude_uncertainty=0.5368853227,
toplongitude_value=10.66320713,
toplongitude_uncertainty=0.7947170871,
topdepth_value=1000,
bottomlatitude_value=10.66320713,
bottomlatitude_uncertainty=0.5368853227,
bottomlongitude_value=10.66320713,
bottomlongitude_uncertainty=0.7947170871,
bottomdepth_value=1100,
holediameter_value=0.3,
casingdiameter_value=0.28, )
sample1 = orm.HydraulicSample(
publicid='smi:ch.ethz.sed/bh/section/sample/'
'11111111-ea2c-4b7f-a418-628afdf8f93b',
datetime_value=datetime.datetime(2010, 12, 9, 12, 00),
toptemperature_value=273,
topflow_value=42,
toppressure_value=73,
bottomtemperature_value=303,
bottomflow_value=42,
bottompressure_value=73,
fluiddensity_value=8,
fluidviscosity_value=0.5,
fluidph_value=7, )
sample2 = orm.HydraulicSample(
publicid='smi:ch.ethz.sed/bh/section/'
'sample/11111111-ea2c-4b7f-a418-628afdf8f92j',
datetime_value=datetime.datetime(2010, 12, 9, 13, 00),
toptemperature_value=290,
topflow_value=52,
toppressure_value=83,
bottomtemperature_value=313,
bottomflow_value=55,
bottompressure_value=83,
fluiddensity_value=8,
fluidviscosity_value=0.7,
fluidph_value=8, )
sample3 = orm.HydraulicSample(
publicid='smi:ch.ethz.sed/bh/section/sample/'
'11111111-ea2c-4b7f-a418-628afdf8f99t',
datetime_value=datetime.datetime(2010, 12, 1, 12, 00),
toptemperature_value=273,
topflow_value=42,
toppressure_value=73,
bottomtemperature_value=303,
bottomflow_value=42,
bottompressure_value=73,
fluiddensity_value=8,
fluidviscosity_value=0.5,
fluidph_value=7, )
# Second borehole, no sections.
bh2 = orm.Borehole(
publicid='smi:ch.ethz.sed/bh/11111111-e4a0-4692-bf29-33b5591eb799',
depth_value=2000,
latitude_value=40.66320713,
latitude_uncertainty=0.5368853227,
longitude_value=-10.66320713,
longitude_uncertainty=0.7947170871,
bedrockdepth_value=100)
# Third borehole, sections but no hydraulics
bh3 = orm.Borehole(
publicid='smi:ch.ethz.sed/bh/11111111-e4a0-4692-bf29-33b5591eb7987',
depth_value=1000,
latitude_value=10.66320713,
latitude_uncertainty=0.5368853227,
longitude_value=10.66320713,
longitude_uncertainty=0.7947170871,
bedrockdepth_value=0)
bh3_section1 = orm.BoreholeSection(
publicid='smi:ch.ethz.sed/bh/section/'
'11111111-8d89-4f13-95e7-526ade73cc7c',
starttime=datetime.datetime(2018, 12, 1, 00, 1),
endtime=datetime.datetime(2019, 2, 12, 00),
topclosed=False,
bottomclosed=False,
toplatitude_value=15.63484349,
toplatitude_uncertainty=0.0008854447,
toplongitude_value=-50.66323323,
toplongitude_uncertainty=0.7947170871,
topdepth_value=0,
bottomlatitude_value=50.66323327,
bottomlatitude_uncertainty=0.5368853227,
bottomlongitude_value=50.66323330,
bottomlongitude_uncertainty=0.7947170871,
bottomdepth_value=100,
holediameter_value=0.3,
casingdiameter_value=0.28, )
bh3_section2 = orm.BoreholeSection(
publicid='smi:ch.ethz.sed/bh/section/'
'11111111-8d89-4f13-95e7-526ade73cc2i',
starttime=datetime.datetime(2018, 12, 1, 00, 1),
endtime=datetime.datetime(2019, 2, 12, 00, 00),
topclosed=False,
bottomclosed=False,
toplatitude_value=15.63484349,
toplatitude_uncertainty=0.0008854447,
toplongitude_value=-50.66323323,
toplongitude_uncertainty=0.7947170871,
topdepth_value=100,
bottomlatitude_value=50.66323327,
bottomlatitude_uncertainty=0.5368853227,
bottomlongitude_value=50.66323330,
bottomlongitude_uncertainty=0.7947170871,
bottomdepth_value=200,
holediameter_value=0.3,
casingdiameter_value=0.28, )
bh3_section3 = orm.BoreholeSection(
publicid='smi:ch.ethz.sed/bh/section/'
'11111111-8d89-4f13-95e7-526ade73cc5e',
starttime=datetime.datetime(2019, 2, 12, 00, 1),
endtime=datetime.datetime(2019, 3, 12, 00, 00),
topclosed=False,
bottomclosed=True,
toplatitude_value=15.63484349,
toplatitude_uncertainty=0.0008854447,
toplongitude_value=-50.66323323,
toplongitude_uncertainty=0.7947170871,
topdepth_value=100,
bottomlatitude_value=50.66323327,
bottomlatitude_uncertainty=0.5368853227,
bottomlongitude_value=50.66323330,
bottomlongitude_uncertainty=0.7947170871,
bottomdepth_value=200,
holediameter_value=0.3,
casingdiameter_value=0.25, ) # This has been altered from bh3_section2
def insert_orm_values(db_url):
bh1_section1._hydraulics.append(sample1)
bh1_section1._hydraulics.append(sample2)
bh1_section1._hydraulics.append(sample3)
bh1._sections = [bh1_section1, bh1_section2]
bh3._sections = [bh3_section1, bh3_section2, bh3_section3]
try:
engine = create_engine(db_url, echo="debug")
Session = sessionmaker(bind=engine)
session = Session()
session.add(bh0)
session.add(bh1)
session.add(bh2)
session.add(bh3)
session.commit()
session.close()
except Exception as err:
print(err)
def parseargs():
parser = argparse.ArgumentParser()
parser.add_argument(
"--db_url", type=str, required=True,
help="e.g. sqlite:///test.db to create test.db in current"
" directory.")
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parseargs()
insert_orm_values(args.db_url)
......@@ -5,16 +5,18 @@
.. moduleauthor:: Laura Sarson <laura.sarson@sed.ethz.ch>
"""
import datetime
import logging
from functools import partial
from marshmallow import Schema, fields, post_dump, pre_load, validate, validates_schema
from marshmallow import Schema, fields, post_dump, pre_load, validate, validates_schema, post_load
from hydws.db.orm import Borehole, BoreholeSection, HydraulicSample
ValidateLatitude = validate.Range(min=-90., max=90.)
ValidateLongitude = validate.Range(min=-180., max=180.)
ValidatePositive = validate.Range(min=0.)
ValidateConfidenceLevel = validate.Range(min=0., max=100.)
ValidateCelcius = validate.Range(min=-273.15)
ValidateCelcius = validate.Range(min=0)
Datetime = partial(fields.DateTime, format='iso')
DatetimeRequired = partial(Datetime, required=True)
......@@ -315,7 +317,9 @@ class HydraulicSampleSchema(SchemaBase):
fluidph_confidencelevel = ConfidenceLevel()
fluidcomposition = fields.String()
@post_load
def make_hydraulics(self, data):
return HydraulicSample(**data)
class SectionSchema(SchemaBase):
"""Schema implementation of a borehole section.
......@@ -394,7 +398,9 @@ class SectionSchema(SchemaBase):
if starttime and endtime and starttime >= endtime:
raise ValidationError(
'endtime must be greater than starttime')
@post_load
def make_section(self, data):
return BoreholeSection(**data)
class BoreholeSchema(LiteratureSourceCreationInfoSchema, SchemaBase):
"""Schema implementation of a borehole."""
......@@ -432,3 +438,7 @@ class BoreholeSchema(LiteratureSourceCreationInfoSchema, SchemaBase):
sections = fields.Nested(SectionSchema, many=True,
attribute='_sections')
@post_load
def make_borehole(self, data):
return Borehole(**data)
......@@ -109,11 +109,9 @@ class BoreholeListResource(ResourceBase):
level = query_params.get('level')
if level == 'section':
query = query.options(lazyload(Borehole._sections))
dynamic_query = DynamicQuery(query)
# XXX(damb): Emulate QuakeML type Epoch (though on DB level it is
# defined as QuakeML type OpenEpoch
# Use borehole level filtering.
dynamic_query.filter_query(query_params,
'borehole')
......@@ -175,7 +173,7 @@ class BoreholeHydraulicSampleListResource(ResourceBase):
dynamic_query = DynamicQuery(query)
dynamic_query.filter_query(query_params,
'hydraulic')
print(query)
if query_params.get('limit'):
paginate_obj = dynamic_query.paginate_query(
query_params.get('limit'), query_params.get('page'))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment