Skip to content

Commit 0692195

Browse files
committed
Overwrite Flask app with final state of tutorials series part 3
1 parent eaa5081 commit 0692195

File tree

6 files changed

+58
-75
lines changed

6 files changed

+58
-75
lines changed
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from flask import render_template
2+
23
import config
34
from models import Person
45

5-
import sys
6-
76
app = config.connex_app
8-
app.add_api("swagger.yml")
7+
app.add_api(config.basedir / "swagger.yml")
98

109

1110
@app.route("/")
@@ -14,15 +13,5 @@ def home():
1413
return render_template("home.html", people=people)
1514

1615

17-
@app.route("/<person_name>")
18-
def person(person_name):
19-
print(person_name, file=sys.stderr)
20-
person_object = Person.query.order_by(
21-
Person.fname.ilike("person_name")
22-
).first()
23-
print(person_object.fname, file=sys.stderr)
24-
return render_template("person.html", person=person_object)
25-
26-
2716
if __name__ == "__main__":
2817
app.run(host="0.0.0.0", port=8000, debug=True)

build-a-rest-api-frontend/source_code_final/config.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import os
1+
import pathlib
2+
23
import connexion
3-
from flask_sqlalchemy import SQLAlchemy
44
from flask_marshmallow import Marshmallow
5+
from flask_sqlalchemy import SQLAlchemy
56

6-
basedir = os.path.abspath(os.path.dirname(__file__))
7+
basedir = pathlib.Path(__file__).parent.resolve()
78
connex_app = connexion.App(__name__, specification_dir=basedir)
89

910
app = connex_app.app
10-
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
11-
basedir, "people.db"
12-
)
11+
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{basedir / 'people.db'}"
1312
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
1413

1514
db = SQLAlchemy(app)

build-a-rest-api-frontend/source_code_final/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from datetime import datetime
2+
23
from marshmallow_sqlalchemy import fields
4+
35
from config import db, ma
46

57

