Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 50ae563

Browse files
committed
Clean up the sample flask app and PEP8ify the code to standards
1 parent 10be54e commit 50ae563

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

app/app.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
import os
22

3-
from flask import Flask, request, render_template
3+
from flask import Flask, render_template, request
44
from flask_migrate import Migrate
55
from flask_sqlalchemy import SQLAlchemy
66

7-
APP = Flask(__name__)
8-
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
97

10-
APP.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://%s:%s@%s/%s' % (
11-
# ARGS.dbuser, ARGS.dbpass, ARGS.dbhost, ARGS.dbname
12-
os.environ['DBUSER'], os.environ['DBPASS'], os.environ['DBHOST'], os.environ['DBNAME']
8+
database_uri = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
9+
dbuser=os.environ['DBUSER'],
10+
dbpass=os.environ['DBPASS'],
11+
dbhost=os.environ['DBHOST'],
12+
dbname=os.environ['DBNAME']
13+
)
14+
15+
app = Flask(__name__)
16+
app.config.update(
17+
SQLALCHEMY_DATABASE_URI=database_uri,
18+
SQLALCHEMY_TRACK_MODIFICATIONS=False,
1319
)
1420

1521
# initialize the database connection
16-
DB = SQLAlchemy(APP)
22+
db = SQLAlchemy(app)
1723

1824
# initialize database migration management
19-
MIGRATE = Migrate(APP, DB)
20-
21-
from models import *
25+
migrate = Migrate(app, db)
2226

2327

24-
@APP.route('/')
28+
@app.route('/')
2529
def view_registered_guests():
30+
from models import Guest
2631
guests = Guest.query.all()
2732
return render_template('guest_list.html', guests=guests)
2833

2934

30-
@APP.route('/register', methods = ['GET'])
35+
@app.route('/register', methods=['GET'])
3136
def view_registration_form():
3237
return render_template('guest_registration.html')
3338

3439

35-
@APP.route('/register', methods = ['POST'])
40+
@app.route('/register', methods=['POST'])
3641
def register_guest():
42+
from models import Guest
3743
name = request.form.get('name')
3844
email = request.form.get('email')
39-
partysize = request.form.get('partysize')
40-
if not partysize or partysize=='':
41-
partysize = 1
45+
partysize = request.form.get('partysize', 1)
4246

4347
guest = Guest(name, email, partysize)
44-
DB.session.add(guest)
45-
DB.session.commit()
48+
db.session.add(guest)
49+
db.session.commit()
4650

47-
return render_template('guest_confirmation.html',
48-
name=name, email=email, partysize=partysize)
51+
return render_template(
52+
'guest_confirmation.html', name=name, email=email, partysize=partysize)

app/models.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from app import DB
1+
from app import db
22

3-
class Guest(DB.Model):
3+
4+
class Guest(db.Model):
45
"""Simple database model to track event attendees."""
5-
6+
67
__tablename__ = 'guests'
7-
id = DB.Column(DB.Integer, primary_key=True)
8-
name = DB.Column(DB.String(80))
9-
email = DB.Column(DB.String(120))
10-
partysize = DB.Column(DB.Integer, default=1)
8+
id = db.Column(db.Integer, primary_key=True)
9+
name = db.Column(db.String(80))
10+
email = db.Column(db.String(120))
11+
partysize = db.Column(db.Integer, default=1)
1112

1213
def __init__(self, name=None, email=None, partysize=1):
1314
self.name = name
1415
self.email = email
1516
self.partysize = partysize
16-

app/templates/guest_registration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h1>Guest Registration</h1>
88
<form action="/register" method="POST">
99
<div class="form-group">
1010
<label for="namefield">Name</label>
11-
<input type="text" class="form-control" id="namefield" name="name" aria-describedby="emailHelp" placeholder="Email">
11+
<input type="text" class="form-control" id="namefield" name="name" aria-describedby="emailHelp" placeholder="Name">
1212
<small id="emailHelp" class="form-text text-muted">Let us know who you are.</small>
1313
</div>
1414
<div class="form-group">

0 commit comments

Comments
 (0)