diff --git a/journalmarks/__init__.py b/journalmarks/__init__.py index f320bc1..778a2e0 100644 --- a/journalmarks/__init__.py +++ b/journalmarks/__init__.py @@ -1 +1 @@ -from .journalmarks import app +from .journalmarks import create_app diff --git a/journalmarks/journalmarks.py b/journalmarks/journalmarks.py index edb4793..a997681 100644 --- a/journalmarks/journalmarks.py +++ b/journalmarks/journalmarks.py @@ -7,17 +7,17 @@ import hashlib import datetime import functools from urllib.parse import urlparse -from flask import Flask, request, session, render_template, url_for, redirect +from flask import Flask, request, session, render_template, url_for, \ + redirect, Blueprint from flask import g as flask_g from peewee import CharField, DateTimeField, ForeignKeyField, DoesNotExist from playhouse.flask_utils import FlaskDB from passlib.hash import pbkdf2_sha256 -app = Flask(__name__) -app.config.from_envvar('JOURNALMARKS_SETTINGS', silent=True) -app.secret_key = app.config['SECRET_KEY'] -db_wrapper = FlaskDB(app) +journalmarks = Blueprint('journalmarks', __name__, template_folder='templates', + static_folder='static') +db_wrapper = FlaskDB() class User(db_wrapper.Model): @@ -41,7 +41,7 @@ def login_required(f): @functools.wraps(f) def decorated_function(*args, **kwargs): if 'username' not in session: - return redirect(url_for('login', next=request.path)) + return redirect(url_for('journalmarks.login', next=request.path)) try: u = User.select().where(User.username == session['username']).get() except DoesNotExist: @@ -51,12 +51,7 @@ def login_required(f): return decorated_function -@app.cli.command('initdb') -def initdb_command(): - db_wrapper.database.create_tables([User, AccessToken, Journalmark]) - - -@app.route('/register', methods=['POST']) +@journalmarks.route('/register', methods=['POST']) def register(): if len(request.form) != 3: return ('wrong number of fields', 400, None) @@ -93,16 +88,16 @@ def register(): return json.dumps('ok') -@app.route('/login', methods=['GET']) +@journalmarks.route('/login', methods=['GET']) def show_login(): if 'next' in request.args and urlparse(request.args['next']).netloc == '': next = request.args['next'] else: - next = url_for('index') + next = url_for('journalmarks.journalmarks.index') return render_template('login.html', next=next) -@app.route('/login', methods=['POST']) +@journalmarks.route('/login', methods=['POST']) def login(): print(request.json) if len(request.json) != 2: @@ -129,20 +124,20 @@ def login(): return json.dumps('ok') -@app.route('/logout') +@journalmarks.route('/logout') @login_required def logout(): del session['username'] return render_template('logout.html') -@app.route('/') +@journalmarks.route('/') @login_required def index(): return render_template('index.html') -@app.route('/create', methods=['POST']) +@journalmarks.route('/create', methods=['POST']) @login_required def create(): if len(request.json) != 1 or 'content' not in request.json: @@ -160,13 +155,13 @@ def create(): return json.dumps(tag) -@app.route('/overview', methods=['GET']) +@journalmarks.route('/overview', methods=['GET']) @login_required def overview(): return render_template('overview.html') -@app.route('/overview', methods=['POST']) +@journalmarks.route('/overview', methods=['POST']) @login_required def overview_get_journalmarks(): jms = Journalmark.select().where(Journalmark.user == flask_g.user) \ @@ -181,7 +176,7 @@ def overview_get_journalmarks(): return json.dumps(ret) -@app.route('/') +@journalmarks.route('/') @login_required def get_journalmark(tag): print(tag) @@ -192,3 +187,12 @@ def get_journalmark(tag): except DoesNotExist: return ('tag not found', 404, None) return render_template('get_journalmark.html', j=j) + + +def create_app(config_filename): + app = Flask(__name__) + app.register_blueprint(journalmarks) + app.config.from_pyfile(config_filename) + app.secret_key = app.config['SECRET_KEY'] + db_wrapper.init_app(app) + return app diff --git a/journalmarks/templates/base.html b/journalmarks/templates/base.html index 377df57..11b923a 100644 --- a/journalmarks/templates/base.html +++ b/journalmarks/templates/base.html @@ -1,7 +1,7 @@ Journalmarks - - - + + + {% block script %}{% endblock %} {% block body %}{% endblock %} diff --git a/journalmarks/templates/get_journalmark.html b/journalmarks/templates/get_journalmark.html index f41bff6..f17708a 100644 --- a/journalmarks/templates/get_journalmark.html +++ b/journalmarks/templates/get_journalmark.html @@ -11,7 +11,7 @@ function run() { }); }).catch(function() { deleteKey(); - window.location.href = '{{ url_for('login') }}'; + window.location.href = '{{ url_for('journalmarks.login') }}'; }); } diff --git a/journalmarks/templates/index.html b/journalmarks/templates/index.html index 8f14853..4270e85 100644 --- a/journalmarks/templates/index.html +++ b/journalmarks/templates/index.html @@ -13,14 +13,14 @@ function isURL(str) { function run() { journalmarks_loadkey().catch(function() { deleteKey(); - window.location.href = '{{ url_for('login') }}'; + window.location.href = '{{ url_for('journalmarks.login') }}'; }); document.getElementById('create').addEventListener('click', function() { var url = document.getElementById('url').value; if(isURL(url)) { journalmarks_encrypturl(url).then(function (encurl) { - return post_object('{{ url_for('create') }}', {content: encurl}); + return post_object('{{ url_for('journalmarks.create') }}', {content: encurl}); }).then(function(tag) { console.log('ok', tag); }); @@ -47,8 +47,8 @@ else document.attachEvent('onreadystatechange', function(){ {% block body %} Welcome!

-overview -logout +overview +logout

diff --git a/journalmarks/templates/login.html b/journalmarks/templates/login.html index d2be261..3637a69 100644 --- a/journalmarks/templates/login.html +++ b/journalmarks/templates/login.html @@ -8,7 +8,7 @@ function run() { sha256(password).then(function(pwhash) { var credentials = {username: username, password_hash: pwhash} console.log(username, password, credentials); - post_object('{{ url_for('login') }}', credentials) + post_object('{{ url_for('journalmarks.login') }}', credentials) .then(journalmarks_initkey(username, password)).then(function() { window.location.href = '{{ next }}'; }).catch(function(error) { diff --git a/journalmarks/templates/overview.html b/journalmarks/templates/overview.html index 1372bf3..5c8e3b3 100644 --- a/journalmarks/templates/overview.html +++ b/journalmarks/templates/overview.html @@ -11,7 +11,7 @@ function run() { }); */ journalmarks_loadkey().then(function() { - return post_object('{{ url_for('overview_get_journalmarks') }}', {}); + return post_object('{{ url_for('journalmarks.overview_get_journalmarks') }}', {}); }).then(function(jms) { console.log(jms); var decrs = []; @@ -36,7 +36,7 @@ function run() { }).catch(function(error) { console.log(error); //deleteKey(); - //window.location.href = '{{ url_for('login') }}'; + //window.location.href = '{{ url_for('journalmarks.login') }}'; }); }