add download func
This commit is contained in:
81
app.py
81
app.py
@@ -1,23 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from flask import Flask, render_template, request, flash, redirect, url_for
|
||||
from flask import Flask, render_template, request, flash, redirect, url_for, send_file
|
||||
from werkzeug.utils import secure_filename
|
||||
import os, secrets
|
||||
import os
|
||||
import secrets
|
||||
from models import db, PrintedDetal
|
||||
import logging
|
||||
|
||||
logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG)
|
||||
|
||||
|
||||
UPLOAD_IMG_FOLDER = os.path.join('static', 'img', 'uploads')
|
||||
UPLOAD_STL_FOLDER = os.path.join('static', 'stl')
|
||||
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'stl'}
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_IMG_FOLDER
|
||||
app.config['UPLOAD_FOLDER2'] = UPLOAD_STL_FOLDER
|
||||
app.config['UPLOAD_IMG_FOLDER'] = os.path.join('static', 'uploads', 'img')
|
||||
app.config['UPLOAD_STL_FOLDER'] = os.path.join('static', 'uploads', 'stl')
|
||||
app.config['UPLOAD_SRC_FOLDER'] = os.path.join('static', 'uploads', 'src')
|
||||
|
||||
db.init_app(app)
|
||||
db.init_app(app)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@@ -27,74 +25,73 @@ def index():
|
||||
|
||||
detalsQuery = PrintedDetal.query.all()
|
||||
|
||||
|
||||
|
||||
for detal in detalsQuery:
|
||||
detals = {}
|
||||
detals['img'] = detal.img
|
||||
detals['stl'] = detal.stl
|
||||
detals['src']= detal.src
|
||||
detals['stl'] = detal.stl
|
||||
detals['src'] = detal.src
|
||||
detals['isprinted'] = detal.isprinted
|
||||
|
||||
|
||||
allDetals[detal.id] = detals
|
||||
|
||||
|
||||
print(allDetals)
|
||||
|
||||
logging.debug(allDetals)
|
||||
|
||||
return render_template("index.html") # , detals=detals)
|
||||
return render_template("index.html", detals=allDetals)
|
||||
|
||||
|
||||
@app.route("/admin", methods=['GET', 'POST'])
|
||||
def admipage():
|
||||
logging.debug("adminpage loaded")
|
||||
|
||||
|
||||
return render_template("adminpage.html")
|
||||
|
||||
|
||||
@app.route('/uploader', methods=['GET', 'POST'])
|
||||
def upload_file():
|
||||
hex = secrets.token_hex(15)
|
||||
|
||||
|
||||
if request.method == 'POST':
|
||||
image_file = request.files['imgfile']
|
||||
stl_file = request.files['stlfile']
|
||||
src_file = request.files['stlfile']
|
||||
image_file = request.files['img']
|
||||
stl_file = request.files['stl']
|
||||
src_file = request.files['src']
|
||||
|
||||
if len(image_file.filename) > 0:
|
||||
image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(image_file.filename)))
|
||||
|
||||
if len(stl_file.filename) >0:
|
||||
stl_file.save(os.path.join(app.config['UPLOAD_FOLDER2'], secure_filename(stl_file.filename)))
|
||||
|
||||
if len(src_file.filename) >0:
|
||||
src_file.save(os.path.join(app.config['UPLOAD_FOLDER2'], secure_filename(src_file.filename)))
|
||||
filename = image_file.filename.split('.')
|
||||
image_file.filename = secrets.token_hex(15) + '.' + filename[-1]
|
||||
image_file.save(os.path.join(
|
||||
app.config['UPLOAD_IMG_FOLDER'], image_file.filename))
|
||||
|
||||
if len(stl_file.filename) > 0:
|
||||
filename = stl_file.filename.split('.')
|
||||
stl_file.filename = secrets.token_hex(15) + '.' + filename[-1]
|
||||
stl_file.save(os.path.join(app.config['UPLOAD_STL_FOLDER'],stl_file.filename))
|
||||
|
||||
if len(src_file.filename) > 0:
|
||||
filename = src_file.filename.split('.')
|
||||
src_file.filename = secrets.token_hex(15) + '.' + filename[-1]
|
||||
src_file.save(os.path.join(app.config['UPLOAD_SRC_FOLDER'], src_file.filename))
|
||||
|
||||
|
||||
if request.form.get('isprinted') == 'on':
|
||||
isprinered = True
|
||||
else:
|
||||
isprinered = False
|
||||
|
||||
|
||||
logging.debug(image_file)
|
||||
logging.debug(stl_file)
|
||||
logging.debug(image_file.filename)
|
||||
logging.debug(stl_file.filename)
|
||||
logging.debug(src_file.filename)
|
||||
logging.debug(isprinered)
|
||||
|
||||
detal = PrintedDetal(img=image_file.filename,
|
||||
stl=stl_file.filename,
|
||||
src=src_file.filename,
|
||||
isprinted=isprinered)
|
||||
stl=stl_file.filename,
|
||||
src=src_file.filename,
|
||||
isprinted=isprinered)
|
||||
db.session.add(detal)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
return redirect(url_for('admipage'))
|
||||
|
||||
|
||||
@app.route('/download/<filename>', methods=['GET', 'POST'])
|
||||
def download(filename):
|
||||
return send_file(app.config['UPLOAD_SRC_FOLDER']+'/'+ filename, as_attachment=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug="True", port="3800")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user