Skip to content

Commit 3ae7cce

Browse files
committed
Reformatted the code using the BLACK reformatter
1 parent d450773 commit 3ae7cce

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

flask-connexion-rest-part-3/build_database.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@
1212
("Cool, a mini-blogging application!", "2019-01-06 22:17:54"),
1313
("This could be useful", "2019-01-08 22:17:54"),
1414
("Well, sort of useful", "2019-03-06 22:17:54"),
15-
]
15+
],
1616
},
1717
{
1818
"fname": "Kent",
1919
"lname": "Brockman",
2020
"notes": [
21-
("I'm going to make really profound observations",
22-
"2019-01-07 22:17:54"),
23-
("Maybe they'll be more obvious than I thought",
24-
"2019-02-06 22:17:54"),
25-
]
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+
],
2630
},
2731
{
2832
"fname": "Bunny",
2933
"lname": "Easter",
3034
"notes": [
3135
("Has anyone seen my Easter eggs?", "2019-01-07 22:47:54"),
3236
("I'm really late delivering these!", "2019-04-06 22:17:54"),
33-
]
37+
],
3438
},
3539
]
3640

@@ -48,9 +52,11 @@
4852
# Add the notes for the person
4953
for note in person.get("notes"):
5054
content, timestamp = note
51-
p.notes.append(Note(
52-
content=content,
53-
timestamp=datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S"))
55+
p.notes.append(
56+
Note(
57+
content=content,
58+
timestamp=datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S"),
59+
)
5460
)
5561
db.session.add(p)
5662

flask-connexion-rest-part-3/models.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55

66
class Person(db.Model):
7-
__tablename__ = 'person'
7+
__tablename__ = "person"
88
person_id = db.Column(db.Integer, primary_key=True)
99
lname = db.Column(db.String(32))
1010
fname = db.Column(db.String(32))
1111
timestamp = db.Column(
1212
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
1313
)
1414
notes = db.relationship(
15-
'Note',
16-
backref='person',
17-
lazy='joined',
18-
cascade='all, delete, delete-orphan',
15+
"Note",
16+
backref="person",
17+
lazy="joined",
18+
cascade="all, delete, delete-orphan",
1919
single_parent=True,
20-
order_by='desc(Note.timestamp)'
20+
order_by="desc(Note.timestamp)",
2121
)
2222

2323

2424
class Note(db.Model):
25-
__tablename__ = 'note'
25+
__tablename__ = "note"
2626
note_id = db.Column(db.Integer, primary_key=True)
27-
person_id = db.Column(db.Integer, db.ForeignKey('person.person_id'))
27+
person_id = db.Column(db.Integer, db.ForeignKey("person.person_id"))
2828
content = db.Column(db.String, nullable=False)
2929
timestamp = db.Column(
3030
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
@@ -35,13 +35,15 @@ class PersonSchema(ma.ModelSchema):
3535
class Meta:
3636
model = Person
3737
sqla_session = db.session
38-
notes = fields.Nested('PersonNoteSchema', default=[], many=True)
38+
39+
notes = fields.Nested("PersonNoteSchema", default=[], many=True)
3940

4041

4142
class PersonNoteSchema(ma.ModelSchema):
4243
"""
4344
This class exists to get around a recursion issue
4445
"""
46+
4547
note_id = fields.Int()
4648
person_id = fields.Int()
4749
content = fields.Str()
@@ -52,13 +54,15 @@ class NoteSchema(ma.ModelSchema):
5254
class Meta:
5355
model = Note
5456
sqla_session = db.session
55-
person = fields.Nested('NotePersonSchema', default=None)
57+
58+
person = fields.Nested("NotePersonSchema", default=None)
5659

5760

5861
class NotePersonSchema(ma.ModelSchema):
5962
"""
6063
This class exists to get around a recursion issue
6164
"""
65+
6266
person_id = fields.Int()
6367
lname = fields.Str()
6468
fname = fields.Str()

flask-connexion-rest-part-3/notes.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def read_all():
1919
notes = Note.query.order_by(db.desc(Note.timestamp)).all()
2020

2121
# Serialize the list of notes from our data
22-
note_schema = NoteSchema(many=True, exclude=['person.notes'])
22+
note_schema = NoteSchema(many=True, exclude=["person.notes"])
2323
data = note_schema.dump(notes).data
2424
return data
2525

@@ -35,11 +35,12 @@ def read_one(person_id, note_id):
3535
:return: json string of note contents
3636
"""
3737
# Query the database for the note
38-
note = Note.query \
39-
.join(Person, Person.person_id == Note.person_id) \
40-
.filter(Person.person_id == person_id) \
41-
.filter(Note.note_id == note_id) \
38+
note = (
39+
Note.query.join(Person, Person.person_id == Note.person_id)
40+
.filter(Person.person_id == person_id)
41+
.filter(Note.note_id == note_id)
4242
.one_or_none()
43+
)
4344

4445
# Was a note found?
4546
if note is not None:
@@ -49,10 +50,7 @@ def read_one(person_id, note_id):
4950

5051
# Otherwise, nope, didn't find that note
5152
else:
52-
abort(
53-
404,
54-
"Note not found for Id: {note_id}".format(note_id=note_id),
55-
)
53+
abort(404, "Note not found for Id: {note_id}".format(note_id=note_id))
5654

5755

5856
def create(person_id, note):
@@ -97,10 +95,11 @@ def update(person_id, note_id, note):
9795
:param content: The JSON containing the note data
9896
:return: 200 on success
9997
"""
100-
update_note = Note.query \
101-
.filter(Person.person_id == person_id) \
102-
.filter(Note.note_id == note_id) \
98+
update_note = (
99+
Note.query.filter(Person.person_id == person_id)
100+
.filter(Note.note_id == note_id)
103101
.one_or_none()
102+
)
104103

105104
# Did we find an existing note?
106105
if update_note is not None:
@@ -124,10 +123,7 @@ def update(person_id, note_id, note):
124123

125124
# Otherwise, nope, didn't find that note
126125
else:
127-
abort(
128-
404,
129-
"Note not found for Id: {note_id}".format(note_id=note_id),
130-
)
126+
abort(404, "Note not found for Id: {note_id}".format(note_id=note_id))
131127

132128

133129
def delete(person_id, note_id):
@@ -139,10 +135,11 @@ def delete(person_id, note_id):
139135
:return: 200 on successful delete, 404 if not found
140136
"""
141137
# Get the note requested
142-
note = Note.query \
143-
.filter(Person.person_id == person_id) \
144-
.filter(Note.note_id == note_id) \
138+
note = (
139+
Note.query.filter(Person.person_id == person_id)
140+
.filter(Note.note_id == note_id)
145141
.one_or_none()
142+
)
146143

147144
# did we find a note?
148145
if note is not None:
@@ -154,7 +151,4 @@ def delete(person_id, note_id):
154151

155152
# Otherwise, nope, didn't find that note
156153
else:
157-
abort(
158-
404,
159-
"Note not found for Id: {note_id}".format(note_id=note_id),
160-
)
154+
abort(404, "Note not found for Id: {note_id}".format(note_id=note_id))

flask-connexion-rest-part-3/people.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ def read_one(person_id):
3333
:return: person matching id
3434
"""
3535
# Build the initial query
36-
person = Person.query \
37-
.filter(Person.person_id == person_id) \
38-
.outerjoin(Note) \
36+
person = (
37+
Person.query.filter(Person.person_id == person_id)
38+
.outerjoin(Note)
3939
.one_or_none()
40+
)
4041

4142
# Did we find a person?
4243
if person is not None:

0 commit comments

Comments
 (0)