parent
34c6faf630
commit
2986743a8c
@ -0,0 +1 @@ |
||||
"""Tests for the app.""" |
@ -1,23 +1,31 @@ |
||||
# -*- coding: utf-8 -*- |
||||
from factory import Sequence, PostGenerationMethodCall |
||||
"""Factories to help in tests.""" |
||||
from factory import PostGenerationMethodCall, Sequence |
||||
from factory.alchemy import SQLAlchemyModelFactory |
||||
|
||||
from {{cookiecutter.app_name}}.user.models import User |
||||
from {{cookiecutter.app_name}}.database import db |
||||
from {{cookiecutter.app_name}}.user.models import User |
||||
|
||||
|
||||
class BaseFactory(SQLAlchemyModelFactory): |
||||
"""Base factory.""" |
||||
|
||||
class Meta: |
||||
"""Factory configuration.""" |
||||
|
||||
abstract = True |
||||
sqlalchemy_session = db.session |
||||
|
||||
|
||||
class UserFactory(BaseFactory): |
||||
username = Sequence(lambda n: "user{0}".format(n)) |
||||
email = Sequence(lambda n: "user{0}@example.com".format(n)) |
||||
"""User factory.""" |
||||
|
||||
username = Sequence(lambda n: 'user{0}'.format(n)) |
||||
email = Sequence(lambda n: 'user{0}@example.com'.format(n)) |
||||
password = PostGenerationMethodCall('set_password', 'example') |
||||
active = True |
||||
|
||||
class Meta: |
||||
"""Factory configuration.""" |
||||
|
||||
model = User |
||||
|
@ -0,0 +1 @@ |
||||
"""Main application package.""" |
@ -1,22 +1,23 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""Application assets.""" |
||||
from flask_assets import Bundle, Environment |
||||
|
||||
css = Bundle( |
||||
"libs/bootstrap/dist/css/bootstrap.css", |
||||
"css/style.css", |
||||
filters="cssmin", |
||||
output="public/css/common.css" |
||||
'libs/bootstrap/dist/css/bootstrap.css', |
||||
'css/style.css', |
||||
filters='cssmin', |
||||
output='public/css/common.css' |
||||
) |
||||
|
||||
js = Bundle( |
||||
"libs/jQuery/dist/jquery.js", |
||||
"libs/bootstrap/dist/js/bootstrap.js", |
||||
"js/plugins.js", |
||||
'libs/jQuery/dist/jquery.js', |
||||
'libs/bootstrap/dist/js/bootstrap.js', |
||||
'js/plugins.js', |
||||
filters='jsmin', |
||||
output="public/js/common.js" |
||||
output='public/js/common.js' |
||||
) |
||||
|
||||
assets = Environment() |
||||
|
||||
assets.register("js_all", js) |
||||
assets.register("css_all", css) |
||||
assets.register('js_all', js) |
||||
assets.register('css_all', css) |
||||
|
@ -1,22 +1,16 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""Extensions module. Each extension is initialized in the app factory located |
||||
in app.py |
||||
""" |
||||
"""Extensions module. Each extension is initialized in the app factory located in app.py.""" |
||||
|
||||
from flask_bcrypt import Bcrypt |
||||
bcrypt = Bcrypt() |
||||
|
||||
from flask_cache import Cache |
||||
from flask_debugtoolbar import DebugToolbarExtension |
||||
from flask_login import LoginManager |
||||
login_manager = LoginManager() |
||||
|
||||
from flask_migrate import Migrate |
||||
from flask_sqlalchemy import SQLAlchemy |
||||
db = SQLAlchemy() |
||||
|
||||
from flask_migrate import Migrate |
||||
bcrypt = Bcrypt() |
||||
login_manager = LoginManager() |
||||
db = SQLAlchemy() |
||||
migrate = Migrate() |
||||
|
||||
from flask_cache import Cache |
||||
cache = Cache() |
||||
|
||||
from flask_debugtoolbar import DebugToolbarExtension |
||||
debug_toolbar = DebugToolbarExtension() |
||||
|
@ -1,4 +1,3 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""The public module, including the homepage and user auth.""" |
||||
|
||||
from . import views |
||||
from . import views # noqa |
||||
|
@ -1,63 +1,63 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""Public section, including homepage and signup.""" |
||||
from flask import (Blueprint, request, render_template, flash, url_for, |
||||
redirect, session) |
||||
from flask_login import login_user, login_required, logout_user |
||||
from flask import Blueprint, flash, redirect, render_template, request, url_for |
||||
from flask_login import login_required, login_user, logout_user |
||||
|
||||
from {{cookiecutter.app_name}}.extensions import login_manager |
||||
from {{cookiecutter.app_name}}.user.models import User |
||||
from {{cookiecutter.app_name}}.public.forms import LoginForm |
||||
from {{cookiecutter.app_name}}.user.forms import RegisterForm |
||||
from {{cookiecutter.app_name}}.user.models import User |
||||
from {{cookiecutter.app_name}}.utils import flash_errors |
||||
from {{cookiecutter.app_name}}.database import db |
||||
|
||||
blueprint = Blueprint('public', __name__, static_folder="../static") |
||||
blueprint = Blueprint('public', __name__, static_folder='../static') |
||||
|
||||
|
||||
@login_manager.user_loader |
||||
def load_user(id): |
||||
"""Load user by ID.""" |
||||
return User.get_by_id(int(id)) |
||||
|
||||
|
||||
@blueprint.route("/", methods=["GET", "POST"]) |
||||
@blueprint.route('/', methods=['GET', 'POST']) |
||||
def home(): |
||||
"""Home page.""" |
||||
form = LoginForm(request.form) |
||||
# Handle logging in |
||||
if request.method == 'POST': |
||||
if form.validate_on_submit(): |
||||
login_user(form.user) |
||||
flash("You are logged in.", 'success') |
||||
redirect_url = request.args.get("next") or url_for("user.members") |
||||
flash('You are logged in.', 'success') |
||||
redirect_url = request.args.get('next') or url_for('user.members') |
||||
return redirect(redirect_url) |
||||
else: |
||||
flash_errors(form) |
||||
return render_template("public/home.html", form=form) |
||||
return render_template('public/home.html', form=form) |
||||
|
||||
|
||||
@blueprint.route('/logout/') |
||||
@login_required |
||||
def logout(): |
||||
"""Logout.""" |
||||
logout_user() |
||||
flash('You are logged out.', 'info') |
||||
return redirect(url_for('public.home')) |
||||
|
||||
|
||||
@blueprint.route("/register/", methods=['GET', 'POST']) |
||||
@blueprint.route('/register/', methods=['GET', 'POST']) |
||||
def register(): |
||||
"""Register new user.""" |
||||
form = RegisterForm(request.form, csrf_enabled=False) |
||||
if form.validate_on_submit(): |
||||
new_user = User.create(username=form.username.data, |
||||
email=form.email.data, |
||||
password=form.password.data, |
||||
active=True) |
||||
flash("Thank you for registering. You can now log in.", 'success') |
||||
User.create(username=form.username.data, email=form.email.data, password=form.password.data, active=True) |
||||
flash('Thank you for registering. You can now log in.', 'success') |
||||
return redirect(url_for('public.home')) |
||||
else: |
||||
flash_errors(form) |
||||
return render_template('public/register.html', form=form) |
||||
|
||||
|
||||
@blueprint.route("/about/") |
||||
@blueprint.route('/about/') |
||||
def about(): |
||||
"""About page.""" |
||||
form = LoginForm(request.form) |
||||
return render_template("public/about.html", form=form) |
||||
return render_template('public/about.html', form=form) |
||||
|
@ -1,3 +1,3 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""The user module.""" |
||||
from . import views |
||||
from . import views # noqa |
||||
|
@ -1,12 +1,13 @@ |
||||
# -*- coding: utf-8 -*- |
||||
"""User views.""" |
||||
from flask import Blueprint, render_template |
||||
from flask_login import login_required |
||||
|
||||
blueprint = Blueprint("user", __name__, url_prefix='/users', |
||||
static_folder="../static") |
||||
blueprint = Blueprint('user', __name__, url_prefix='/users', static_folder='../static') |
||||
|
||||
|
||||
@blueprint.route("/") |
||||
@blueprint.route('/') |
||||
@login_required |
||||
def members(): |
||||
return render_template("users/members.html") |
||||
"""List members.""" |
||||
return render_template('users/members.html') |
||||
|
Loading…
Reference in new issue