@@ -29,8 +31,9 @@ class Person(db.Model):
2931
timestamp = db.Column(
3032
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
3133
)
34+
3235
notes = db.relationship(
33-
"Note",
36+
Note,
3437
backref="person",
3538
cascade="all, delete, delete-orphan",
3639
single_parent=True,
@@ -46,3 +49,8 @@ class Meta:
4649
include_relationships = True
4750

4851
notes = fields.Nested(NoteSchema, many=True)
52+
53+
54+
note_schema = NoteSchema()
55+
person_schema = PersonSchema()
56+
people_schema = PersonSchema(many=True)
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
from flask import make_response, abort
1+
from flask import abort, make_response
22

33
from config import db
4-
from models import (
5-
Note,
6-
NoteSchema,
7-
Person,
8-
)
4+
from models import Note, Person, note_schema
95

106

117
def read_one(note_id):
128
note = Note.query.get(note_id)
139

1410
if note is not None:
15-
note_schema = NoteSchema()
1611
return note_schema.dump(note)
1712
else:
1813
abort(404, f"Note with ID {note_id} not found")
@@ -22,12 +17,11 @@ def update(note_id, note):
2217
existing_note = Note.query.get(note_id)
2318

2419
if existing_note:
25-
schema = NoteSchema()
26-
update_note = schema.load(note, session=db.session)
20+
update_note = note_schema.load(note, session=db.session)
2721
existing_note.content = update_note.content
2822
db.session.merge(existing_note)
2923
db.session.commit()
30-
return schema.dump(existing_note), 201
24+
return note_schema.dump(existing_note), 201
3125
else:
3226
abort(404, f"Note with ID {note_id} not found")
3327

@@ -38,7 +32,7 @@ def delete(note_id):
3832
if existing_note:
3933
db.session.delete(existing_note)
4034
db.session.commit()
41-
return make_response(f"{note_id} successfully deleted", 200)
35+
return make_response(f"{note_id} successfully deleted", 204)
4236
else:
4337
abort(404, f"Note with ID {note_id} not found")
4438

@@ -48,10 +42,9 @@ def create(note):
4842
person = Person.query.get(person_id)
4943

5044
if person:
51-
schema = NoteSchema()
52-
new_note = schema.load(note, session=db.session)
45+
new_note = note_schema.load(note, session=db.session)
5346
person.notes.append(new_note)
5447
db.session.commit()
55-
return schema.dump(new_note), 201
48+
return note_schema.dump(new_note), 201
5649
else:
5750
abort(404, f"Person not found for ID: {person_id}")
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
1-
from flask import make_response, abort
1+
from flask import abort, make_response
22

33
from config import db
4-
from models import (
5-
Person,
6-
PersonSchema,
7-
)
4+
from models import Person, people_schema, person_schema
85

96

107
def read_all():
118
people = Person.query.all()
12-
person_schema = PersonSchema(many=True)
13-
return person_schema.dump(people)
9+
return people_schema.dump(people)
1410

1511

1612
def create(person):
17-
schema = PersonSchema()
18-
new_person = schema.load(person, session=db.session)
19-
db.session.add(new_person)
20-
db.session.commit()
21-
return schema.dump(new_person), 201
13+
lname = person.get("lname")
14+
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
15+
16+
if existing_person is None:
17+
new_person = person_schema.load(person, session=db.session)
18+
db.session.add(new_person)
19+
db.session.commit()
20+
return person_schema.dump(new_person), 201
21+
else:
22+
abort(406, f"Person with last name {lname} already exists")
2223

2324

24-
def read_one(person_id):
25-
person = Person.query.get(person_id)
25+
def read_one(lname):
26+
person = Person.query.filter(Person.lname == lname).one_or_none()
2627

2728
if person is not None:
28-
person_schema = PersonSchema()
2929
return person_schema.dump(person)
3030
else:
31-
abort(404, f"Person with ID {person_id} not found")
31+
abort(404, f"Person with last name {lname} not found")
3232

3333

34-
def update(person_id, person):
35-
existing_person = Person.query.get(person_id)
34+
def update(lname, person):
35+
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
3636

3737
if existing_person:
38-
schema = PersonSchema()
39-
update_person = schema.load(person, session=db.session)
38+
update_person = person_schema.load(person, session=db.session)
4039
existing_person.fname = update_person.fname
4140
db.session.merge(existing_person)
4241
db.session.commit()
43-
return schema.dump(existing_person), 201
42+
return person_schema.dump(existing_person), 201
4443
else:
45-
abort(404, f"Person with ID {person_id} not found")
44+
abort(404, f"Person with last name {lname} not found")
4645

4746

48-
def delete(person_id):
49-
existing_person = Person.query.get(person_id)
47+
def delete(lname):
48+
existing_person = Person.query.filter(Person.lname == lname).one_or_none()
5049

5150
if existing_person:
5251
db.session.delete(existing_person)
5352
db.session.commit()
54-
return make_response(f"{person_id} successfully deleted", 200)
53+
return make_response(f"{lname} successfully deleted", 200)
5554
else:
56-
abort(404, f"Person with ID {person_id} not found")
55+
abort(404, f"Person with last name {lname} not found")

build-a-rest-api-frontend/source_code_final/swagger.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ components:
1313
type: "object"
1414
required:
1515
- lname
16-
- fname
1716
properties:
1817
fname:
1918
type: "string"
2019
lname:
2120
type: "string"
22-
2321
parameters:
24-
person_id:
25-
name: "person_id"
26-
description: "ID of the person"
22+
lname:
23+
name: "lname"
24+
description: "Last name of the person to get"
2725
in: path
2826
required: True
2927
schema:
@@ -32,9 +30,9 @@ components:
3230
name: "note_id"
3331
description: "ID of the note"
3432
in: path
35-
required: True
33+
required: true
3634
schema:
37-
type: "string"
35+
type: "integer"
3836

3937
paths:
4038
/people:
@@ -62,15 +60,14 @@ paths:
6260
responses:
6361
"201":
6462
description: "Successfully created person"
65-
66-
/people/{person_id}:
63+
/people/{lname}:
6764
get:
6865
operationId: "people.read_one"
6966
tags:
7067
- People
7168
summary: "Read one person"
7269
parameters:
73-
- $ref: "#/components/parameters/person_id"
70+
- $ref: "#/components/parameters/lname"
7471
responses:
7572
"200":
7673
description: "Successfully read person"
@@ -80,7 +77,7 @@ paths:
8077
operationId: "people.update"
8178
summary: "Update a person"
8279
parameters:
83-
- $ref: "#/components/parameters/person_id"
80+
- $ref: "#/components/parameters/lname"
8481
responses:
8582
"200":
8683
description: "Successfully updated person"
@@ -96,11 +93,10 @@ paths:
9693
operationId: "people.delete"
9794
summary: "Delete a person"
9895
parameters:
99-
- $ref: "#/components/parameters/person_id"
96+
- $ref: "#/components/parameters/lname"
10097
responses:
10198
"204":
10299
description: "Successfully deleted person"
103-
104100
/notes:
105101
post:
106102
operationId: "notes.create"
@@ -117,13 +113,12 @@ paths:
117113
type: "object"
118114
properties:
119115
person_id:
120-
type: "string"
116+
type: "integer"
121117
content:
122118
type: "string"
123119
responses:
124120
"201":
125121
description: "Successfully created a note"
126-
127122
/notes/{note_id}:
128123
get:
129124
operationId: "notes.read_one"

0 commit comments

Comments
 (0)