import os import random import shutil import string import subprocess from pathlib import Path import petname from flask import Flask, flash, 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 HiddenField, RadioField, StringField from wtforms.validators import DataRequired def rand(length=30): """Generated random secret.""" return "".join(random.choices(string.ascii_uppercase + string.digits, k=length)) app = Flask(__name__) app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", rand()) app.config["WTF_CSRF_SECRET_KEY"] = os.environ.get("WTF_CSRF_SECRET_KEY", rand()) CWD = Path().resolve() class UploadForm(FlaskForm): metadata = FileField( "Metadata file", validators=[FileAllowed(["db"], "Metadata databases only")], ) title = RadioField( "Title", choices=[("side a", "side a"), ("side b", "side b")], default="side a" ) authors = RadioField( "Authors", choices=[("side a", "side a"), ("side b", "side b")], default="side a", ) comments = RadioField( "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")], default="side a", ) tags = RadioField( "Tags", choices=[("side a", "side a"), ("side b", "side b")], default="side b" ) petname = HiddenField("petname") def impose_cards(fpath): """Impose the cards with pdfcpu.""" if shutil.which("pdfcpu"): old_fpath = "{}.old.pdf".format(fpath) shutil.move(fpath, old_fpath) cmd = 'pdfcpu booklet -- "p:A4, border:on" {} 4 {}'.format(fpath, old_fpath) subprocess.run(cmd, shell=True) @app.route("/") def home(): upload_form = UploadForm() pname = petname.generate() pname_msg = "Index of/~{}".format(pname) # pname_msg = "You are now {}".format(pname) return render_template( "index.html", upload_form=upload_form, petname=pname, petname_message=pname_msg, ) @app.route("/about") def about(): return render_template( "about.html", ) @app.route("/upload", methods=["POST"]) def upload(): upload_form = UploadForm() if upload_form.validate_on_submit(): if not upload_form.metadata.data: pname = upload_form.petname.data metadata_name = "metadata-{}.db".format(pname) if not Path(os.path.join(CWD, "metadatum", metadata_name)).exists(): flash("Oops, you forgot to upload a metadata file...") # pname_msg = "You are still {}".format(pname) pname_msg = "Index of/~{}".format(pname) return render_template( "index.html", upload_form=upload_form, petname_message=pname_msg, petname=pname, ) pname = upload_form.petname.data # pname_msg = "You are still {}".format(pname) pname_msg = "Index of/~{}".format(pname) 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_name = "index-{}.pdf".format(pname) # pdf_view = "{}.pdf#view=FitH".join(pname) pdf_path = os.path.join(CWD, "static", "generated", pdf_name) metadata_conn = "sqlite:///{}".format( os.path.join(CWD, "metadatum", metadata_name) ) fields = ["title", "authors", "comments", "timestamp", "tags"] side_a = [ field for field in upload_form.data.keys() if field in fields and upload_form.data[field] == "side a" ] side_b = [ field for field in upload_form.data.keys() if field in fields and upload_form.data[field] == "side b" ] make_cards( pdf_path, metadata_conn, side_a, side_b, ) impose_cards(pdf_path) 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, ) else: pname = upload_form.petname.data pname_msg = "You are still {}".format(pname) return render_template( "index.html", upload_form=upload_form, petname_message=pname_msg, petname=pname, ) 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 )