- additional module for Gschliefghraben

This commit is contained in:
Arno Kaimbacher 2022-02-21 16:07:04 +01:00
parent 54b210c07d
commit c62eb2cdb0
8 changed files with 243 additions and 1 deletions

View File

@ -0,0 +1,2 @@
# For relative imports to work in Python 3.6
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

2
db/__init__.py Normal file
View File

@ -0,0 +1,2 @@
# For relative imports to work in Python 3.6
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

169
db/pg_models.py Normal file
View File

@ -0,0 +1,169 @@
'''
Tutorial link: https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
Sqlalchemy version: 1.2.15
Python version: 3.7
'''
import os
from sqlalchemy import (create_engine, Column, Integer,
SmallInteger, String, ForeignKey, DateTime, Numeric)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
import sqlalchemy.orm.session
Base = declarative_base()
class Platform(Base):
""" Platform class """
__tablename__ = 'platform'
__table_args__ = {"schema": "gba"}
id = Column('platform_id', Integer, primary_key=True)
identifier = Column('identifier', String)
sta_identifier = Column('sta_identifier', String)
name = Column('name', String)
# datasets = relationship('Dataset')
datasets = relationship('Dataset', back_populates="platform", lazy=True)
def __repr__(self):
return f'Platform {self.name}'
class Phenomenon(Base):
""" phenomenon class """
__tablename__ = 'phenomenon'
__table_args__ = {"schema": "gba"}
id = Column('phenomenon_id', Integer, primary_key=True)
name = Column('name', String)
sta_identifier = Column('sta_identifier', String)
# datasets = relationship('Dataset')
datasets = relationship('Dataset', back_populates="phenomenon", lazy=True)
def __repr__(self):
return f'Phenomenon {self.name}'
class Procedure(Base):
""" procedure class """
__tablename__ = 'procedure'
__table_args__ = {"schema": "gba"}
id = Column('procedure_id', Integer, primary_key=True)
name = Column('name', String)
sta_identifier = Column('sta_identifier', String)
# datasets = relationship('Dataset')
datasets = relationship('Dataset', back_populates="procedure", lazy=True)
def __repr__(self):
return f'Procedure {self.name}'
class Dataset(Base):
""" dataset class """
__tablename__ = 'dataset'
__table_args__ = {"schema": "gba"}
id = Column('dataset_id', Integer, primary_key=True)
name = Column('name', String)
is_published = Column('is_published', SmallInteger)
is_hidden = Column('is_hidden', SmallInteger)
dataset_type = Column('dataset_type', String)
observation_type = Column('observation_type', String)
value_type = Column('value_type', String)
last_time = Column('last_time', DateTime)
last_value = Column('last_value', Numeric(20, 10))
fk_last_observation_id = Column(
'fk_last_observation_id',
Integer
)
# last_observation = relationship(
# "Observation", foreign_keys=[fk_last_observation_id])
first_time = Column('first_time', DateTime)
first_value = Column('first_value', Numeric(20, 10))
fk_first_observation_id = Column(
'fk_first_observation_id',
Integer
)
# first_observation = relationship("Observation", foreign_keys=[
# fk_first_observation_id])
observations = relationship(
'Observation', back_populates='dataset', lazy=True)
fk_procedure_id = Column('fk_procedure_id', Integer, ForeignKey(
'gba.procedure.procedure_id'), nullable=False)
# procedure = relationship("Procedure", lazy="joined")
procedure = relationship(
"Procedure", back_populates="datasets", lazy="joined")
fk_phenomenon_id = Column(
'fk_phenomenon_id', Integer, ForeignKey('gba.phenomenon.phenomenon_id'), nullable=False)
# phenomenon = relationship("Phenomenon", lazy="joined", foreign_keys=[fk_phenomenon_id])
phenomenon = relationship(
"Phenomenon", back_populates="datasets", lazy="joined")
# fk_platform_id = Column(
# 'fk_platform_id', Integer, ForeignKey('gba.platform.platform_id'), nullable=True)
# # platform = relationship("Platform", lazy="joined", foreign_keys=[fk_platform_id])
fk_platform_id = Column('fk_platform_id', Integer, ForeignKey(
'gba.platform.platform_id'), nullable=True)
platform = relationship(
"Platform", back_populates="datasets", lazy="joined")
def __repr__(self):
return f'Dataset {self.name}'
class Observation(Base):
""" observation class """
__tablename__ = 'observation'
__table_args__ = {"schema": "gba"}
id = Column('observation_id', Integer, primary_key=True)
name = Column('name', String)
value_type = Column('value_type', String)
# pitch = Column('PITCH', String)
# roll = Column('ROLL', String)
sampling_time_start = Column('sampling_time_start', DateTime)
sampling_time_end = Column('sampling_time_end', DateTime)
result_time = Column('result_time', DateTime)
sta_identifier = Column('sta_identifier', String)
value_quantity = Column('value_quantity', Numeric(20, 10), nullable=False)
# fk_dataset_id = Column('fk_dataset_id', Integer,
# ForeignKey('gba.dataset.dataset_id'))
# dataset = relationship("Dataset", lazy="joined",
# foreign_keys=[fk_dataset_id])
fk_dataset_id = Column(Integer, ForeignKey(
'gba.dataset.dataset_id'), nullable=False)
dataset = relationship("Dataset", back_populates="observations")
def __repr__(self):
return f'Observation {self.name}'
# @property
# def result_time(self):
# ''' Create a datetime object '''
# start_datetime = datetime.datetime.combine(self.date, self.ora)
# return start_datetime
def create_pg_session() -> sqlalchemy.orm.sessionmaker:
"""Return the sum of x and y."""
dbschema = ''
db_user = os.environ.get("POSTGIS_DBUSER")
db_password = os.environ.get("POSTGIS_DBPASSWORD")
db_url = os.environ.get("POSTGIS_DBURL")
engine = create_engine(
"postgresql+psycopg2://" + db_user + ":" + db_password + "@" + db_url,
connect_args={'options': '-csearch_path={}'.format(dbschema)},
isolation_level="READ UNCOMMITTED")
session_maker = sessionmaker(bind=engine)
session = session_maker()
Base.metadata.create_all(engine)
return session

