Browse Source

feat: file saving, reloading, prompts

main
cellarspoon 3 years ago
parent
commit
a63cfb7f0c
No known key found for this signature in database GPG Key ID: 3789458B3D0C410
  1. 83
      app.py
  2. 16
      templates/index.html

83
app.py

@ -1,13 +1,14 @@
import os import os
from pathlib import Path from pathlib import Path
from flask import Flask, redirect, render_template, url_for import petname
from flask import Flask, redirect, render_template, request, url_for
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from flask_wtf.csrf import CSRFProtect from flask_wtf.csrf import CSRFProtect
from flask_wtf.file import FileAllowed, FileField, FileRequired from flask_wtf.file import FileAllowed, FileField, FileRequired
from temp_index import make_cards from temp_index import make_cards
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from wtforms import RadioField, StringField from wtforms import HiddenField, RadioField, StringField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
app = Flask(__name__) app = Flask(__name__)
@ -21,23 +22,41 @@ CWD = Path().resolve()
class UploadForm(FlaskForm): class UploadForm(FlaskForm):
metadata = FileField( metadata = FileField(
"Metadata file", "Metadata file",
validators=[FileAllowed(["db"], "Metadata databases only"), FileRequired()], validators=[FileAllowed(["db"], "Metadata databases only")],
)
title = RadioField(
"title", choices=[("side a", "side a"), ("side b", "side b")], default="side a"
)
author = RadioField(
"author", choices=[("side a", "side a"), ("side b", "side b")], default="side a"
) )
title = RadioField("title", choices=[("side a", "side a"), ("side b", "side b")])
author = RadioField("author", choices=[("side a", "side a"), ("side b", "side b")])
tags = RadioField("tags", choices=[("side a", "side a"), ("side b", "side b")])
comments = RadioField( comments = RadioField(
"comments", choices=[("side a", "side a"), ("side b", "side b")] "comments",
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
) )
timestamp = RadioField( timestamp = RadioField(
"timestamp", choices=[("side a", "side a"), ("side b", "side b")] "timestamp",
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
)
tags = RadioField(
"tags", choices=[("side a", "side a"), ("side b", "side b")], default="side b"
) )
petname = HiddenField("petname")
@app.route("/") @app.route("/")
def home(): def home():
upload_form = UploadForm() upload_form = UploadForm()
return render_template("index.html", upload_form=upload_form) pname = petname.generate()
pname_msg = "You are now {}".format(pname)
return render_template(
"index.html",
upload_form=upload_form,
petname=pname,
petname_message=pname_msg,
)
@app.route("/upload", methods=["POST"]) @app.route("/upload", methods=["POST"])
@ -45,21 +64,43 @@ def upload():
upload_form = UploadForm() upload_form = UploadForm()
if upload_form.validate_on_submit(): if upload_form.validate_on_submit():
f = upload_form.metadata.data pname = upload_form.petname.data
filename = secure_filename(f.filename) pname_msg = "You are still {}".format(pname)
metadata_fpath = os.path.join(CWD, "metadatum", filename)
f.save(metadata_fpath)
# FIXME: fix naming of files metadata_name = "metadata-{}.db".format(pname)
if upload_form.metadata.data:
f = upload_form.metadata.data
f.save(os.path.join(CWD, "metadatum", metadata_name))
pdf = os.path.join(CWD, "static", "generated", "{}.pdf".format(filename)) pdf_name = "index-{}.pdf".format(pname)
metadata = "sqlite:///{}".format(os.path.join(CWD, "metadatum", filename)) pdf_path = os.path.join(CWD, "static", "generated", pdf_name)
metadata_conn = "sqlite:///{}".format(
os.path.join(CWD, "metadatum", metadata_name)
)
# FIXME: remove hardcoding, feed fields in
make_cards( make_cards(
pdf, metadata, ["title", "timestamp", "comments", "authors"], ["tags"] pdf_path,
metadata_conn,
["title", "timestamp", "comments", "authors"],
["tags"],
) )
pdf_url = url_for("static", filename="generated/{}.pdf".format(filename))
return render_template("index.html", upload_form=upload_form, pdf=pdf_url)
return render_template("index.html", upload_form=upload_form) gen_msg = (
"Currently working on {}, click 'generate' to re-generate the same PDF"
).format(pdf_name)
pdf_url = url_for("static", filename="generated/{}".format(pdf_name))
return render_template(
"index.html",
upload_form=upload_form,
pdf=pdf_url,
petname=pname,
petname_message=pname_msg,
generation_message=gen_msg,
)
pname = petname.generate()
pname_msg = "You have become {}".format(pname)
return render_template(
"index.html", upload_form=upload_form, petname_message=pname_msg, petname=pname
)

16
templates/index.html

@ -7,6 +7,12 @@
<title>THIS IS A TITLE</title> <title>THIS IS A TITLE</title>
</head> </head>
<body> <body>
<p>{{ petname_message}}. You are anonymous. You may reveal your identity later when saving your generated files.</p>
{% if generation_message %}
<p> {{ generation_message }} </p>
{% endif %}
<form method="POST" action="/upload" enctype="multipart/form-data"> <form method="POST" action="/upload" enctype="multipart/form-data">
{{ upload_form.csrf_token }} {{ upload_form.csrf_token }}
@ -19,16 +25,18 @@
{{ upload_form.author.label }} {{ upload_form.author.label }}
{{ upload_form.author }} {{ upload_form.author }}
{{ upload_form.tags.label }}
{{ upload_form.tags }}
{{ upload_form.comments.label }} {{ upload_form.comments.label }}
{{ upload_form.comments }} {{ upload_form.comments }}
{{ upload_form.timestamp.label }} {{ upload_form.timestamp.label }}
{{ upload_form.timestamp }} {{ upload_form.timestamp }}
<input type="submit" value="Go" /> {{ upload_form.tags.label }}
{{ upload_form.tags }}
<input type="hidden" id="petname" name="petname" value="{{ petname }}">
<input type="submit" value="generate" />
</form> </form>
{% if upload_form.metadata.errors %} {% if upload_form.metadata.errors %}

Loading…
Cancel
Save