Browse Source

switched to following the app factory pattern

master
Fr3deric 6 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 @@ @@ -1 +1 @@
from .journalmarks import app
from .journalmarks import create_app

46
journalmarks/journalmarks.py

@ -7,17 +7,17 @@ import hashlib @@ -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): @@ -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): @@ -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(): @@ -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(): @@ -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(): @@ -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(): @@ -181,7 +176,7 @@ def overview_get_journalmarks():
return json.dumps(ret)
@app.route('/<tag>')
@journalmarks.route('/<tag>')
@login_required
def get_journalmark(tag):
print(tag)
@ -192,3 +187,12 @@ def get_journalmark(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

6
journalmarks/templates/base.html

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

2
journalmarks/templates/get_journalmark.html

@ -11,7 +11,7 @@ function run() { @@ -11,7 +11,7 @@ function run() {
});
}).catch(function() {
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) { @@ -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(){ @@ -47,8 +47,8 @@ else document.attachEvent('onreadystatechange', function(){
{% block body %}
Welcome!
<p>
<a href="{{ url_for('overview') }}">overview</a>
<a href="{{ url_for('logout') }}">logout</a>
<a href="{{ url_for('journalmarks.overview') }}">overview</a>
<a href="{{ url_for('journalmarks.logout') }}">logout</a>
</p>
<input id="url" type="text">

2
journalmarks/templates/login.html

@ -8,7 +8,7 @@ function run() { @@ -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) {

4
journalmarks/templates/overview.html

@ -11,7 +11,7 @@ function run() { @@ -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() { @@ -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') }}';
});
}

Loading…
Cancel
Save