Skip to content

Commit b65dbc1

Browse files
authored
Merge branch 'master' into openai-dalle
2 parents fbcd91f + 09239e7 commit b65dbc1

File tree

21 files changed

+2219
-0
lines changed

21 files changed

+2219
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
.hypothesis/
50+
.pytest_cache/
51+
52+
# Translations
53+
*.mo
54+
*.pot
55+
56+
# Django stuff:
57+
*.log
58+
local_settings.py
59+
db.sqlite3
60+
61+
# Flask stuff:
62+
instance/
63+
.webassets-cache
64+
65+
# Scrapy stuff:
66+
.scrapy
67+
68+
# Sphinx documentation
69+
docs/_build/
70+
71+
# PyBuilder
72+
target/
73+
74+
# Jupyter Notebook
75+
.ipynb_checkpoints
76+
77+
# IPython
78+
profile_default/
79+
ipython_config.py
80+
81+
# pyenv
82+
.python-version
83+
84+
# celery beat schedule file
85+
celerybeat-schedule
86+
87+
# SageMath parsed files
88+
*.sage.py
89+
90+
# Environments
91+
.env
92+
.venv
93+
env/
94+
venv/
95+
ENV/
96+
env.bak/
97+
venv.bak/
98+
99+
# Spyder project settings
100+
.spyderproject
101+
.spyproject
102+
103+
# Rope project settings
104+
.ropeproject
105+
106+
# mkdocs documentation
107+
/site
108+
109+
# mypy
110+
.mypy_cache/
111+
.dmypy.json
112+
dmypy.json
113+
114+
# Pyre type checker
115+
.pyre/
116+
117+
# ignore the database
118+
people.db

flask-connexion-rest-part-4/__init__.py

Whitespace-only changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
from datetime import datetime
3+
from config import db
4+
from models import Person, Note
5+
6+
# Data to initialize database with
7+
PEOPLE = [
8+
{
9+
"fname": "Doug",
10+
"lname": "Farrell",
11+
"notes": [
12+
("Cool, a mini-blogging application!", "2019-01-06 22:17:54"),
13+
("This could be useful", "2019-01-08 22:17:54"),
14+
("Well, sort of useful", "2019-03-06 22:17:54"),
15+
],
16+
},
17+
{
18+
"fname": "Kent",
19+
"lname": "Brockman",
20+
"notes": [
21+
(
22+
"I'm going to make really profound observations",
23+
"2019-01-07 22:17:54",
24+
),
25+
(
26+
"Maybe they'll be more obvious than I thought",
27+
"2019-02-06 22:17:54",
28+
),
29+
],
30+
},
31+
{
32+
"fname": "Bunny",
33+
"lname": "Easter",
34+
"notes": [
35+
("Has anyone seen my Easter eggs?", "2019-01-07 22:47:54"),
36+
("I'm really late delivering these!", "2019-04-06 22:17:54"),
37+
],
38+
},
39+
]
40+
41+
# Delete database file if it exists currently
42+
if os.path.exists("people.db"):
43+
os.remove("people.db")
44+
45+
# Create the database
46+
db.create_all()
47+
48+
# iterate over the PEOPLE structure and populate the database
49+
for person in PEOPLE:
50+
p = Person(lname=person.get("lname"), fname=person.get("fname"))
51+
52+
# Add the notes for the person
53+
for note in person.get("notes"):
54+
content, timestamp = note
55+
p.notes.append(
56+
Note(
57+
content=content,
58+
timestamp=datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S"),
59+
)
60+
)
61+
db.session.add(p)
62+
63+
db.session.commit()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import connexion
3+
from flask_sqlalchemy import SQLAlchemy
4+
from flask_marshmallow import Marshmallow
5+
6+
basedir = os.path.abspath(os.path.dirname(__file__))
7+
8+
# Create the connexion application instance
9+
connex_app = connexion.App(__name__, specification_dir=basedir)
10+
11+
# Get the underlying Flask app instance
12+
app = connex_app.app
13+
14+
# Build the Sqlite ULR for SqlAlchemy
15+
sqlite_url = "sqlite:////" + os.path.join(basedir, "people.db")
16+
17+
# Configure the SqlAlchemy part of the app instance
18+
app.config["SQLALCHEMY_ECHO"] = False
19+
app.config["SQLALCHEMY_DATABASE_URI"] = sqlite_url
20+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
21+
22+
# Create the SqlAlchemy db instance
23+
db = SQLAlchemy(app)
24+
25+
# Initialize Marshmallow
26+
ma = Marshmallow(app)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from datetime import datetime
2+
from config import db, ma
3+
from marshmallow import fields
4+
5+
6+
class Person(db.Model):
7+
__tablename__ = "person"
8+
person_id = db.Column(db.Integer, primary_key=True)
9+
lname = db.Column(db.String(32))
10+
fname = db.Column(db.String(32))
11+
timestamp = db.Column(
12+
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
13+
)
14+
notes = db.relationship(
15+
"Note",
16+
backref="person",
17+
cascade="all, delete, delete-orphan",
18+
single_parent=True,
19+
order_by="desc(Note.timestamp)",
20+
)
21+
22+
23+
class Note(db.Model):
24+
__tablename__ = "note"
25+
note_id = db.Column(db.Integer, primary_key=True)
26+
person_id = db.Column(db.Integer, db.ForeignKey("person.person_id"))
27+
content = db.Column(db.String, nullable=False)
28+
timestamp = db.Column(
29+
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
30+
)
31+
32+
33+
class PersonSchema(ma.ModelSchema):
34+
def __init__(self, **kwargs):
35+
super().__init__(strict=True, **kwargs)
36+
37+
class Meta:
38+
model = Person
39+
sqla_session = db.session
40+
41+
notes = fields.Nested("PersonNoteSchema", default=[], many=True)
42+
43+
44+
class PersonNoteSchema(ma.ModelSchema):
45+
"""
46+
This class exists to get around a recursion issue
47+
"""
48+
49+
def __init__(self, **kwargs):
50+
super().__init__(strict=True, **kwargs)
51+
52+
note_id = fields.Int()
53+
person_id = fields.Int()
54+
content = fields.Str()
55+
timestamp = fields.Str()
56+
57+
58+
class NoteSchema(ma.ModelSchema):
59+
def __init__(self, **kwargs):
60+
super().__init__(strict=True, **kwargs)
61+
62+
class Meta:
63+
model = Note
64+
sqla_session = db.session
65+
66+
person = fields.Nested("NotePersonSchema", default=None)
67+
68+
69+
class NotePersonSchema(ma.ModelSchema):
70+
"""
71+
This class exists to get around a recursion issue
72+
"""
73+
74+
def __init__(self, **kwargs):
75+
super().__init__(strict=True, **kwargs)
76+
77+
person_id = fields.Int()
78+
lname = fields.Str()
79+
fname = fields.Str()
80+
timestamp = fields.Str()

0 commit comments

Comments
 (0)