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.

119 lines
3.4 KiB

import os
from pathlib import Path
import petname
from flask import Flask, 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
app = Flask(__name__)
3 years ago
app.config["SECRET_KEY"] = "foo"
app.config["WTF_CSRF_SECRET_KEY"] = "bar"
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(
"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"
3 years ago
)
3 years ago
comments = RadioField(
"comments",
choices=[("side a", "side a"), ("side b", "side b")],
default="side a",
3 years ago
)
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"
3 years ago
)
petname = HiddenField("petname")
3 years ago
@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)
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_path = os.path.join(CWD, "static", "generated", pdf_name)
metadata_conn = "sqlite:///{}".format(
os.path.join(CWD, "metadatum", metadata_name)
)
3 years ago
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"
]
3 years ago
make_cards(
pdf_path,
metadata_conn,
side_a,
side_b,
3 years ago
)
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
)