|
|
|
from flask import (Blueprint, flash, g, redirect,
|
|
|
|
render_template, request, session, url_for)
|
|
|
|
|
|
|
|
from exquisite_branch.db import get_db
|
|
|
|
from werkzeug.exceptions import abort
|
|
|
|
|
|
|
|
import shortuuid
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
def get_uuid(d=7):
|
|
|
|
'''return a short uuid (default 7 chars)'''
|
|
|
|
u = uuid.uuid4()
|
|
|
|
s = shortuuid.encode(u)
|
|
|
|
return s[:d]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bp = Blueprint('draw', __name__, url_prefix='/draw')
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<parent>', methods=('GET', 'POST'))
|
|
|
|
def draw(parent=None):
|
|
|
|
db = get_db()
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
content = request.form.get('content')
|
|
|
|
branch = request.form.get('branch')
|
|
|
|
username = request.form.get('username')
|
|
|
|
if request.is_json:
|
|
|
|
data = request.get_json()
|
|
|
|
content = data['content']
|
|
|
|
branch = data['branch']
|
|
|
|
username = data['username']
|
|
|
|
|
|
|
|
db.execute(
|
|
|
|
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
|
|
|
|
(content, parent, branch, username)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
print(url_for('share.share', branch=f"{branch}"))
|
|
|
|
return redirect(url_for('share.share', branch=branch))
|
|
|
|
|
|
|
|
branch = get_uuid()
|
|
|
|
|
|
|
|
|
|
|
|
previous = db.execute(
|
|
|
|
"SELECT content, branch, parent FROM branches"
|
|
|
|
" WHERE branch = ?",
|
|
|
|
(parent,)
|
|
|
|
).fetchone()
|
|
|
|
|
|
|
|
if previous is None:
|
|
|
|
abort(404, f"Previous with id {parent} doesn't exist")
|
|
|
|
|
|
|
|
return render_template('draw.html', parent=parent, content=previous['content'], branch=branch)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/last', methods=('GET', 'POST'))
|
|
|
|
def last():
|
|
|
|
|
|
|
|
branch = get_uuid()
|
|
|
|
db = get_db()
|
|
|
|
previous = db.execute(
|
|
|
|
'SELECT * FROM branches ORDER BY id DESC LIMIT 1'
|
|
|
|
).fetchone()
|
|
|
|
|
|
|
|
parent = previous['branch']
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
content = request.form['content']
|
|
|
|
branch = request.form['branch']
|
|
|
|
username = request.form['username']
|
|
|
|
|
|
|
|
db.execute(
|
|
|
|
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
|
|
|
|
(content, parent, branch, username)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return redirect(url_for('share.share', branch=branch))
|
|
|
|
|
|
|
|
return render_template('draw.html', parent=parent, content=previous['content'], branch=branch)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/', methods=('GET', 'POST'))
|
|
|
|
def new():
|
|
|
|
db = get_db()
|
|
|
|
branch = get_uuid()
|
|
|
|
parent = 'NEW'
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
content = request.form['content']
|
|
|
|
branch = request.form['branch']
|
|
|
|
|
|
|
|
username = request.form['username']
|
|
|
|
|
|
|
|
db.execute(
|
|
|
|
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
|
|
|
|
(content, parent, branch, username)
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return redirect(url_for('share.share', branch=branch))
|
|
|
|
|
|
|
|
return render_template('draw.html', parent=parent, branch=branch)
|