add download func

This commit is contained in:
2022-11-06 23:05:43 +03:00
parent de3e20e0cb
commit 347f467735
44 changed files with 53 additions and 50 deletions

81
app.py
View File

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

View File

@@ -30,8 +30,13 @@ header {
background-color: #fef6dd; background-color: #fef6dd;
border-radius: 12px; border-radius: 12px;
box-shadow: 0px 5px 10px 2px rgba(34, 60, 80, 0.2); box-shadow: 0px 5px 10px 2px rgba(34, 60, 80, 0.2);
height: 450px;
} }
.card-img-top{
height: 250px;
}
.btn { .btn {

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@@ -8,7 +8,7 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<link rel="stylesheet" href="{{ url_for('static', filename='css/adminpage.css')}}"> <link rel="stylesheet" href="{{ url_for('static', filename='css/adminpage.css')}}">
<title>3d Detals</title> <title>3d Detals adminka</title>
</head> </head>
<body> <body>
@@ -20,11 +20,11 @@
<div class="row "> <div class="row ">
<form method="post" enctype=multipart/form-data action="/uploader"> <form method="post" enctype=multipart/form-data action="/uploader">
<label class=" d-block p-2">Выберите изображение </label> <label class=" d-block p-2">Выберите изображение </label>
<input class=" formblock d-block p-2" type="file" name="imgfile" /> <input class=" formblock d-block p-2" type="file" name="img" />
<label class=" d-block p-2">Выберите исходный file </label> <label class=" d-block p-2">Выберите исходный file </label>
<input class=" formblock d-block p-2" type="file" name="origfile" /> <input class=" formblock d-block p-2" type="file" name="src" />
<label class=" d-block p-2">Выберите stl file </label> <label class=" d-block p-2">Выберите stl file </label>
<input class=" formblock d-block p-2" type="file" name="stlfile" /> <input class=" formblock d-block p-2" type="file" name="stl" />
<label > Напечатано </label> <label > Напечатано </label>
<input type="checkbox" class="bxigchekcbox" id="printed" name="isprinted"> <input type="checkbox" class="bxigchekcbox" id="printed" name="isprinted">
<input class="btn d-block p-2" type="submit" value="Upload"> <input class="btn d-block p-2" type="submit" value="Upload">

View File

@@ -17,18 +17,19 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
{% for i in range(10) %} {% for key, value in detals.items() %}
<div class="col-md-4"> <div class="col-md-4">
<div class="card mobile-margin"> <div class="card mobile-margin">
<img class="card-img-top" src="{{url_for('static', filename='img/1.jpg')}}" alt="1 image"> <img class="card-img-top" src="{{url_for('static', filename='uploads/img/' + value['img'])}}">
<div class="card-body"> <div class="card-body">
<dv class="row"> <dv class="row">
<div class="col"> <div class="col">
<button class="btn btn-block mobile-margin">Скачать исходник</button> <a href="{{url_for('static', filename='uploads/src/' + value['src'])}}" class="btn btn-block mobile-margin" download >Скачать исходник</a>
</div> </div>
<div class="col"> <div class="col">
<button class="btn btn-block mobile-margin">Скачать stl </button> <a href="{{url_for('static', filename='uploads/stl/' + value['stl'])}}" class="btn btn-block mobile-margin" download>Скачать stl </a>
</div> </div>
<div class="row"> <div class="row">
<div class="col d-flex justify-content-center"> <div class="col d-flex justify-content-center">