api work in progress
parent
442f739451
commit
c0eeb1a007
@ -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)
|
||||
|
@ -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>
|
Loading…
Reference in New Issue