|
|
|
@ -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 |
|
|
|
|