You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.3 KiB
Python

from app import app
import flask
from flask import Blueprint, render_template, request, redirect, url_for, flash
import datetime
import os
from werkzeug.utils import secure_filename
from werkzeug.datastructures import ImmutableOrderedMultiDict
import collections
from collections import OrderedDict
import dataset
import sys
import time
app.secret_key = 'PiracyIsCool'
now = datetime.datetime.now()
@app.route("/")
def index():
return render_template("public/index.html")
@app.route("/about")
def about():
return render_template("public/about.html")
# UPLOAD FILES
# THIS SHOULD BE IN CONFIG
app.config["BOOK_UPLOADS"] = "/var/www/TacticalApp/app/static/upload/"
app.config["BOOK_REQUEST"] = "/var/www/TacticalApp/app/static/request"
app.config["ALLOWED_BOOK_EXTENSIONS"] = ["PDF", "EPUB"]
app.config["MAX_BOOK_FILESIZE"] = 500000000
def allowed_book(filename):
if not "." in filename:
return False
ext = filename.rsplit(".", 1)[1]
if ext.upper() in app.config["ALLOWED_BOOK_EXTENSIONS"]:
return True
else:
return False
def allowed_book_filesize(filesize):
if int(filesize) <= app.config["MAX_BOOK_FILESIZE"]:
return True
else:
return False
@app.route("/uploadbook", methods=["GET", "POST"])
def uploadbook():
if request.method == "POST":
if request.files:
#request filesize
if not allowed_book_filesize(request.cookies.get("filesize")):
print("File exceeded maximum size")
return redirect(request.url)
#demand checkbox
if not request.form.get('match-with-pairs'):
print("You should agree with the T&C")
return redirect(request.url)
book = request.files["book"]
# confirm book has a name
if book.filename == "":
print("Book must have a filename")
return redirect(request.url)
# confirm book is the desired format
if not allowed_book(book.filename):
print("That book extention is not allowed")
return redirect(request.url)
#create a new secure filename
else:
filename = secure_filename(book.filename)
basename, ext = os.path.splitext(filename)
folder = os.path.join(app.config["BOOK_UPLOADS"], basename)
try:
os.makedirs(folder)
except FileExistsError:
pass
bookpath = os.path.join(folder, filename)
x=2
while os.path.exists(bookpath):
bookpath = os.path.join(folder, basename + "_" + str(x) + ext)
x+=1
book.save(bookpath)
print("Book saved")
# flash("Book saved")
return redirect(request.url)
return render_template("public/upload_book.html")
#Form for watermark
@app.route("/watermark", methods=["GET", "POST"])
def watermark():
if request.method == "POST":
req = request.form
name = req["name"]
scan = req["scan"]
time = req["time"]
source = req["source"]
anecdote = req["anecdote"]
fieldnames = [name, scan, time, source, anecdote]
print(fieldnames)
with open((os.path.join(app.config["BOOK_UPLOADS"], str(now)) + ".txt"), 'w') as f:
f.write(str(fieldnames))
return redirect(request.url)
return render_template("public/watermark_form.html")
#Request a book
# Link database
db = dataset.connect('sqlite:///file.db')
# create table
table = db['requested']
@app.route('/request', methods=['GET'])
def request_form():
return render_template('public/request_form.html')
# from GET /request_book render request_book.html
@app.route('/request_book', methods=['GET'])
def request_book():
books = table.find(order_by='-id')
return render_template('public/request_book.html', books=books)
# from POST /submit store data in the database and forward to the request_book
@app.route('/submit', methods=['POST'])
def submit():
book = dict(title=request.form['title'], author=request.form['author'], publisher=request.form['publisher'], year=request.form['year'], extention=request.form['extention'])
table.insert(book)
return redirect(url_for('request_book'))