add observation class for importing observations
This commit is contained in:
parent
1dafc5824c
commit
7dd739b04e
|
@ -1,2 +1,4 @@
|
|||
# For relative imports to work in Python 3.6
|
||||
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
||||
''' For relative imports to work in Python 3.6 '''
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
||||
|
|
|
@ -10,10 +10,10 @@ import os
|
|||
# currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
# parentdir = os.path.dirname(currentdir)
|
||||
# sys.path.insert(0, parentdir)
|
||||
from db.pg_models import create_pg_session
|
||||
from sqlalchemy.orm import session
|
||||
from gschliefgraben_glasfaser.models import PersonSchema
|
||||
from models import Person, PersonSchema
|
||||
from models import ObservationSchema, Person, PersonSchema, Observation
|
||||
from db.pg_models import create_pg_session
|
||||
#from models import Person, PersonSchema
|
||||
# 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
|
||||
|
@ -21,21 +21,29 @@ from models import Person, PersonSchema
|
|||
|
||||
|
||||
def main():
|
||||
# db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
# print(db_user)
|
||||
''' main method '''
|
||||
db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
print(db_user)
|
||||
|
||||
pg_session: session = create_pg_session()
|
||||
pg_person: Person = pg_session.query(Person).first()
|
||||
# pg_person: Person = pg_session.query(Person).first()
|
||||
observation: Observation = pg_session.query(Observation).first()
|
||||
print (observation)
|
||||
|
||||
# serialize db data to json
|
||||
person_schema = PersonSchema()
|
||||
dump_data = person_schema.dump(pg_person)
|
||||
# person_schema = PersonSchema()
|
||||
# dump_data = person_schema.dump(pg_person)
|
||||
# print(dump_data)
|
||||
# serialize db data to json
|
||||
observation_schema = ObservationSchema()
|
||||
dump_data = observation_schema.dump(observation)
|
||||
print(dump_data)
|
||||
|
||||
# deserialize
|
||||
load_data: Person = person_schema.load(dump_data)
|
||||
print(load_data)
|
||||
create(dump_data)
|
||||
# # deserialize
|
||||
# load_data: Person = person_schema.load(dump_data)
|
||||
# print(load_data)
|
||||
|
||||
# create(dump_data)
|
||||
|
||||
|
||||
def create(person_json: PersonSchema):
|
||||
|
@ -48,13 +56,13 @@ def create(person_json: PersonSchema):
|
|||
|
||||
login = person_json.get('login')
|
||||
#lname = person.get('lname')
|
||||
session = create_pg_session()
|
||||
db_session = create_pg_session()
|
||||
|
||||
# existing_person = Person.query \
|
||||
# .filter(Person.login == login) \
|
||||
# .one_or_none()
|
||||
existing_person: bool = ( \
|
||||
session.query(Person) \
|
||||
db_session.query(Person) \
|
||||
.filter(Person.login == login)
|
||||
.one_or_none()
|
||||
)
|
||||
|
@ -67,8 +75,8 @@ def create(person_json: PersonSchema):
|
|||
new_person: Person = schema.load(person_json)
|
||||
|
||||
# Add the person to the database
|
||||
session.add(new_person)
|
||||
session.commit()
|
||||
db_session.add(new_person)
|
||||
db_session.commit()
|
||||
|
||||
# Serialize and return the newly created person in the response
|
||||
data = schema.dump(new_person)
|
||||
|
|
|
@ -10,16 +10,52 @@ from datetime import datetime
|
|||
|
||||
# import os
|
||||
from sqlalchemy import (Column, Integer,
|
||||
String, DateTime)
|
||||
String, DateTime, ForeignKey, Numeric)
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import session
|
||||
from marshmallow import Schema
|
||||
from db.pg_models import create_pg_session
|
||||
from sqlalchemy.orm import session, relationship
|
||||
#from marshmallow import Schema
|
||||
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
|
||||
from db.pg_models import create_pg_session
|
||||
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
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")
|
||||
|
||||
class ObservationSchema(SQLAlchemyAutoSchema):
|
||||
""" Platform class """
|
||||
class Meta:
|
||||
""" Platform class """
|
||||
model = Observation
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
#pg_session: session = create_pg_session()
|
||||
sqla_session: session = create_pg_session()
|
||||
|
||||
|
||||
class Person(Base):
|
||||
""" Platform class """
|
||||
__tablename__ = 'accounts'
|
||||
|
@ -30,6 +66,7 @@ class Person(Base):
|
|||
login = Column(String(255))
|
||||
timestamp = Column('updated_at', DateTime, default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow)
|
||||
|
||||
def __repr__(self):
|
||||
return "<User(name='%s', lastname='%s')>" % (
|
||||
self.login, self.lname)
|
||||
|
|
|
@ -30,7 +30,7 @@ d:/Software/geomon/.venv/Scripts/python.exe -m pip install -U autopep8
|
|||
|
||||
python -m pip install fdb
|
||||
python -m pip install sqlalchemy-firebird
|
||||
python -m pip install psycopg2
|
||||
python -m pip uninstall 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.
|
||||
python -m pip install marshmallow-sqlalchemy marshmallow
|
Loading…
Reference in New Issue
Block a user