diff --git a/app.py b/app.py index 888f29b..a254e98 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,14 @@ import os 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.csrf import CSRFProtect from flask_wtf.file import FileAllowed, FileField, FileRequired from temp_index import make_cards from werkzeug.utils import secure_filename -from wtforms import RadioField, StringField +from wtforms import HiddenField, RadioField, StringField from wtforms.validators import DataRequired app = Flask(__name__) @@ -21,23 +22,41 @@ CWD = Path().resolve() class UploadForm(FlaskForm): metadata = FileField( "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", 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", 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("/") def home(): 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"]) @@ -45,21 +64,43 @@ def upload(): upload_form = UploadForm() if upload_form.validate_on_submit(): - f = upload_form.metadata.data - filename = secure_filename(f.filename) - metadata_fpath = os.path.join(CWD, "metadatum", filename) - f.save(metadata_fpath) + pname = upload_form.petname.data + pname_msg = "You are still {}".format(pname) - # 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)) - metadata = "sqlite:///{}".format(os.path.join(CWD, "metadatum", filename)) + pdf_name = "index-{}.pdf".format(pname) + 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( - 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 + ) diff --git a/templates/index.html b/templates/index.html index 12e446e..4b10303 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,12 @@ THIS IS A TITLE +

{{ petname_message}}. You are anonymous. You may reveal your identity later when saving your generated files.

+ + {% if generation_message %} +

{{ generation_message }}

+ {% endif %} +
{{ upload_form.csrf_token }} @@ -19,16 +25,18 @@ {{ upload_form.author.label }} {{ upload_form.author }} - {{ upload_form.tags.label }} - {{ upload_form.tags }} - {{ upload_form.comments.label }} {{ upload_form.comments }} {{ upload_form.timestamp.label }} {{ upload_form.timestamp }} - + {{ upload_form.tags.label }} + {{ upload_form.tags }} + + + +
{% if upload_form.metadata.errors %}