Skip to content

Commit 00306ee

Browse files
committed
Think I finally have the demonstration web application nailed down! Whew!
1 parent 11b4346 commit 00306ee

File tree

14 files changed

+298
-157
lines changed

14 files changed

+298
-157
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
sqlite_url = "sqlite:////" + os.path.join(basedir, "people.db")
1616

1717
# Configure the SqlAlchemy part of the app instance
18-
app.config["SQLALCHEMY_ECHO"] = True
18+
app.config["SQLALCHEMY_ECHO"] = False
1919
app.config["SQLALCHEMY_DATABASE_URI"] = sqlite_url
2020
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
2121

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ class Person(db.Model):
1212
timestamp = db.Column(
1313
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
1414
)
15-
notes = db.relationship('Note',
16-
backref=db.backref('person',
17-
lazy='joined',
18-
cascade='delete, delete-orphan',
19-
single_parent=True),
20-
order_by='desc(Note.timestamp)')
15+
notes = db.relationship(
16+
'Note',
17+
backref='person',
18+
lazy='joined',
19+
cascade='all, delete, delete-orphan',
20+
single_parent=True,
21+
order_by='desc(Note.timestamp)'
22+
)
2123

2224

2325
class Note(db.Model):
2426
__tablename__ = 'note'
2527
note_id = db.Column(db.Integer, primary_key=True)
26-
person_id = db.Column(db.Integer, db.ForeignKey('person.person_id'), nullable=False)
28+
person_id = db.Column(db.Integer, db.ForeignKey('person.person_id'))
2729
content = db.Column(db.String, nullable=False)
2830
timestamp = db.Column(
2931
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def read_one(person_id):
3535
# Build the initial query
3636
person = Person.query \
3737
.filter(Person.person_id == person_id) \
38-
.join(Note) \
38+
.outerjoin(Note) \
3939
.options(db.joinedload(Person.notes)) \
4040
.one_or_none()
4141

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,30 @@ def home():
3030

3131
# Create a URL route in our application for "/people"
3232
@connex_app.route("/people")
33-
def people():
33+
@connex_app.route("/people/<int:person_id>")
34+
def people(person_id=""):
3435
"""
3536
This function just responds to the browser URL
3637
localhost:5000/people
3738
3839
:return: the rendered template "people.html"
3940
"""
40-
return render_template("people.html")
41+
return render_template("people.html", person_id=person_id)
4142

4243

4344
# Create a URL route to the notes page
44-
@connex_app.route("/notes/<int:person_id>")
45-
def notes(person_id):
45+
@connex_app.route("/people/<int:person_id>")
46+
@connex_app.route("/people/<int:person_id>/notes")
47+
@connex_app.route("/people/<int:person_id>/notes/<int:note_id>")
48+
def notes(person_id, note_id=""):
4649
"""
4750
This function responds to the browser URL
4851
localhost:5000/notes/<person_id>
4952
5053
:param person_id: Id of the person to show notes for
5154
:return: the rendered template "notes.html"
5255
"""
53-
return render_template("notes.html", person_id=person_id)
56+
return render_template("notes.html", person_id=person_id, note_id=note_id)
5457

5558

5659
if __name__ == "__main__":

flask-connexion-rest-part-3/static/css/home.css

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* CSS stylesheet for home page
33
*/
44

5+
div.blog {
6+
margin: 10px 10px 10px 10px;
7+
border: 2px solid darkgrey;
8+
}
9+
510
.blog table {
611
border-collapse: collapse;
712
width: 100%;
@@ -10,22 +15,16 @@
1015
.blog table caption {
1116
font-weight: bold;
1217
padding: 10px 0 10px 0;
13-
border-bottom: 1px solid darkgrey;
18+
border-bottom: 2px solid darkgrey;
1419
background-color: antiquewhite;
1520
}
1621

1722
.blog table thead {
1823
padding: 10px 0 10px 0;
1924
}
2025

21-
.blog table thead tj {
22-
border-right: 1px solid darkgrey;
23-
}
24-
25-
26-
th, td {
27-
padding: 3px;
28-
border: 1px solid darkgrey;
26+
th:not(:last-child), td:not(:last-child) {
27+
border-right: 2px solid darkgrey;
2928
}
3029

3130
td.name {
@@ -35,21 +34,32 @@ td.name {
3534

3635
td.timestamp {
3736
text-align: center;
38-
width: 230px;
37+
width: 280px;
3938
}
4039

4140
td.content {
4241
text-align: left;
42+
padding-left: 5px;
4343
}
4444

4545
tbody tr:nth-child(odd) {
46-
background-color: lightgrey;
46+
background-color: gainsboro;
4747
}
4848

49-
tbody tr {
49+
tbody td.name, tbody td.content {
5050
cursor: pointer;
51+
height: 33px;
52+
}
53+
54+
thead tr {
55+
height: 33px;
56+
border-bottom: 2px solid darkgrey;
57+
}
58+
59+
tbody tr:not(:last-child){
60+
border-bottom: 2px solid darkgrey;
5161
}
5262

5363
tbody tr:hover {
54-
background-color: aqua;
64+
background-color: powderblue;
5565
}

flask-connexion-rest-part-3/static/css/notes.css

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
margin-left: auto;
1616
margin-right: auto;
1717
padding: 5px;
18-
border: 1px solid lightgrey;
19-
border-radius: 3px;
18+
border: 2px solid darkgrey;
19+
border-radius: 4px;
2020
margin-bottom: 5px;
2121
}
2222

@@ -40,51 +40,67 @@ input[type=text] {
4040
width: 550px;
4141
}
4242

43-
.notes {
44-
width: 95%;
45-
margin-left: auto;
46-
margin-right: auto;
47-
margin-bottom: 5px;
43+
div.notes {
44+
margin: 10px 10px 10px 10px;
45+
border: 2px solid darkgrey;
4846
}
4947

5048
table {
5149
width: 100%;
5250
border-collapse: collapse;
5351
}
5452

55-
table, caption, th, td {
56-
border: 1px solid lightgrey;
57-
}
58-
5953
table caption {
60-
height: 33px;
6154
font-weight: bold;
62-
padding-top: 5px;
63-
border-bottom: none;
55+
padding: 10px 0 10px 0;
56+
border-bottom: 2px solid darkgrey;
57+
background-color: antiquewhite;
6458
}
6559

66-
tr {
67-
cursor: pointer;
68-
height: 33px;
60+
.blog table thead {
61+
padding: 10px 0 10px 0;
6962
}
7063

71-
tr:nth-child(even) {
72-
background-color: #f0f0f0
64+
th:not(:last-child), td:not(:last-child) {
65+
border-right: 2px solid darkgrey;
7366
}
7467

75-
tbody td.content {
76-
text-align: left;
77-
padding: 0 5px 0 5px;
68+
td.name {
69+
text-align: center;
70+
width: 175px;
7871
}
7972

80-
td {
73+
td.timestamp {
8174
text-align: center;
75+
width: 230px;
8276
}
8377

84-
tbody tr:hover {
85-
background-color: cornsilk;
78+
td.content {
79+
text-align: left;
80+
padding-left: 5px;
81+
}
82+
83+
tbody tr:nth-child(odd) {
84+
background-color: gainsboro;
85+
}
86+
87+
tbody tr {
88+
cursor: pointer;
89+
height: 33px;
8690
}
8791

92+
thead tr {
93+
height: 33px;
94+
border-bottom: 2px solid darkgrey;
95+
}
96+
97+
tbody tr:not(:last-child){
98+
border-bottom: 2px solid darkgrey;
99+
}
100+
101+
tbody tr:hover {
102+
background-color: powderblue;
103+
}
88104
.content {
89105
width: 70%;
90106
}

flask-connexion-rest-part-3/static/css/people.css

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
margin-left: auto;
1212
margin-right: auto;
1313
padding: 5px;
14-
border: 1px solid lightgrey;
15-
border-radius: 3px;
14+
border: 2px solid darkgrey;
15+
border-radius: 4px;
1616
margin-bottom: 5px;
1717
}
1818

@@ -28,44 +28,59 @@ button {
2828
background-color: #eee;
2929
}
3030

31-
.people {
32-
width: 50%;
33-
margin-left: auto;
34-
margin-right: auto;
35-
margin-bottom: 5px;
31+
div.people {
32+
margin: 10px 30px 10px 30px;
33+
border: 2px solid darkgrey;
3634
}
3735

3836
table {
3937
width: 100%;
4038
border-collapse: collapse;
4139
}
4240

43-
table, caption, th, td {
44-
border: 1px solid lightgrey;
41+
.people table caption {
42+
font-weight: bold;
43+
padding: 10px 0 10px 0;
44+
border-bottom: 2px solid darkgrey;
45+
background-color: antiquewhite;
4546
}
4647

47-
table caption {
48-
height: 33px;
49-
font-weight: bold;
50-
padding-top: 5px;
51-
border-bottom: none;
48+
.people table thead {
49+
padding: 10px 0 10px 0;
5250
}
5351

54-
tr {
55-
cursor: pointer;
56-
height: 33px;
52+
th:not(:last-child), td:not(:last-child) {
53+
border-right: 2px solid darkgrey;
5754
}
5855

5956
tr:nth-child(even) {
60-
background-color: #f0f0f0
57+
background-color: gainsboro;
6158
}
6259

6360
td {
6461
text-align: center;
6562
}
6663

64+
tbody td.timestamp {
65+
width: 280px;
66+
}
67+
68+
tbody td.name {
69+
cursor: pointer;
70+
height: 33px;
71+
}
72+
73+
thead tr {
74+
height: 33px;
75+
border-bottom: 2px solid darkgrey;
76+
}
77+
78+
tbody tr:not(:last-child){
79+
border-bottom: 2px solid darkgrey;
80+
}
81+
6782
tbody tr:hover {
68-
background-color: cornsilk;
83+
background-color: powderblue;
6984
}
7085

7186
.error {

flask-connexion-rest-part-3/static/js/home.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ns.view = (function() {
4141
// Return the API
4242
return {
4343
build_table: function(data) {
44-
var source = $('#blog-table-template').html(),
44+
let source = $('#blog-table-template').html(),
4545
template = Handlebars.compile(source),
4646
html;
4747

@@ -76,6 +76,23 @@ ns.controller = (function(m, v) {
7676
model.read();
7777
}, 100);
7878

79+
// handle application events
80+
$('table').on('dblclick', 'tbody td.name', function(e) {
81+
let $target = $(e.target).parent(),
82+
person_id = $target.data('person_id');
83+
84+
window.location = `/people/${person_id}`;
85+
86+
});
87+
88+
$('table').on('dblclick', 'tbody td.content', function(e) {
89+
let $target = $(e.target).parent(),
90+
person_id = $target.data('person_id'),
91+
note_id = $target.data('note_id');
92+
93+
window.location = `people/${person_id}/notes/${note_id}`;
94+
});
95+
7996
// Handle the model events
8097
$event_pump.on('model_read_success', function(e, data) {
8198
view.build_table(data);

0 commit comments

Comments
 (0)