Browse Source

switched to following the app factory pattern

master
Fr3deric 7 years ago
parent
commit
d69dcd54da
  1. 2
      journalmarks/__init__.py
  2. 46
      journalmarks/journalmarks.py
  3. 6
      journalmarks/templates/base.html
  4. 2
      journalmarks/templates/get_journalmark.html
  5. 8
      journalmarks/templates/index.html
  6. 2
      journalmarks/templates/login.html
  7. 4
      journalmarks/templates/overview.html

2
journalmarks/__init__.py

@ -1 +1 @@
from .journalmarks import app from .journalmarks import create_app

46
journalmarks/journalmarks.py

@ -7,17 +7,17 @@ import hashlib
import datetime import datetime
import functools import functools
from urllib.parse import urlparse 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 flask import g as flask_g
from peewee import CharField, DateTimeField, ForeignKeyField, DoesNotExist from peewee import CharField, DateTimeField, ForeignKeyField, DoesNotExist
from playhouse.flask_utils import FlaskDB from playhouse.flask_utils import FlaskDB
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
app = Flask(__name__) journalmarks = Blueprint('journalmarks', __name__, template_folder='templates',
app.config.from_envvar('JOURNALMARKS_SETTINGS', silent=True) static_folder='static')
app.secret_key = app.config['SECRET_KEY'] db_wrapper = FlaskDB()
db_wrapper = FlaskDB(app)
class User(db_wrapper.Model): class User(db_wrapper.Model):
@ -41,7 +41,7 @@ def login_required(f):
@functools.wraps(f) @functools.wraps(f)
def decorated_function(*args, **kwargs): def decorated_function(*args, **kwargs):
if 'username' not in session: if 'username' not in session:
return redirect(url_for('login', next=request.path)) return redirect(url_for('journalmarks.login', next=request.path))
try: try:
u = User.select().where(User.username == session['username']).get() u = User.select().where(User.username == session['username']).get()
except DoesNotExist: except DoesNotExist:
@ -51,12 +51,7 @@ def login_required(f):
return decorated_function return decorated_function
@app.cli.command('initdb') @journalmarks.route('/register', methods=['POST'])
def initdb_command():
db_wrapper.database.create_tables([User, AccessToken, Journalmark])
@app.route('/register', methods=['POST'])
def register(): def register():
if len(request.form) != 3: if len(request.form) != 3:
return ('wrong number of fields', 400, None) return ('wrong number of fields', 400, None)
@ -93,16 +88,16 @@ def register():
return json.dumps('ok') return json.dumps('ok')
@app.route('/login', methods=['GET']) @journalmarks.route('/login', methods=['GET'])
def show_login(): def show_login():
if 'next' in request.args and urlparse(request.args['next']).netloc == '': if 'next' in request.args and urlparse(request.args['next']).netloc == '':
next = request.args['next'] next = request.args['next']
else: else:
next = url_for('index') next = url_for('journalmarks.journalmarks.index')
return render_template('login.html', next=next) return render_template('login.html', next=next)
@app.route('/login', methods=['POST']) @journalmarks.route('/login', methods=['POST'])
def login(): def login():
print(request.json) print(request.json)
if len(request.json) != 2: if len(request.json) != 2:
@ -129,20 +124,20 @@ def login():
return json.dumps('ok') return json.dumps('ok')
@app.route('/logout') @journalmarks.route('/logout')
@login_required @login_required
def logout(): def logout():
del session['username'] del session['username']
return render_template('logout.html') return render_template('logout.html')
@app.route('/') @journalmarks.route('/')
@login_required @login_required
def index(): def index():
return render_template('index.html') return render_template('index.html')
@app.route('/create', methods=['POST']) @journalmarks.route('/create', methods=['POST'])
@login_required @login_required
def create(): def create():
if len(request.json) != 1 or 'content' not in request.json: if len(request.json) != 1 or 'content' not in request.json:
@ -160,13 +155,13 @@ def create():
return json.dumps(tag) return json.dumps(tag)
@app.route('/overview', methods=['GET']) @journalmarks.route('/overview', methods=['GET'])
@login_required @login_required
def overview(): def overview():
return render_template('overview.html') return render_template('overview.html')
@app.route('/overview', methods=['POST']) @journalmarks.route('/overview', methods=['POST'])
@login_required @login_required
def overview_get_journalmarks(): def overview_get_journalmarks():
jms = Journalmark.select().where(Journalmark.user == flask_g.user) \ jms = Journalmark.select().where(Journalmark.user == flask_g.user) \
@ -181,7 +176,7 @@ def overview_get_journalmarks():
return json.dumps(ret) return json.dumps(ret)
@app.route('/<tag>') @journalmarks.route('/<tag>')
@login_required @login_required
def get_journalmark(tag): def get_journalmark(tag):
print(tag) print(tag)
@ -192,3 +187,12 @@ def get_journalmark(tag):
except DoesNotExist: except DoesNotExist:
return ('tag not found', 404, None) return ('tag not found', 404, None)
return render_template('get_journalmark.html', j=j) 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