View File

@ -0,0 +1,2 @@
# For relative imports to work in Python 3.6
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

View File

@ -0,0 +1,12 @@
# import os
# #import connexion
# from sqlalchemy.ext import
# from marshmallow import Marshmallow
# Create the SQLAlchemy db instance
# db = SQLAlchemy(app)
# Initialize Marshmallow
# ma = Marshmallow(app)

View File

@ -0,0 +1,15 @@
'''
Tutorial link: https://realpython.com/flask-connexion-rest-api-part-2/
Sqlalchemy version: 1.2.15
Python version: 3.7
'''
import os
# response = requests.get('https://api.com/')
# print(response) # shows the response's HTTP status code
# print(response.json()) # shows the response's JSON response body, if it has one
# print(response.content) # get the data content of the response
# (for your case this is the downloaded file)
# print(dir(response)) # shows you all the different methods you can call on this response object
db_user = os.environ.get("POSTGIS_DBUSER")
print(db_user)

View File

@ -0,0 +1,37 @@
'''
Tutorial link: https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
Sqlalchemy version: 1.2.15
Python version: 3.7
'''
from datetime import datetime
# from config import db, ma
# import os
from sqlalchemy import (Column, Integer,
String, DateTime)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import session
from marshmallow import Schema
from db.pg_models import create_pg_session
Base = declarative_base()
class Person(Base):
""" Platform class """
__tablename__ = 'person'
person_id = Column(Integer, primary_key=True)
lname = Column(String(32), index=True)
fname = Column(String(32))
timestamp = Column(DateTime, default=datetime.utcnow,
onupdate=datetime.utcnow)
class PersonSchema(Schema):
""" Platform class """
class Meta:
""" Platform class """
model = Person
#pg_session: session = create_pg_session()
sqla_session: session = create_pg_session()

View File

@ -31,3 +31,6 @@ d:/Software/geomon/.venv/Scripts/python.exe -m pip install -U autopep8
pip install fdb pip install fdb
pip install sqlalchemy-firebird pip install sqlalchemy-firebird
pip install psycopg2 pip install psycopg2
Marshmallow provides functionality to serialize and deserialize Python objects as they flow out of and into our JSON-based REST API. Marshmallow converts Python class instances to objects that can be converted to JSON.
pip install marshmallow-sqlalchemy marshmallowll