api work in progress

main
km0 2 years ago
parent 442f739451
commit c0eeb1a007

@ -48,6 +48,9 @@ def create_app(test_config=None):
from . import home
app.register_blueprint(home.bp)
from . import api
app.register_blueprint(api.bp)
app.wsgi_app = prefix.PrefixMiddleware(
app.wsgi_app, prefix=os.environ.get("URL_PREFIX", ''))

@ -0,0 +1,59 @@
from flask import (Blueprint, jsonify)
from exquisite_branch.db import get_db
bp = Blueprint('api', __name__, url_prefix='/api/v1')
def makeTree(entries):
if len(entries) > 1:
last = entries.pop()
if last['parent'] != 'NEWS':
parent_index = next(
(index for (index, entry) in enumerate(entries) if entry['id'] == last['parent']),
None)
entries[parent_index].setdefault('branches', []).append(last)
makeTree(entries)
return entries
@bp.route('/trees/')
def list():
db = get_db()
trees = db.execute('SELECT * FROM trees').fetchall()
response = []
for tree in trees:
response.append(tree['slug'])
return jsonify(response)
@bp.route('tree/<tree>/')
def tree(tree=None):
db = get_db()
tree_info = db.execute('SELECT * FROM trees WHERE slug = ?', (tree,)).fetchone()
entries = db.execute(
"SELECT content, id, parent, username FROM branches WHERE tree = ?",
(tree,)
).fetchall()
response = {
"id": tree_info['id'],
"slug": tree_info['slug'],
"name": tree_info['name'],
}
# convert sqlite entries to list of normal dict
branches = []
for entry in entries:
branches.append({**entry})
# make tree:
# while list is not empty
# start from the last one
# insert it in its parent if not NEW
# remove it from the list
response['branches'] = makeTree(branches)
return jsonify(response)

@ -8,30 +8,6 @@ from exquisite_branch.db import get_db
bp = Blueprint('display', __name__, url_prefix='/display')
# branches = db.execute(
# "SELECT content, branch, parent, username FROM branches"
# ).fetchall()
# streams = []
# for branch in branches[::-1]:
# if branch not in flatten(streams):
# stream = [branch]
# parent = branch['parent']
# while parent != 'NEW':
# current = next(
# (x for x in branches if x['branch'] == parent), None)
# parent = current['parent']
# stream.append(current)
# streams.append(stream[::-1])
# return render_template('display_mako.html', branches=branches, streams=streams)
# def flatten(t):
# return [item for sublist in t for item in sublist]
@bp.route('/')
def list():
db = get_db()
@ -44,8 +20,13 @@ def display(tree=None):
db = get_db()
branches = db.execute(
"SELECT content, branch, parent, username FROM branches WHERE tree = ?",
"SELECT content, id, parent, username FROM branches WHERE tree = ?",
(tree,)
).fetchall()
return render_template('display.html', tree=tree, branches=branches)
@bp.route('/<tree>/js/')
def js(tree=None):
return render_template('display_js.html', tree=tree)

@ -2,10 +2,9 @@ DROP TABLE IF EXISTS branches;
DROP TABLE IF EXISTS trees;
CREATE TABLE branches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
branch TEXT NOT NULL,
content TEXT NOT NULL,
id TEXT PRIMARY KEY NOT NULL,
parent TEXT NOT NULL,
content TEXT NOT NULL,
username TEXT,
tree TEXT NOT NULL,
FOREIGN KEY (tree) REFERENCES trees(slug)

@ -4,6 +4,6 @@ from flask import (Blueprint, render_template)
bp = Blueprint('share', __name__, url_prefix='/share')
@bp.route('/<tree>/<branch>/')
def share(tree=None, branch=None):
return render_template('share.html', tree=tree, branch=branch)
@bp.route('/<tree>/<id>/')
def share(tree=None, id=None):
return render_template('share.html', tree=tree, id=id)