6
journalmarks/templates/base.html

@ -1,7 +1,7 @@
<!doctype html> <!doctype html>
<title>Journalmarks</title> <title>Journalmarks</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> <link rel=stylesheet type=text/css href="{{ url_for('journalmarks.static', filename='style.css') }}">
<script src="{{ url_for('static', filename='journalmarks.js') }}"></script> <script src="{{ url_for('journalmarks.static', filename='journalmarks.js') }}"></script>
<script src="{{ url_for('static', filename='base64js.min.js') }}"></script> <script src="{{ url_for('journalmarks.static', filename='base64js.min.js') }}"></script>
{% block script %}{% endblock %} {% block script %}{% endblock %}
{% block body %}{% endblock %} {% block body %}{% endblock %}

2
journalmarks/templates/get_journalmark.html

@ -11,7 +11,7 @@ function run() {
}); });
}).catch(function() { }).catch(function() {
deleteKey(); deleteKey();
window.location.href = '{{ url_for('login') }}'; window.location.href = '{{ url_for('journalmarks.login') }}';
}); });
} }

8
journalmarks/templates/index.html

@ -13,14 +13,14 @@ function isURL(str) {
function run() { function run() {
journalmarks_loadkey().catch(function() { journalmarks_loadkey().catch(function() {
deleteKey(); deleteKey();
window.location.href = '{{ url_for('login') }}'; window.location.href = '{{ url_for('journalmarks.login') }}';
}); });
document.getElementById('create').addEventListener('click', function() { document.getElementById('create').addEventListener('click', function() {
var url = document.getElementById('url').value; var url = document.getElementById('url').value;
if(isURL(url)) { if(isURL(url)) {
journalmarks_encrypturl(url).then(function (encurl) { 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) { }).then(function(tag) {
console.log('ok', tag); console.log('ok', tag);
}); });
@ -47,8 +47,8 @@ else document.attachEvent('onreadystatechange', function(){
{% block body %} {% block body %}
Welcome! Welcome!
<p> <p>
<a href="{{ url_for('overview') }}">overview</a> <a href="{{ url_for('journalmarks.overview') }}">overview</a>
<a href="{{ url_for('logout') }}">logout</a> <a href="{{ url_for('journalmarks.logout') }}">logout</a>
</p> </p>
<input id="url" type="text"> <input id="url" type="text">

2
journalmarks/templates/login.html

@ -8,7 +8,7 @@ function run() {
sha256(password).then(function(pwhash) { sha256(password).then(function(pwhash) {
var credentials = {username: username, password_hash: pwhash} var credentials = {username: username, password_hash: pwhash}
console.log(username, password, credentials); 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() { .then(journalmarks_initkey(username, password)).then(function() {
window.location.href = '{{ next }}'; window.location.href = '{{ next }}';
}).catch(function(error) { }).catch(function(error) {

4
journalmarks/templates/overview.html

@ -11,7 +11,7 @@ function run() {
}); });
*/ */
journalmarks_loadkey().then(function() { journalmarks_loadkey().then(function() {
return post_object('{{ url_for('overview_get_journalmarks') }}', {}); return post_object('{{ url_for('journalmarks.overview_get_journalmarks') }}', {});
}).then(function(jms) { }).then(function(jms) {
console.log(jms); console.log(jms);
var decrs = []; var decrs = [];
@ -36,7 +36,7 @@ function run() {
}).catch(function(error) { }).catch(function(error) {
console.log(error); console.log(error);
//deleteKey(); //deleteKey();
//window.location.href = '{{ url_for('login') }}'; //window.location.href = '{{ url_for('journalmarks.login') }}';
}); });
} }

Loading…
Cancel
Save