Skip to content

Commit 07a9ec5

Browse files
committed
updated requirements, added app factory pattern, linted
1 parent 13c6095 commit 07a9ec5

File tree

14 files changed

+97
-123
lines changed

14 files changed

+97
-123
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ venv
44
__pycache__
55

66
*.pyc
7-
*.sqlite
7+
*.db
88
*.coverage
99
.DS_Store
1010
env.sh
1111
migrations
12+
htmlcov

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.6.0
1+
3.6.3

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2016 Michael Herman
2+
Copyright (c) 2017 Michael Herman
33

44
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:
55

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $ python manage.py create_data
4141
$ python manage.py runserver
4242
```
4343

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

4646
> Want to specify a different port?
4747

manage.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from flask_script import Manager
99
from flask_migrate import Migrate, MigrateCommand
1010

11+
from project.server import create_app, db
12+
from project.server.models import User
13+
14+
# code coverage
1115
COV = coverage.coverage(
1216
branch=True,
1317
include='project/*',
@@ -19,10 +23,7 @@
1923
)
2024
COV.start()
2125

22-
from project.server import app, db
23-
from project.server.models import User
24-
25-
26+
app = create_app()
2627
migrate = Migrate(app, db)
2728
manager = Manager(app)
2829

project/server/__init__.py

Lines changed: 66 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# project/server/__init__.py
22

33

4-
#################
5-
#### imports ####
6-
#################
7-
84
import os
95

106
from flask import Flask, render_template
@@ -13,79 +9,72 @@
139
from flask_debugtoolbar import DebugToolbarExtension
1410
from flask_bootstrap import Bootstrap
1511
from flask_sqlalchemy import SQLAlchemy
12+
from flask_migrate import Migrate
1613

1714

18-
################
19-
#### config ####
20-
################
21-
22-
app = Flask(
23-
__name__,
24-
template_folder='../client/templates',
25-
static_folder='../client/static'
26-
)
27-
28-
29-
app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig')
30-
app.config.from_object(app_settings)
31-
32-
33-
####################
34-
#### extensions ####
35-
####################
36-
15+
# instantiate the extensions
3716
login_manager = LoginManager()
38-
login_manager.init_app(app)
39-
bcrypt = Bcrypt(app)
40-
toolbar = DebugToolbarExtension(app)
41-
bootstrap = Bootstrap(app)
42-
db = SQLAlchemy(app)
43-
44-
45-
###################
46-
### blueprints ####
47-
###################
48-
49-
from project.server.user.views import user_blueprint
50-
from project.server.main.views import main_blueprint
51-
app.register_blueprint(user_blueprint)
52-
app.register_blueprint(main_blueprint)
53-
54-
55-
###################
56-
### flask-login ####
57-
###################
58-
59-
from project.server.models import User
60-
61-
login_manager.login_view = "user.login"
62-
login_manager.login_message_category = 'danger'
63-
64-
65-
@login_manager.user_loader
66-
def load_user(user_id):
67-
return User.query.filter(User.id == int(user_id)).first()
68-
69-
70-
########################
71-
#### error handlers ####
72-
########################
73-
74-
@app.errorhandler(401)
75-
def unauthorized_page(error):
76-
return render_template("errors/401.html"), 401
77-
78-
79-
@app.errorhandler(403)
80-
def forbidden_page(error):
81-
return render_template("errors/403.html"), 403
82-
83-
84-
@app.errorhandler(404)
85-
def page_not_found(error):
86-
return render_template("errors/404.html"), 404
87-
88-
89-
@app.errorhandler(500)
90-
def server_error_page(error):
91-
return render_template("errors/500.html"), 500
17+
bcrypt = Bcrypt()
18+
toolbar = DebugToolbarExtension()
19+
bootstrap = Bootstrap()
20+
db = SQLAlchemy()
21+
migrate = Migrate()
22+
23+
24+
def create_app():
25+
26+
# instantiate the app
27+
app = Flask(
28+
__name__,
29+
template_folder='../client/templates',
30+
static_folder='../client/static'
31+
)
32+
33+
# set config
34+
app_settings = os.getenv(
35+
'APP_SETTINGS', 'project.server.config.DevelopmentConfig')
36+
app.config.from_object(app_settings)
37+
38+
# set up extensions
39+
login_manager.init_app(app)
40+
bcrypt.init_app(app)
41+
toolbar.init_app(app)
42+
bootstrap.init_app(app)
43+
db.init_app(app)
44+
migrate.init_app(app, db)
45+
46+
# register blueprints
47+
from project.server.user.views import user_blueprint
48+
from project.server.main.views import main_blueprint
49+
app.register_blueprint(user_blueprint)
50+
app.register_blueprint(main_blueprint)
51+
52+
# flask login
53+
from project.server.models import User
54+
login_manager.login_view = 'user.login'
55+
login_manager.login_message_category = 'danger'
56+
57+
@login_manager.user_loader
58+
def load_user(user_id):
59+
return User.query.filter(User.id == int(user_id)).first()
60+
61+
return app
62+
63+
# error handlers
64+
@app.errorhandler(401)
65+
def unauthorized_page(error):
66+
return render_template('errors/401.html'), 401
67+
68+
@app.errorhandler(403)
69+
def forbidden_page(error):
70+
return render_template('errors/403.html'), 403
71+
72+
@app.errorhandler(404)
73+
def page_not_found(error):
74+
return render_template('errors/404.html'), 404
75+
76+
@app.errorhandler(500)
77+
def server_error_page(error):
78+
return render_template('errors/500.html'), 500
79+
80+
return app

project/server/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DevelopmentConfig(BaseConfig):
2020
DEBUG = True
2121
BCRYPT_LOG_ROUNDS = 4
2222
WTF_CSRF_ENABLED = False
23-
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'dev.sqlite')
23+
SQLALCHEMY_DATABASE_URI = f'sqlite:///{os.path.join(basedir, "dev.db")}'
2424
DEBUG_TB_ENABLED = True
2525

2626

project/server/main/views.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
# project/server/main/views.py
22

33

4-
#################
5-
#### imports ####
6-
#################
7-
84
from flask import render_template, Blueprint
95

106

11-
################
12-
#### config ####
13-
################
14-
157
main_blueprint = Blueprint('main', __name__,)
168

179

18-
################
19-
#### routes ####
20-
################
21-
22-
2310
@main_blueprint.route('/')
2411
def home():
2512
return render_template('main/home.html')

project/server/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
import datetime
55

6-
from project.server import app, db, bcrypt
6+
from flask import current_app
7+
8+
from project.server import db, bcrypt
79

810

911
class User(db.Model):
1012

11-
__tablename__ = "users"
13+
__tablename__ = 'users'
1214

1315
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
1416
email = db.Column(db.String(255), unique=True, nullable=False)
@@ -19,7 +21,7 @@ class User(db.Model):
1921
def __init__(self, email, password, admin=False):
2022
self.email = email
2123
self.password = bcrypt.generate_password_hash(
22-
password, app.config.get('BCRYPT_LOG_ROUNDS')
24+
password, current_app.config.get('BCRYPT_LOG_ROUNDS')
2325
).decode('utf-8')
2426
self.registered_on = datetime.datetime.now()
2527
self.admin = admin

project/server/user/views.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# project/server/user/views.py
22

33

4-
#################
5-
#### imports ####
6-
#################
7-
84
from flask import render_template, Blueprint, url_for, \
95
redirect, flash, request
106
from flask_login import login_user, logout_user, login_required
@@ -13,17 +9,10 @@
139
from project.server.models import User
1410
from project.server.user.forms import LoginForm, RegisterForm
1511

16-
################
17-
#### config ####
18-
################
1912

2013
user_blueprint = Blueprint('user', __name__,)
2114

2215

23-
################
24-
#### routes ####
25-
################
26-
2716
@user_blueprint.route('/register', methods=['GET', 'POST'])
2817
def register():
2918
form = RegisterForm(request.form)

0 commit comments

Comments
 (0)