From 4dad84074bdcba5ec23d25c1f9f1bfdc42eb6417 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Fri, 6 Dec 2013 17:30:12 -0600 Subject: [PATCH] Use Bcrypt for password hashing and refactor extension registration --- .../{{cookiecutter.repo_name}}/app.py | 18 +++++++----------- .../{{cookiecutter.repo_name}}/assets.py | 11 ++++++++--- .../{{cookiecutter.repo_name}}/extensions.py | 5 +++++ .../templates/_layouts/base.html | 4 ++-- .../{{cookiecutter.repo_name}}/user/models.py | 15 +++++++++------ 5 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py index a4e8637..92c9773 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py @@ -1,16 +1,13 @@ # -*- coding: utf-8 -*- -import os from flask import Flask -from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.assets import Environment from webassets.loaders import PythonLoader from {{ cookiecutter.repo_name }}.settings import ProdConfig -from {{cookiecutter.repo_name}} import assets +from {{cookiecutter.repo_name}}.assets import assets from {{cookiecutter.repo_name}}.database import db from {{cookiecutter.repo_name}} import public, user -assets_env = Environment() def create_app(config_object=ProdConfig): '''An application factory, as explained here: @@ -20,17 +17,16 @@ def create_app(config_object=ProdConfig): ''' app = Flask(__name__) app.config.from_object(config_object) - # Initialize SQLAlchemy - db.init_app(app) - # Register asset bundles - assets_env.init_app(app) - assets_loader = PythonLoader(assets) - for name, bundle in assets_loader.load_bundles().iteritems(): - assets_env.register(name, bundle) + register_extensions(app) register_blueprints(app) return app +def register_extensions(app): + db.init_app(app) + assets.init_app(app) + + def register_blueprints(app): # Register blueprints app.register_blueprint(public.views.blueprint) diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/assets.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/assets.py index f751629..67d5ec3 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/assets.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/assets.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- -from flask.ext.assets import Bundle +from flask.ext.assets import Bundle, Environment -common_css = Bundle( +css = Bundle( "libs/bootstrap3/css/bootstrap.min.css", "css/style.css", filters="cssmin", output="public/css/common.css" ) -common_js = Bundle( +js = Bundle( "libs/jquery2/jquery-2.0.3.min.js", "libs/bootstrap3/js/bootstrap.min.js", "js/plugins.js", @@ -18,3 +18,8 @@ common_js = Bundle( ), output="public/js/common.js" ) + +assets = Environment() + +assets.register("js_all", js) +assets.register("css_all", css) diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py new file mode 100644 index 0000000..3cff6d4 --- /dev/null +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/extensions.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +"""Extensions module.""" + +from flask.ext.bcrypt import Bcrypt +bcrypt = Bcrypt() diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/base.html b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/base.html index 036a6a1..0ffe851 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/base.html +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/_layouts/base.html @@ -20,7 +20,7 @@ - {% assets "common_css" %} + {% assets "css_all" %} {% endassets %} @@ -60,7 +60,7 @@ {% include "_layouts/footer.html" %} -{% assets "common_js" %} +{% assets "js_all" %} {% endassets %} {% block js %}{% endblock %} diff --git a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py index c320e7f..fbf36a3 100644 --- a/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py +++ b/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- -from passlib.apps import custom_app_context as pwd_context +import datetime as dt + from {{cookiecutter.repo_name}}.database import db +from {{cookiecutter.repo_name}}.extensions import bcrypt class User(db.Model): @@ -10,18 +12,19 @@ class User(db.Model): username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(80), unique=True, nullable=False) password_hash = db.Column(db.String, nullable=False) + created_at = db.Column(db.DateTime(), nullable=False) - def __init__(self, username=None, email=None, password=None): + def __init__(self, username, email, password): self.username = username self.email = email - if password: - self.set_password(password) + self.set_password(password) + self.created_at = dt.datetime.utcnow() def set_password(self, password): - self.password_hash = pwd_context.encrypt(password) + self.password_hash = bcrypt.generate_password_hash(password) def check_password(self, password): - return pwd_context.verify(password, self.password_hash) + return bcrypt.check_password_hash(self.password_hash, password) def __repr__(self): return ''.format(username=self.username)