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