@ -30,10 +30,10 @@
<% steer = 0 %>
% endif
<% transform[branch['branch']] = f'{transform[branch["parent"]]} rotate({random() * 0.04 + steer}turn) translateX(100%)' %>
<% transform[branch['id']] = f'{transform[branch["parent"]]} rotate({random() * 0.04 + steer}turn) translateX(100%)' %>
<div class="text-container" style="transform: ${transform[branch['parent']]}">
<a href="${url_for('write.write', tree=tree, parent=branch['branch'], _external=True, _scheme='http')}" target="__blank">
<a href="${url_for('write.write', tree=tree, parent=branch['id'], _external=True, _scheme='http')}" target="__blank">
${branch['content']}
</a>
<span class="author">${branch['username']}</span>

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${tree}</title>
<style>
.container {
font-size: 24px;
}
.child {
display: flex;
flex-direction: row;
align-items: flex-start;
background-color: rgba(255,0,0,0.075);
border: 1px solid rgba(255,0,0,0.1);
gap: 0.5em;
padding: 0.5em;
}
.child+.child {
flex-direction: column;
}
</style>
</head>
<body>
<h1 id="tree">${tree}</h1>
<div class="container"></div>
<script>
const title = document.querySelector('#tree').innerText
const slug = title.innerText
const container = document.querySelector('.container')
const display = (element, tree) => {
if(tree.branches){
let content = document.createElement('div')
content.classList.add('child')
if (tree.content) {
content.innerHTML = tree.content
}
element.appendChild(content)
for (const b of tree.branches) {
display(content, b)
}
}
}
fetch(`/api/v1/tree/${tree}/`).then(res=>res.json()).then(res=>display(container, res))
</script>
</body>
</html>

@ -12,9 +12,9 @@
<div class="share">
Send this link to your friends:
<a
href="{{url_for('write.write',tree=tree, parent=branch)}}"
data-copy="{{ url_for('write.write', tree=tree, parent=branch, _external=True, _scheme='https')}}"
>{{ url_for('write.write', tree=tree, parent=branch, _external=True, _scheme='https')}}</a
href="{{url_for('write.write',tree=tree, parent=id)}}"
data-copy="{{ url_for('write.write', tree=tree, parent=id, _external=True, _scheme='https')}}"
>{{ url_for('write.write', tree=tree, parent=id, _external=True, _scheme='https')}}</a
>
</div>

@ -18,7 +18,7 @@
<!-- mmm i dont remember what i was using these two things for! -->
<!-- data-parent="{{parent or None}}" -->
<!-- data-branch="{{branch}}" -->
<!-- data-id="{{id}}" -->
<!-- Maybe to create links in the display page? -->
<header>
<h1>Write</h1>
@ -31,7 +31,7 @@
<div id="previous">{% if content %} {{content|safe}} {% endif %}</div>
<form method="POST">
<input type="hidden" name="branch" value="{{branch}}" />
<input type="hidden" name="id" value="{{id}}" />
{% if tree is undefined %}
<input type="text" name="tree_name" placeholder="Tree name" required />

@ -15,70 +15,70 @@ def write(tree=None, parent=None):
if request.method == 'POST':
content = request.form.get('content')
branch = request.form.get('branch')
id = request.form.get('id')
username = request.form.get('username')
if request.is_json:
data = request.get_json()
content = data['content']
branch = data['branch']
id = data['id']
username = data['username']
db.execute(
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
'INSERT INTO branches (content, parent, id, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, id, username, tree)
)
db.commit()
print(url_for('share.share', tree=tree, branch=branch))
return redirect(url_for('share.share', tree=tree, branch=branch))
print(url_for('share.share', tree=tree, id=id))
return redirect(url_for('share.share', tree=tree, id=id))
branch = uuid()
id = uuid()
previous = db.execute(
"SELECT content, branch, parent FROM branches"
" WHERE branch = ? AND tree = ?",
"SELECT content, id, parent FROM branches"
" WHERE id = ? AND tree = ?",
(parent, tree)
).fetchone()
if previous is None:
abort(404, f"Previous with id {parent} doesn't exist")
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], id=id)
@bp.route('/last', methods=('GET', 'POST'))
def last():
branch = uuid()
id = uuid()
db = get_db()
previous = db.execute(
'SELECT * FROM branches ORDER BY id DESC LIMIT 1'
).fetchone()
parent = previous['branch']
parent = previous['id']
tree = previous['tree']
if request.method == 'POST':
content = request.form['content']
branch = request.form['branch']
id = request.form['id']
username = request.form['username']
db.execute(
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
'INSERT INTO branches (content, parent, id, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, id, username, tree)
)
db.commit()
return redirect(url_for('share.share', tree=tree, branch=branch))
return redirect(url_for('share.share', tree=tree, id=id))
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], id=id)
@bp.route('/', methods=('GET', 'POST'))
def new():
db = get_db()
branch = uuid()
id = uuid()
parent = 'NEW'
if request.method == 'POST':
content = request.form['content']
branch = request.form['branch']
id = request.form['id']
username = request.form['username']
tree_name = request.form['tree_name']
@ -93,10 +93,10 @@ def new():
)
db.execute(
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
'INSERT INTO branches (content, parent, id, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, id, username, tree)
)
db.commit()
return redirect(url_for('share.share', tree=tree, branch=branch))
return redirect(url_for('share.share', tree=tree, id=id))
return render_template('write.html', parent=parent, branch=branch)
return render_template('write.html', parent=parent, id=id)

Loading…
Cancel
Save