multi tree mode

pull/1/head
km0 2 years ago
parent 1ade6436b6
commit e2657452ac

@ -1,5 +1,5 @@
from flask import (Blueprint, flash, g, redirect,
request, session, url_for)
request, session, url_for, jsonify)
from flask_mako import render_template
from exquisite_branch.db import get_db
@ -8,8 +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()
@ -35,11 +33,19 @@ bp = Blueprint('display', __name__, url_prefix='/display')
@bp.route('/')
def display():
def list():
db = get_db()
trees = db.execute('SELECT * FROM trees').fetchall()
return render_template('list.html', trees=trees)
@bp.route('/<tree>/')
def display(tree=None):
db = get_db()
branches = db.execute(
"SELECT content, branch, parent, username FROM branches"
"SELECT content, branch, parent, username FROM branches WHERE tree = ?",
(tree,)
).fetchall()
return render_template('display.html', branches=branches)
return render_template('display.html', tree=tree, branches=branches)

@ -1,9 +1,18 @@
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,
parent TEXT NOT NULL,
username TEXT
username TEXT,
tree TEXT NOT NULL,
FOREIGN KEY (tree) REFERENCES trees(slug)
);
CREATE TABLE trees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
slug TEXT NOT NULL UNIQUE
);

@ -1,9 +1,9 @@
from flask import (Blueprint, flash, g, redirect,
render_template, request, session, url_for)
from flask import (Blueprint, render_template)
bp = Blueprint('share', __name__, url_prefix='/share')
@bp.route('/<branch>/')
def share(branch=None):
return render_template('share.html', branch=branch)
@bp.route('/<tree>/<branch>/')
def share(tree=None, branch=None):
return render_template('share.html', tree=tree, branch=branch)

