This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up the sample flask app and PEP8ify the code to standards
- Loading branch information
Showing
3 changed files
with
33 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters