|
@ -1,13 +1,14 @@ |
|
|
import os |
|
|
import os |
|
|
from pathlib import Path |
|
|
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 import FlaskForm |
|
|
from flask_wtf.csrf import CSRFProtect |
|
|
from flask_wtf.csrf import CSRFProtect |
|
|
from flask_wtf.file import FileAllowed, FileField, FileRequired |
|
|
from flask_wtf.file import FileAllowed, FileField, FileRequired |
|
|
from temp_index import make_cards |
|
|
from temp_index import make_cards |
|
|
from werkzeug.utils import secure_filename |
|
|
from werkzeug.utils import secure_filename |
|
|
from wtforms import RadioField, StringField |
|
|
from wtforms import HiddenField, RadioField, StringField |
|
|
from wtforms.validators import DataRequired |
|
|
from wtforms.validators import DataRequired |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
app = Flask(__name__) |
|
@ -21,23 +22,41 @@ CWD = Path().resolve() |
|
|
class UploadForm(FlaskForm): |
|
|
class UploadForm(FlaskForm): |
|
|
metadata = FileField( |
|
|
metadata = FileField( |
|
|
"Metadata file", |
|
|
"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 = 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 = 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("/") |
|
|
@app.route("/") |
|
|
def home(): |
|
|
def home(): |
|
|
upload_form = UploadForm() |
|
|
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"]) |
|
|
@app.route("/upload", methods=["POST"]) |
|
@ -45,21 +64,43 @@ def upload(): |
|
|
upload_form = UploadForm() |
|
|
upload_form = UploadForm() |
|
|
|
|
|
|
|
|
if upload_form.validate_on_submit(): |
|
|
if upload_form.validate_on_submit(): |
|
|
f = upload_form.metadata.data |
|
|
pname = upload_form.petname.data |
|
|
filename = secure_filename(f.filename) |
|
|
pname_msg = "You are still {}".format(pname) |
|
|
metadata_fpath = os.path.join(CWD, "metadatum", filename) |
|
|
|
|
|
f.save(metadata_fpath) |
|
|
|
|
|
|
|
|
|
|
|
# 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)) |
|
|
pdf_name = "index-{}.pdf".format(pname) |
|
|
metadata = "sqlite:///{}".format(os.path.join(CWD, "metadatum", filename)) |
|
|
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( |
|
|
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 |
|
|
|
|
|
) |
|
|