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.

66 lines
2.2 KiB

import os
from pathlib import Path
from flask import Flask, redirect, render_template, 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 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"), FileRequired()],
3 years ago
)
title = RadioField("title", choices=[("side a", "side a"), ("side b", "side b")])
3 years ago
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", choices=[("side a", "side a"), ("side b", "side b")]
)
timestamp = RadioField(
"timestamp", choices=[("side a", "side a"), ("side b", "side b")]
)
3 years ago
@app.route("/")
def home():
upload_form = UploadForm()
return render_template("index.html", upload_form=upload_form)
@app.route("/upload", methods=["POST"])
def upload():
upload_form = UploadForm()
if upload_form.validate_on_submit():
3 years ago
f = upload_form.metadata.data
filename = secure_filename(f.filename)
3 years ago
metadata_fpath = os.path.join(CWD, "metadatum", filename)
f.save(metadata_fpath)
3 years ago
# FIXME: fix naming of files
pdf = os.path.join(CWD, "static", "generated", "{}.pdf".format(filename))
3 years ago
metadata = "sqlite:///{}".format(os.path.join(CWD, "metadatum", filename))
3 years ago
# FIXME: remove hardcoding, feed fields in
3 years ago
make_cards(
pdf, metadata, ["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)