Merge branch 'multi' of kamo/exex into main

main
km0 2 years ago committed by Gitea
commit 442f739451

@ -1,5 +1,5 @@
from flask import (Blueprint, flash, g, redirect, from flask import (Blueprint, flash, g, redirect,
request, session, url_for) request, session, url_for, jsonify)
from flask_mako import render_template from flask_mako import render_template
from exquisite_branch.db import get_db 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') bp = Blueprint('display', __name__, url_prefix='/display')
# branches = db.execute( # branches = db.execute(
# "SELECT content, branch, parent, username FROM branches" # "SELECT content, branch, parent, username FROM branches"
# ).fetchall() # ).fetchall()
@ -35,11 +33,19 @@ bp = Blueprint('display', __name__, url_prefix='/display')
@bp.route('/') @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() db = get_db()
branches = db.execute( branches = db.execute(
"SELECT content, branch, parent, username FROM branches" "SELECT content, branch, parent, username FROM branches WHERE tree = ?",
(tree,)
).fetchall() ).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 branches;
DROP TABLE IF EXISTS trees;
CREATE TABLE branches ( CREATE TABLE branches (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
branch TEXT NOT NULL, branch TEXT NOT NULL,
content TEXT NOT NULL, content TEXT NOT NULL,
parent 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, from flask import (Blueprint, render_template)
render_template, request, session, url_for)
bp = Blueprint('share', __name__, url_prefix='/share') bp = Blueprint('share', __name__, url_prefix='/share')
@bp.route('/<branch>/')
def share(branch=None): @bp.route('/<tree>/<branch>/')
return render_template('share.html', branch=branch) def share(tree=None, branch=None):
return render_template('share.html', tree=tree, branch=branch)

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

@ -27,3 +27,33 @@ nav {
nav > * + * { nav > * + * {
margin-left: 1ch; 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> <nav>
{%block nav %} {%block nav %}
<a href="{{url_for('home.home')}}">Home</a> <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 %} {% endblock %}
</nav> </nav>
{%block contents %} {%endblock%} {%block contents %} {%endblock%}

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

@ -33,7 +33,7 @@
<% transform[branch['branch']] = f'{transform[branch["parent"]]} rotate({random() * 0.04 + steer}turn) translateX(100%)' %> <% transform[branch['branch']] = f'{transform[branch["parent"]]} rotate({random() * 0.04 + steer}turn) translateX(100%)' %>
<div class="text-container" style="transform: ${transform[branch['parent']]}"> <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']} ${branch['content']}
</a> </a>
<span class="author">${branch['username']}</span> <span class="author">${branch['username']}</span>

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

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

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

Loading…
Cancel
Save