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

Commit

Permalink
Clean up the sample flask app and PEP8ify the code to standards
Browse files Browse the repository at this point in the history
  • Loading branch information
nnja committed Sep 12, 2018
1 parent 10be54e commit 2491a96
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
42 changes: 23 additions & 19 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
import os

from flask import Flask, request, render_template
from flask import Flask, render_template, request
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

APP = Flask(__name__)
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

APP.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://%s:%s@%s/%s' % (
# ARGS.dbuser, ARGS.dbpass, ARGS.dbhost, ARGS.dbname
os.environ['DBUSER'], os.environ['DBPASS'], os.environ['DBHOST'], os.environ['DBNAME']
database_uri = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
dbuser=os.environ['DBUSER'],
dbpass=os.environ['DBPASS'],
dbhost=os.environ['DBHOST'],
dbname=os.environ['DBNAME']
)

app = Flask(__name__)
app.config.update(
SQLALCHEMY_DATABASE_URI=database_uri,
SQLALCHEMY_TRACK_MODIFICATIONS=False,
)

# initialize the database connection
DB = SQLAlchemy(APP)
db = SQLAlchemy(app)

# initialize database migration management
MIGRATE = Migrate(APP, DB)

from models import *
migrate = Migrate(app, db)


@APP.route('/')
@app.route('/')
def view_registered_guests():
from models import Guest
guests = Guest.query.all()
return render_template('guest_list.html', guests=guests)


@APP.route('/register', methods = ['GET'])
@app.route('/register', methods=['GET'])
def view_registration_form():
return render_template('guest_registration.html')


@APP.route('/register', methods = ['POST'])
@app.route('/register', methods=['POST'])
def register_guest():
from models import Guest
name = request.form.get('name')
email = request.form.get('email')
partysize = request.form.get('partysize')
if not partysize or partysize=='':
partysize = 1

guest = Guest(name, email, partysize)
DB.session.add(guest)
DB.session.commit()
db.session.add(guest)
db.session.commit()

return render_template('guest_confirmation.html',
name=name, email=email, partysize=partysize)
return render_template(
'guest_confirmation.html', name=name, email=email, partysize=partysize)
18 changes: 9 additions & 9 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from app import DB
from app import db

class Guest(DB.Model):

class Guest(db.Model):
"""Simple database model to track event attendees."""

__tablename__ = 'guests'
id = DB.Column(DB.Integer, primary_key=True)
name = DB.Column(DB.String(80))
email = DB.Column(DB.String(120))
partysize = DB.Column(DB.Integer, default=1)
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
email = db.Column(db.String(120))
partysize = db.Column(db.Integer, default=1)

def __init__(self, name=None, email=None, partysize=1):
self.name = name
self.email = email
self.partysize = partysize

self.partysize = partysize or 1
2 changes: 1 addition & 1 deletion app/templates/guest_registration.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>Guest Registration</h1>
<form action="/register" method="POST">
<div class="form-group">
<label for="namefield">Name</label>
<input type="text" class="form-control" id="namefield" name="name" aria-describedby="emailHelp" placeholder="Email">
<input type="text" class="form-control" id="namefield" name="name" aria-describedby="emailHelp" placeholder="Name">
<small id="emailHelp" class="form-text text-muted">Let us know who you are.</small>
</div>
<div class="form-group">
Expand Down

0 comments on commit 2491a96

Please sign in to comment.