|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from flask import Flask, redirect, render_template, url_for
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from flask_wtf.csrf import CSRFProtect
|
|
|
|
from flask_wtf.file import FileAllowed, FileField, FileRequired
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from wtforms import StringField
|
|
|
|
from wtforms.validators import DataRequired
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config["SECRET_KEY"] = "foo"
|
|
|
|
app.config["WTF_CSRF_SECRET_KEY"] = "bar"
|
|
|
|
|
|
|
|
|
|
|
|
CWD = Path().resolve()
|
|
|
|
|
|
|
|
|
|
|
|
class UploadForm(FlaskForm):
|
|
|
|
pdf = FileField(validators=[FileAllowed(["pdf"], "PDFs only!"), FileRequired()])
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
form = UploadForm()
|
|
|
|
return render_template("index.html", form=form)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/upload", methods=["POST"])
|
|
|
|
def upload():
|
|
|
|
form = UploadForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
f = 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", form=form)
|