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.

65 lines
1.7 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
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):
pdf = FileField(validators=[FileAllowed(["pdf"], "PDFs only!"), FileRequired()])
3 years ago
3 years ago
class CardSidesForm(FlaskForm):
title = RadioField("title", choices=[("side a", "side a"), ("side b", "side b")])
3 years ago
@app.route("/")
def home():
upload_form = UploadForm()
card_sides_form = CardSidesForm()
return render_template(
"index.html", upload_form=upload_form, card_sides_form=card_sides_form
)
@app.route("/upload", methods=["POST"])
def upload():
upload_form = UploadForm()
card_sides_form = CardSidesForm()
if upload_form.validate_on_submit():
f = upload_form.pdf.data
filename = secure_filename(f.filename)
f.save(os.path.join(CWD, "pdfs", filename))
return redirect(url_for("home"))
return render_template(
"index.html", upload_form=upload_form, card_sides_form=card_sides_form
)
@app.route("/generate", methods=["POST"])
def generate():
upload_form = UploadForm()
card_sides_form = CardSidesForm()
if card_sides_form.validate_on_submit():
print(card_sides_form.title.data)
else:
print("didnt work")
return render_template(
"index.html", upload_form=upload_form, card_sides_form=card_sides_form
)