Skip to content

Commit

Permalink
updated requirements, added app factory pattern, linted
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Nov 18, 2017
1 parent 13c6095 commit 62403ed
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 124 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ venv
__pycache__

*.pyc
*.sqlite
*.db
*.coverage
.DS_Store
env.sh
migrations
htmlcov
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.0
3.6.3
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2016 Michael Herman
Copyright (c) 2017 Michael Herman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $ python manage.py create_data
$ python manage.py runserver
```

So access the application at the address [http://localhost:5000/](http://localhost:5000/)
Access the application at the address [http://localhost:5000/](http://localhost:5000/)

> Want to specify a different port?
Expand Down
10 changes: 5 additions & 5 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# manage.py


import os
import unittest
import coverage

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from project.server import create_app, db
from project.server.models import User

# code coverage
COV = coverage.coverage(
branch=True,
include='project/*',
Expand All @@ -19,10 +22,7 @@
)
COV.start()

from project.server import app, db
from project.server.models import User


app = create_app()
migrate = Migrate(app, db)
manager = Manager(app)

Expand Down
143 changes: 66 additions & 77 deletions project/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# project/server/__init__.py


#################
#### imports ####
#################

import os

from flask import Flask, render_template
Expand All @@ -13,79 +9,72 @@
from flask_debugtoolbar import DebugToolbarExtension
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate


################
#### config ####
################

app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)


app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)


####################
#### extensions ####
####################

# instantiate the extensions
login_manager = LoginManager()
login_manager.init_app(app)
bcrypt = Bcrypt(app)
toolbar = DebugToolbarExtension(app)
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)


###################
### blueprints ####
###################

from project.server.user.views import user_blueprint
from project.server.main.views import main_blueprint
app.register_blueprint(user_blueprint)
app.register_blueprint(main_blueprint)


###################
### flask-login ####
###################

from project.server.models import User

login_manager.login_view = "user.login"
login_manager.login_message_category = 'danger'


@login_manager.user_loader
def load_user(user_id):
return User.query.filter(User.id == int(user_id)).first()


########################
#### error handlers ####
########################

@app.errorhandler(401)
def unauthorized_page(error):
return render_template("errors/401.html"), 401


@app.errorhandler(403)
def forbidden_page(error):
return render_template("errors/403.html"), 403


@app.errorhandler(404)
def page_not_found(error):
return render_template("errors/404.html"), 404


@app.errorhandler(500)
def server_error_page(error):
return render_template("errors/500.html"), 500
bcrypt = Bcrypt()
toolbar = DebugToolbarExtension()
bootstrap = Bootstrap()
db = SQLAlchemy()
migrate = Migrate()


def create_app():

# instantiate the app
app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)

# set config
app_settings = os.getenv(
'APP_SETTINGS', 'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)

# set up extensions
login_manager.init_app(app)
bcrypt.init_app(app)
toolbar.init_app(app)
bootstrap.init_app(app)
db.init_app(app)
migrate.init_app(app, db)

# register blueprints
from project.server.user.views import user_blueprint
from project.server.main.views import main_blueprint
app.register_blueprint(user_blueprint)
app.register_blueprint(main_blueprint)

# flask login
from project.server.models import User
login_manager.login_view = 'user.login'
login_manager.login_message_category = 'danger'

@login_manager.user_loader
def load_user(user_id):
return User.query.filter(User.id == int(user_id)).first()

return app

# error handlers
@app.errorhandler(401)
def unauthorized_page(error):
return render_template('errors/401.html'), 401

@app.errorhandler(403)
def forbidden_page(error):
return render_template('errors/403.html'), 403

@app.errorhandler(404)
def page_not_found(error):
return render_template('errors/404.html'), 404

@app.errorhandler(500)
def server_error_page(error):
return render_template('errors/500.html'), 500

return app
3 changes: 2 additions & 1 deletion project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class DevelopmentConfig(BaseConfig):
DEBUG = True
BCRYPT_LOG_ROUNDS = 4
WTF_CSRF_ENABLED = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'dev.sqlite')
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(
os.path.join(basedir, 'dev.db'))
DEBUG_TB_ENABLED = True


Expand Down
13 changes: 0 additions & 13 deletions project/server/main/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
# project/server/main/views.py


#################
#### imports ####
#################

from flask import render_template, Blueprint


################
#### config ####
################

main_blueprint = Blueprint('main', __name__,)


################
#### routes ####
################


@main_blueprint.route('/')
def home():
return render_template('main/home.html')
Expand Down
8 changes: 5 additions & 3 deletions project/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

import datetime

from project.server import app, db, bcrypt
from flask import current_app

from project.server import db, bcrypt


class User(db.Model):

__tablename__ = "users"
__tablename__ = 'users'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(255), unique=True, nullable=False)
Expand All @@ -19,7 +21,7 @@ class User(db.Model):
def __init__(self, email, password, admin=False):
self.email = email
self.password = bcrypt.generate_password_hash(
password, app.config.get('BCRYPT_LOG_ROUNDS')
password, current_app.config.get('BCRYPT_LOG_ROUNDS')
).decode('utf-8')
self.registered_on = datetime.datetime.now()
self.admin = admin
Expand Down
11 changes: 0 additions & 11 deletions project/server/user/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# project/server/user/views.py


#################
#### imports ####
#################

from flask import render_template, Blueprint, url_for, \
redirect, flash, request
from flask_login import login_user, logout_user, login_required
Expand All @@ -13,17 +9,10 @@
from project.server.models import User
from project.server.user.forms import LoginForm, RegisterForm

################
#### config ####
################

user_blueprint = Blueprint('user', __name__,)


################
#### routes ####
################

@user_blueprint.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
Expand Down
4 changes: 3 additions & 1 deletion project/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

from flask_testing import TestCase

from project.server import app, db
from project.server import db, create_app
from project.server.models import User

app = create_app()


class BaseTestCase(TestCase):

Expand Down
4 changes: 3 additions & 1 deletion project/tests/test__config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from flask import current_app
from flask_testing import TestCase

from project.server import app
from project.server import create_app

app = create_app()


class TestDevelopmentConfig(TestCase):
Expand Down
3 changes: 2 additions & 1 deletion project/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def test_registered_on_defaults_to_datetime(self):
def test_check_password(self):
# Ensure given password is correct after unhashing.
user = User.query.filter_by(email='ad@min.com').first()
self.assertTrue(bcrypt.check_password_hash(user.password, 'admin_user'))
self.assertTrue(
bcrypt.check_password_hash(user.password, 'admin_user'))
self.assertFalse(bcrypt.check_password_hash(user.password, 'foobar'))

def test_validate_invalid_password(self):
Expand Down
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
coverage==4.3.4
Flask==0.12
coverage==4.4.2
Flask==0.12.2
Flask-Bcrypt==0.7.1
Flask-Bootstrap==3.3.7.1
Flask-DebugToolbar==0.10.0
Flask-DebugToolbar==0.10.1
Flask-Login==0.4.0
Flask-Migrate==2.0.3
Flask-Script==2.0.5
Flask-SQLAlchemy==2.1
Flask-Testing==0.6.1
Flask-Migrate==2.1.1
Flask-Script==2.0.6
Flask-SQLAlchemy==2.3.2
Flask-Testing==0.6.2
Flask-WTF==0.14.2

0 comments on commit 62403ed

Please sign in to comment.