You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB
Python

2 years ago
from flask import (Blueprint, redirect, render_template, request, url_for)
3 years ago
from exquisite_branch.db import get_db
from werkzeug.exceptions import abort
2 years ago
from werkzeug.utils import secure_filename
3 years ago
from shortuuid import uuid
2 years ago
bp = Blueprint('write', __name__, url_prefix='/write')
3 years ago
2 years ago
@bp.route('/<tree>/<parent>', methods=('GET', 'POST'))
def write(tree=None, parent=None):
3 years ago
db = get_db()
if request.method == 'POST':
content = request.form.get('content')
branch = request.form.get('branch')
username = request.form.get('username')
3 years ago
if request.is_json:
data = request.get_json()
content = data['content']
branch = data['branch']
username = data['username']
3 years ago
db.execute(
2 years ago
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
3 years ago
)
db.commit()
2 years ago
print(url_for('share.share', tree=tree, branch=branch))
return redirect(url_for('share.share', tree=tree, branch=branch))
3 years ago
branch = uuid()
previous = db.execute(
"SELECT content, branch, parent FROM branches"
2 years ago
" WHERE branch = ? AND tree = ?",
(parent, tree)
3 years ago
).fetchone()
if previous is None:
abort(404, f"Previous with id {parent} doesn't exist")
2 years ago
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
3 years ago
@bp.route('/last', methods=('GET', 'POST'))
def last():
branch = uuid()
db = get_db()
previous = db.execute(
'SELECT * FROM branches ORDER BY id DESC LIMIT 1'
).fetchone()
parent = previous['branch']
2 years ago
tree = previous['tree']
3 years ago
if request.method == 'POST':
content = request.form['content']
branch = request.form['branch']
username = request.form['username']
3 years ago
db.execute(
2 years ago
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
3 years ago
)
db.commit()
2 years ago
return redirect(url_for('share.share', tree=tree, branch=branch))
3 years ago
2 years ago
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
3 years ago
@bp.route('/', methods=('GET', 'POST'))
def new():
db = get_db()
branch = uuid()
parent = 'NEW'
if request.method == 'POST':
content = request.form['content']
branch = request.form['branch']
username = request.form['username']
2 years ago
tree_name = request.form['tree_name']
if tree_name == '':
return redirect(url_for('write.new'))
tree = secure_filename(tree_name)
db.execute(
'INSERT INTO trees (name, slug) VALUES (?, ?)',
(tree_name, tree)
)
3 years ago
db.execute(
2 years ago
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
3 years ago
)
db.commit()
2 years ago
return redirect(url_for('share.share', tree=tree, branch=branch))
3 years ago
2 years ago
return render_template('write.html', parent=parent, branch=branch)