modularity
+165 −120 · 23 files changed
-
app/__init__.py+31 −0 -
app/main/__init__.py+5 −0 -
app/main/routes.py+63 −0 -
app/models.py+47 −0 -
{templates => app/templates}/404.html+0 −0 -
{templates => app/templates}/base.html+0 −0 -
{templates => app/templates}/form.html+0 −0 -
{templates => app/templates}/form2.html+0 −0 -
{templates => app/templates}/index.html+0 −0 -
{templates => app/templates}/login.html+0 −0 -
{templates => app/templates}/logout.html+0 −0 -
{templates => app/templates}/macros.html+0 −0 -
{templates => app/templates}/other.html+0 −0 -
{templates => app/templates}/protected.html+0 −0 -
{templates => app/templates}/session.html+0 −0 -
{templates => app/templates}/sqla.html+0 −0 -
{templates => app/templates}/text.txt+0 −0 -
{templates => app/templates}/upload.html+0 −0 -
{templates => app/templates}/user.html+0 −0 -
{templates => app/templates}/weather-average.html+0 −0 -
config/config.py+7 −0 -
flask-blog.py+1 −120 -
run.py+11 −0
@@ -0,0 +1,31 @@ +import os +from flask import Flask +from flask.ext.bootsrap import Bootstrap +from flask.ext.sqlalchemy import SQLAlchemy +from flask.ext.login import LoginManager + +bootstrap = Bootstrap() +db = SQLAlchemy() +lm = LoginManager() +lm.login_view = 'main.login' + + +def create_app(config_name): + """Create an application instance""" + app = Flask(__name__) + + # import configuration + cfg = os.path.join(os.getcwd(), 'config', config_name + '.py') + app.config.from_pyfile(cfg) + + # initialize extensions + bootstrap.init_app(app) + db.init_app(app) + lm.init_app(app) + + # import blueprints + from .main import main as main_blueprint + + app.register_blueprint(main_blueprint) + + return app @@ -0,0 +1,5 @@ +from flask import Blueprint + +main = Blueprint('main', __name__) + +from . import routes @@ -0,0 +1,63 @@ +from flask import render_template, request, url_for, redirect +from flask.ext.login import login_required, login_user, logout_user +from ..models import User +from . import main +from .forms import LoginForm + +""" +POST request > wtf run all the validators and if +they validate they get in the if body +ser.query.filter_by(username=form.username.data) +--if user is None +1 or None (user is valid, user invalid return None +Never get more than one. Username is unique + +--user.verify_password(form.password.data): + we need to let the user try again, redirect to login page + a redirect to itself +--login_user(user, form.remember_me.data) + function that comes with flask.login, + login the user pass the user object and tell it if + we want it that it should be a user to be remembered +--return redirect(request.args.get('next') or url_for('index')) + redirect to home page or protected page while being logged out + intercepted and sent to the login +--request.args + python dictionary will have all the elements + next is used in arguments if user tried to access restricted page + if user will login ok then it will use the next parameter, which in + turn it will go to protected page +--args.get('next' + none > redirect to index page or protected page +-- +""" + + +@main.route('/login', methods=['GET', 'POST']) +def login(): + form = LoginForm() + if form.validate_on_submit(): + user = User.query.filter_by(username=form.username.data).first + if user is None or not user.verify_password(form.password.data): + return redirect(url_for('main.login', **request.args)) + login_user(user, form.remember_me.data) + return redirect(request.args.get('next') or url_for('main.index')) + return render_template('login.html', form=form) + + +@main.route('/logout') +@login_required +def logout(): + logout_user() + return redirect(url_for('main.index')) + + +@main.route('/protected') +@login_required +def protected(): + return render_template('protected.html') + + +@app.route('/') +def hello_world(): + return render_template('index.html') @@ -0,0 +1,47 @@ +from flask.ext.login import UserMixin +from werkzeug.security import generate_password_hash, check_password_hash +from . import db, lm + + + + +# Define a class that represents our users in the database +# This class inherits from a base model classes +# Models > Classes > Associated with entities within a database. +# The hardest part was finding the right SQLAlchemy data types and picking a +# length for our String fields in the database. Using our models is also extremely +# simple, thanks to the SQLAlchemy query syntax + +class User(UserMixin, db.Model): + """ + Inherit from a user mixin class. + Methods > or implement your own methods + """ + __tablename__ = 'users' + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(16), index=True, unique=True) + + username = db.Column(db.String(16), index=True, unique=True) + password_hash = db.Column(db.String(64)) + + def set_password(self, password): + self.password_hash = generate_password_hash(password) + + def verify_password(self, password): + return check_password_hash(self.password_hash, password) + + @staticmethod + def register(username, password): + user = User(username=username) + user.set_password(password) + db.session.add(user) + db.session.commit() + return user + + def __repr__(self): + return '<User {0}>'.format(self.username) + + +@lm.user_loader +def load_user(id): + return User.query.get(int(id)) @@ -0,0 +1,7 @@ +__author__ = 'ovidiucs' +import os + +DEBUG = True +SECRET_KEY = 'top secret' +SQLALHEMY_DATABASE_URI = 'sqlite:///' + os.path.join( + os.path.dirname(__file__), '../data-dev.sqlite3') @@ -1,6 +1,5 @@ import os import imghdr -from werkzeug.security import generate_password_hash, check_password_hash from datetime import datetime from flask import Flask, render_template, request, session, g, \ make_response, jsonify, url_for, redirect @@ -10,9 +9,6 @@ from wtforms import PasswordField, StringField, SubmitField, FileField, \ ValidationError, BooleanField from wtforms.validators import Required, Length from flask.ext.sqlalchemy import SQLAlchemy -from flask.ext.login import LoginManager, UserMixin, login_user, logout_user, \ - login_required - app = Flask(__name__) @@ -22,13 +18,10 @@ app = Flask(__name__) app.config['SECRET_KEY'] = 'top secret!' # where the db is app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.sqlite3' + db = SQLAlchemy(app) # initialize extension bootstrap = Bootstrap(app) -# login manager -lm = LoginManager(app) -lm.login_view = 'login' - class NameForm(Form): name = StringField('What is your name?', validators=[Required(), @@ -47,60 +40,12 @@ class UploadForm(Form): raise ValidationError('Invalid image format') -# Define a class that represents our users in the database -# This class inherits from a base model classes -# Models > Classes > Associated with entities within a database. -# The hardest part was finding the right SQLAlchemy data types and picking a -# length for our String fields in the database. Using our models is also extremely -# simple, thanks to the SQLAlchemy query syntax - -class User(UserMixin, db.Model): - """ - Inherit from a user mixin class. - Methods > or implement your own methods - """ - __tablename__ = 'users' - id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(16), index=True, unique=True) - - username = db.Column(db.String(16), index=True, unique=True) - password_hash = db.Column(db.String(64)) - - def set_password(self, password): - self.password_hash = generate_password_hash(password) - - def verify_password(self, password): - return check_password_hash(self.password_hash, password) - - @staticmethod - def register(username, password): - user = User(username=username) - user.set_password(password) - db.session.add(user) - db.session.commit() - return user - - def __repr__(self): - return '<User {0}>'.format(self.username) - - -@lm.user_loader -def load_user(id): - return User.query.get(int(id)) - - class LoginForm(Form): username = StringField('Username', validators=[Required(), Length(1, 16)]) password = PasswordField('Password', validators=[Required()]) remember_me = BooleanField('Remember Me') submit = SubmitField('Submit') - -@app.route('/') -def hello_world(): - return render_template('index.html') - - @app.route('/text') def text(): return render_template('text.txt'), 200, {'Content-Type': 'text/plain'} @@ -235,68 +180,6 @@ def sqla(): return render_template('sqla.html', form=form, name=name, new=new) -@lm.user_loader -def load_user(id): - return User.query.get(int(id)) - - -@app.route('/login', methods=['GET', 'POST']) -def login(): - form = LoginForm() - """ - POST request > wtf run all the validators and if - they validate they get in the if body - ser.query.filter_by(username=form.username.data) - --if user is None - 1 or None (user is valid, user invalid return None - Never get more than one. Username is unique - - --user.verify_password(form.password.data): - we need to let the user try again, redirect to login page - a redirect to itself - --login_user(user, form.remember_me.data) - function that comes with flask.login, - login the user pass the user object and tell it if - we want it that it should be a user to be remembered - --return redirect(request.args.get('next') or url_for('index')) - redirect to home page or protected page while being logged out - intercepted and sent to the login - --request.args - python dictionary will have all the elements - next is used in arguments if user tried to access restricted page - if user will login ok then it will use the next parameter, which in - turn it will go to protected page - --args.get('next' - none > redirect to index page or protected page - -- - """ - if form.validate_on_submit(): - user = User.query.filter_by(username=form.username.data).first() - if user is None or not user.verify_password(form.password.data): - return redirect(url_for('login'), **request.args) - login_user(user, form.remember_me.data) - return redirect(request.args.get('next') or url_for('login')) - return render_template('login.html', form=form) - - -""" -This route needs to be protected -""" - - -@app.route('/logout') -@login_required -def logout(): - logout_user() - return redirect(url_for('index')) - - -@app.route('/protected') -@login_required -def protected(): - return render_template('protected.html') - - @app.errorhandler(404) def not_found(e): return render_template('404.html') @@ -304,6 +187,4 @@ def not_found(e): if __name__ == '__main__': db.create_all() - if User.query.filter_by(username='john').first() is None: - User.register('john', 'cat') app.run(debug=True) @@ -0,0 +1,11 @@ +#!/usr/bin/env python +from app import create_app, db +from app.models import User + +if __name__ == '__main__': + app = create_app('development') + with app.app_context(): + db.create_all() + if User.query.filter_by(username='john').first() is None: + User.register('john', 'cat') + app.run()