@ -13,9 +13,11 @@ margin: 0;
.container{
display: block;
padding: 64px;
padding: 32px;
}
.stream,
.streams{
/* white-space: nowrap; */

@ -27,3 +27,33 @@ nav {
nav > * + * {
margin-left: 1ch;
}
header, main {
margin: 32px;
}
main form {
display: flex;
flex-direction: column;
gap: 16px;
max-width: 80ch;
align-items: flex-start;
}
form textarea,
form input {
font-size: 1rem;
font-family: serif;
border: none;
padding: 0.5em;
}
textarea:focus,
input:focus {
outline: dotted white;
}
textarea {
width: 80ch;
height: 25ch;
}

@ -14,7 +14,7 @@
<nav>
{%block nav %}
<a href="{{url_for('home.home')}}">Home</a>
<a href="{{url_for('display.display')}}">Results</a>
<a href="{{url_for('display.list')}}">Results</a>
{% endblock %}
</nav>
{%block contents %} {%endblock%}

@ -14,7 +14,7 @@
<nav>
<%block name='nav' >
<a href="${url_for('home.home')}">Home</a>
<a href="${url_for('display.display')}">Results</a>
<a href="${url_for('display.list')}">Results</a>
</%block>
</nav>
${self.body()}

@ -33,7 +33,7 @@
<% transform[branch['branch']] = 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', parent=branch['branch'], _external=True, _scheme='http')}" target="__blank">
<a href="${url_for('write.write', tree=tree, parent=branch['branch'], _external=True, _scheme='http')}" target="__blank">
${branch['content']}
</a>
<span class="author">${branch['username']}</span>

@ -20,7 +20,7 @@
<main>
<a href="{{url_for('write.new')}}">Start new</a> <br />
<a href="{{url_for('write.last')}}">Continue from last</a> <br />
<a href="{{url_for('display.display')}}">Display results</a>
<a href="{{url_for('display.list')}}">Display results</a>
</main>
</body>
</html>

@ -0,0 +1,28 @@
<%inherit file="base_mako.html" />
<%block name="head">
<link rel="stylesheet" href="${url_for('static', filename='css/display.css')}">
</%block>
<header>
<h1>Exquisite Excerpts</h1>
<p>Here is a list of all the writings trees</p>
</header>
<main class="container">
<ul>
% for tree in trees:
<li class="tree">
<a href="${url_for('display.display', tree=tree['slug'])}">
${tree['name']}
</a>
</li>
% endfor
</ul>
</main>

@ -5,15 +5,16 @@
{%endblock%} {%block nav %}
<a href="{{url_for('home.home')}}">Home</a>
<a href="{{url_for('display.display')}}">Results</a>
<a href="{{url_for('display.display', tree=tree)}}">Results</a>
{% endblock %} {%block contents %}
<div class="share">
Send this link to your friends:
<a
href="{{url_for('write.write', parent=branch)}}"
data-copy="{{ url_for('write.write', parent=branch, _external=True, _scheme='https')}}"
>{{ url_for('write.write', parent=branch, _external=True, _scheme='https')}}</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
>
</div>

@ -5,7 +5,14 @@
{%endblock%} {%block nav%}
<a href="{{url_for('home.home')}}">Home</a>
<a href="{{url_for('display.display')}}">Results</a>
{% if tree is undefined %}
<a href="{{url_for('display.list')}}">See all</a>
{% else %}
<a href="{{url_for('display.display', tree=tree)}}">See tree</a>
{% endif %}
{%endblock%} {%block contents%}
@ -13,15 +20,30 @@
<!-- data-parent="{{parent or None}}" -->
<!-- data-branch="{{branch}}" -->
<!-- Maybe to create links in the display page? -->
<header>
<h1>Write</h1>
</header>
<main>
<div id="previous">{% if content %} {{content|safe}} {% endif %}</div>
<form method="POST">
<input type="hidden" name="branch" value="{{branch}}" />
<textarea name="content" ></textarea>
<input type="text" name="username" placeholder="Name" />
<input type="submit" />
{% if tree is undefined %}
<input type="text" name="tree_name" placeholder="Tree name" required />
<br/>
{% endif %}
<textarea name="content" placeholder="Write here..." required></textarea>
<br/>
<input type="text" name="username" placeholder="Author" />
<br/>
<input type="submit" value="Submit" />
</form>
</main>
{%endblock%}

@ -1,16 +1,16 @@
from flask import (Blueprint, flash, g, redirect,
render_template, request, session, url_for)
from flask import (Blueprint, redirect, render_template, request, url_for)
from exquisite_branch.db import get_db
from werkzeug.exceptions import abort
from werkzeug.utils import secure_filename
from shortuuid import uuid
bp = Blueprint('write', __name__, url_prefix='/write')
@bp.route('/<parent>', methods=('GET', 'POST'))
def write(parent=None):
@bp.route('/<tree>/<parent>', methods=('GET', 'POST'))
def write(tree=None, parent=None):
db = get_db()
if request.method == 'POST':
@ -24,30 +24,28 @@ def write(parent=None):
username = data['username']
db.execute(
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
(content, parent, branch, username)
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
)
db.commit()
print(url_for('share.share', branch=f"{branch}"))
return redirect(url_for('share.share', branch=branch))
print(url_for('share.share', tree=tree, branch=branch))
return redirect(url_for('share.share', tree=tree, branch=branch))
branch = uuid()
previous = db.execute(
"SELECT content, branch, parent FROM branches"
" WHERE branch = ?",
(parent,)
" WHERE branch = ? AND tree = ?",
(parent, tree)
).fetchone()
if previous is None:
abort(404, f"Previous with id {parent} doesn't exist")
return render_template('write.html', parent=parent, content=previous['content'], branch=branch)
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
@bp.route('/last', methods=('GET', 'POST'))
def last():
branch = uuid()
db = get_db()
previous = db.execute(
@ -55,6 +53,7 @@ def last():
).fetchone()
parent = previous['branch']
tree = previous['tree']
if request.method == 'POST':
content = request.form['content']
@ -62,13 +61,13 @@ def last():
username = request.form['username']
db.execute(
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
(content, parent, branch, username)
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
)
db.commit()
return redirect(url_for('share.share', branch=branch))
return redirect(url_for('share.share', tree=tree, branch=branch))
return render_template('write.html', parent=parent, content=previous['content'], branch=branch)
return render_template('write.html', tree=tree, parent=parent, content=previous['content'], branch=branch)
@bp.route('/', methods=('GET', 'POST'))
@ -80,14 +79,24 @@ def new():
if request.method == 'POST':
content = request.form['content']
branch = request.form['branch']
username = request.form['username']
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)
)
db.execute(
'INSERT INTO branches (content, parent, branch, username) VALUES (?, ?, ?, ?)',
(content, parent, branch, username)
'INSERT INTO branches (content, parent, branch, username, tree) VALUES (?, ?, ?, ?, ?)',
(content, parent, branch, username, tree)
)
db.commit()
return redirect(url_for('share.share', branch=branch))
return redirect(url_for('share.share', tree=tree, branch=branch))
return render_template('write.html', parent=parent, branch=branch)

Loading…
Cancel
Save