- add marshmallow-sqlalchemy marshmallow for inserting db data via deserialization
This commit is contained in:
parent
c62eb2cdb0
commit
1dafc5824c
10 changed files with 118 additions and 189 deletions
|
@ -1,15 +1,82 @@
|
|||
'''
|
||||
Tutorial link: https://realpython.com/flask-connexion-rest-api-part-2/
|
||||
https://github.com/realpython/materials/blob/master/flask-connexion-rest-part-2/version_1/people.py
|
||||
Sqlalchemy version: 1.2.15
|
||||
Python version: 3.7
|
||||
'''
|
||||
|
||||
import os
|
||||
# import sys, inspect
|
||||
# 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
|
||||
# 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)
|
||||
|
||||
|
||||
def main():
|
||||
# db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
# print(db_user)
|
||||
|
||||
pg_session: session = create_pg_session()
|
||||
pg_person: Person = pg_session.query(Person).first()
|
||||
|
||||
# serialize db data to json
|
||||
person_schema = PersonSchema()
|
||||
dump_data = person_schema.dump(pg_person)
|
||||
print(dump_data)
|
||||
|
||||
# deserialize
|
||||
load_data: Person = person_schema.load(dump_data)
|
||||
print(load_data)
|
||||
create(dump_data)
|
||||
|
||||
|
||||
def create(person_json: PersonSchema):
|
||||
"""
|
||||
This function creates a new person in the people structure
|
||||
based on the passed-in person data
|
||||
:param person: person to create in people structure
|
||||
:return: 201 on success, 406 on person exists
|
||||
"""
|
||||
|
||||
login = person_json.get('login')
|
||||
#lname = person.get('lname')
|
||||
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)
|
||||
.one_or_none()
|
||||
)
|
||||
|
||||
# Can we insert this person?
|
||||
if existing_person is None:
|
||||
# Create a person instance using the schema and the passed in person
|
||||
schema = PersonSchema()
|
||||
# deserialize to object
|
||||
new_person: Person = schema.load(person_json)
|
||||
|
||||
# Add the person to the database
|
||||
session.add(new_person)
|
||||
session.commit()
|
||||
|
||||
# Serialize and return the newly created person in the response
|
||||
data = schema.dump(new_person)
|
||||
return data, 201
|
||||
# Otherwise, nope, person exists already
|
||||
else:
|
||||
print(409, f'Person {login} exists already')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
'''
|
||||
Tutorial link: https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
|
||||
Sqlalchemy version: 1.2.15
|
||||
Python version: 3.7
|
||||
Sqlalchemy version: 1.4.31
|
||||
Python version: 3.10
|
||||
'''
|
||||
#!/usr/bin/python# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import datetime
|
||||
# from config import db, ma
|
||||
|
@ -14,24 +15,32 @@ 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 marshmallow_sqlalchemy import SQLAlchemyAutoSchema
|
||||
|
||||
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,
|
||||
__tablename__ = 'accounts'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
person_id = Column('id', Integer, primary_key=True)
|
||||
lname = Column('last_name', String(255), index=True)
|
||||
fname = Column('first_name', String(255))
|
||||
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)
|
||||
|
||||
|
||||
class PersonSchema(Schema):
|
||||
class PersonSchema(SQLAlchemyAutoSchema):
|
||||
""" Platform class """
|
||||
class Meta:
|
||||
""" Platform class """
|
||||
model = Person
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
#pg_session: session = create_pg_session()
|
||||
sqla_session: session = create_pg_session()
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue