refactoring file uploading

This commit is contained in:
Александр Геннадьевич Сальный
2022-10-22 20:17:12 +03:00
parent a75a386fed
commit 65a52fe64c

37
app.py
View File

@@ -12,60 +12,49 @@ from models import PrintedDetal
logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG) logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG)
UPLOAD_FOLDER = 'static/img/uploads' UPLOAD_IMG_FOLDER = os.path.join('static', 'img', 'uploads')
UPLOAD_FOLDER2 = 'static/stl' UPLOAD_STL_FOLDER = os.path.join('static', 'stl')
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'stl'} ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'stl'}
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['UPLOAD_FOLDER'] = UPLOAD_IMG_FOLDER
app.config['UPLOAD_FOLDER2'] = UPLOAD_FOLDER2 app.config['UPLOAD_FOLDER2'] = UPLOAD_STL_FOLDER
db.init_app(app) db.init_app(app)
@app.route("/") @app.route("/")
def index(): def index():
print(app.config['UPLOAD_FOLDER2'])
print(type(app.config['UPLOAD_FOLDER']))
detals = PrintedDetal.query.all() detals = PrintedDetal.query.all()
logging.debug(detals[0].imgpath)
return render_template("index.html") return render_template("index.html", detals=detals)
@app.route("/admin", methods=['GET', 'POST']) @app.route("/admin", methods=['GET', 'POST'])
def admipage(): def admipage():
stl_folder = 'stl/'
img_folder = 'img/uploads/'
if request.method == 'POST': if request.method == 'POST':
image_file = request.files['images'] image_file = request.files['images']
stl_file = request.files['stl'] stl_file = request.files['stl']
filename = image_file.filename image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], image_file.filename))
path_img = img_folder + filename stl_file.save(os.path.join(app.config['UPLOAD_FOLDER2'], stl_file.filename))
logging.debug(path_img)
image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filename = stl_file.filename
path_stl = stl_folder + filename
stl_file.save(os.path.join(app.config['UPLOAD_FOLDER2'], filename))
if request.form.get('isprinted') == 'on': if request.form.get('isprinted') == 'on':
isprinered = True isprinered = True
else: else:
isprinered = False isprinered = False
detal = PrintedDetal(imgpath=str(path_img), stlpath=str( detal = PrintedDetal(imgpath=image_file.filename,
path_stl), isprinted=isprinered) stlpath=stl_file.filename,
isprinted=isprinered)
db.session.add(detal) db.session.add(detal)
db.session.commit() db.session.commit()
logging.debug(path_img)
logging.debug(path_stl)
logging.debug(isprinered)
return render_template("adminpage.html") return render_template("adminpage.html")
else: else: