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.
 
 
 
 

135 lines
3.9 KiB

import os
import random
import string
from pathlib import Path
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 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"
)
author = RadioField(
"author", 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")
@app.route("/")
def home():
upload_form = UploadForm()
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"])
def upload():
upload_form = UploadForm()
if upload_form.validate_on_submit():
pname = upload_form.petname.data
pname_msg = "You are still {}".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_path = os.path.join(CWD, "static", "generated", pdf_name)
metadata_conn = "sqlite:///{}".format(
os.path.join(CWD, "metadatum", metadata_name)
)
fields = ["title", "author", "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,
)
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
)