You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.2 KiB

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
3 years ago
from flask_wtf import FlaskForm
3 years ago
from flask_wtf.csrf import CSRFProtect
from flask_wtf.file import FileAllowed, FileField, FileRequired
3 years ago
from temp_index import make_cards
from werkzeug.utils import secure_filename
from wtforms import HiddenField, RadioField, StringField
3 years ago
from wtforms.validators import DataRequired
def rand(length=30):
"""Generated random secret."""
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))
3 years ago
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())
3 years ago
3 years ago
CWD = Path().resolve()
3 years ago
class UploadForm(FlaskForm):
3 years ago
metadata = FileField(
3 years ago
"Metadata file",
validators=[FileAllowed(["db"], "Metadata databases only")],
)
title = RadioField(
3 years ago
"Title", choices=[("side a", "side a"), ("side b", "side b")], default="side a"
)
3 years ago
authors = RadioField(
3 years ago
"Authors",
3 years ago
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
3 years ago
)
3 years ago
comments = RadioField(
3 years ago
"Comments",
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
3 years ago
)
timestamp = RadioField(
3 years ago
"Timestamp",
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
)
tags = RadioField(
3 years ago
"Tags", choices=[("side a", "side a"), ("side b", "side b")], default="side b"
3 years ago
)
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)
3 years ago
@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)
3 years ago
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))
3 years ago
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)
)
3 years ago
3 years ago
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"
]
3 years ago
make_cards(
pdf_path,
metadata_conn,
side_a,
side_b,
3 years ago
)
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
)