diff --git a/gschliefgraben_glasfaser/__init__.py b/gschliefgraben_glasfaser/__init__.py index c4f4da5..66cf6dd 100644 --- a/gschliefgraben_glasfaser/__init__.py +++ b/gschliefgraben_glasfaser/__init__.py @@ -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__))) \ No newline at end of file +''' For relative imports to work in Python 3.6 ''' +import os +import sys +sys.path.append(os.path.dirname(os.path.realpath(__file__))) diff --git a/gschliefgraben_glasfaser/config.py b/gschliefgraben_glasfaser/config.py index 7fbc65c..0fabc4d 100644 --- a/gschliefgraben_glasfaser/config.py +++ b/gschliefgraben_glasfaser/config.py @@ -1,6 +1,6 @@ # import os # #import connexion -# from sqlalchemy.ext import +# from sqlalchemy.ext import # from marshmallow import Marshmallow @@ -9,4 +9,4 @@ # Initialize Marshmallow -# ma = Marshmallow(app) \ No newline at end of file +# ma = Marshmallow(app) diff --git a/gschliefgraben_glasfaser/main.py b/gschliefgraben_glasfaser/main.py index 3038f20..4dc35d7 100644 --- a/gschliefgraben_glasfaser/main.py +++ b/gschliefgraben_glasfaser/main.py @@ -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,14 +56,14 @@ 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) \ - .filter(Person.login == login) + db_session.query(Person) \ + .filter(Person.login == login) .one_or_none() ) @@ -66,9 +74,9 @@ def create(person_json: PersonSchema): # deserialize to object new_person: Person = schema.load(person_json) - # Add the person to the database - session.add(new_person) - session.commit() + # Add the person to the database + db_session.add(new_person) + db_session.commit() # Serialize and return the newly created person in the response data = schema.dump(new_person) diff --git a/gschliefgraben_glasfaser/models.py b/gschliefgraben_glasfaser/models.py index fb6589c..f9f7b6d 100644 --- a/gschliefgraben_glasfaser/models.py +++ b/gschliefgraben_glasfaser/models.py @@ -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,9 +66,10 @@ class Person(Base): login = Column(String(255)) timestamp = Column('updated_at', DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + def __repr__(self): return "" % ( - self.login, self.lname) + self.login, self.lname) class PersonSchema(SQLAlchemyAutoSchema): diff --git a/notes.txt b/notes.txt index 088213e..60df6d7 100644 --- a/notes.txt +++ b/notes.txt @@ -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 \ No newline at end of file