text version init

master
km0 1 year ago
parent ae185baf0c
commit 059880cd39

@ -33,8 +33,8 @@ def create_app(test_config=None):
os.path.join(app.root_path, "static"), "favicon.ico", mimetype="image/vnd.microsoft.icon",
)
from . import draw
app.register_blueprint(draw.bp)
from . import write
app.register_blueprint(write.bp)
from . import share
app.register_blueprint(share.bp)

@ -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)

@ -4,7 +4,7 @@
<title>Display</title>
{%endblock%} {%block nav%}
<a href="{{url_for('home.home')}}">Home</a>
<a href="{{url_for('draw.last')}}">Draw</a>
<a href="{{url_for('write.last')}}">Write</a>
{%endblock%} {%block contents%}
@ -42,7 +42,7 @@
{{branch['content'] | safe}}
<span class="author"> {{ branch['username']}} </span>
</div>
{%endfor%}
{%endfor%}
</div>
{%endfor%}
</div>

@ -19,8 +19,8 @@
<h1>really exquisite indeed</h1>
</header>
<main>
<a href="{{url_for('draw.new')}}">Start new</a> <br />
<a href="{{url_for('draw.last')}}">Continue from last</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('display.display')}}">Display results</a>
</main>
</body>

@ -11,9 +11,9 @@
<div class="share">
Send this link to your friends:
<a
href="{{url_for('draw.draw', parent=branch)}}"
data-copy="{{ url_for('draw.draw', parent=branch, _external=True, _scheme='https')}}"
>{{ url_for('draw.draw', parent=branch, _external=True, _scheme='https')}}</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
>
</div>

@ -0,0 +1,21 @@
{%extends 'base.html' %} {%block head %}
<title>Write</title>
<link rel="stylesheet" href="{{url_for('static', filename='css/write.css')}}" />
{%endblock%} {%block nav%}
<a href="{{url_for('home.home')}}">Home</a>
<a href="{{url_for('display.display')}}">Results</a>
{%endblock%} {%block contents%}
<h1>Write</h1>
<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" />
</form>
{%endblock%}

@ -0,0 +1,93 @@
from flask import (Blueprint, redirect,
render_template, request, url_for)
from exquisite_branch.db import get_db
from werkzeug.exceptions import abort
from shortuuid import uuid
bp = Blueprint('write', __name__, url_prefix='/write')
@bp.route('/<parent>', methods=('GET', 'POST'))
def write(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 = 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('write.html', parent=parent, content=previous['content'], branch=branch)
@bp.route('/last', methods=('GET', 'POST'))
def last():
branch = 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('write.html', parent=parent, content=previous['content'], branch=branch)
@bp.route('/', methods=('GET', 'POST'))
def new():
db = get_db()
branch = 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('write.html', parent=parent, branch=branch)
Loading…
Cancel
Save