add writte to DB

This commit is contained in:
Александр Геннадьевич Сальный
2022-10-18 21:12:25 +03:00
parent cae7dc1dac
commit a75a386fed
2 changed files with 40 additions and 15 deletions

51
app.py
View File

@@ -1,47 +1,70 @@
# -*- coding: utf-8 -*-
from turtle import st from turtle import st
from flask import Flask, render_template, request, flash, redirect, url_for from flask import Flask, render_template, request, flash, redirect, url_for
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
import os import os
from models import db from models import db
from models import PrintedDetal from models import PrintedDetal
import logging
from models import PrintedDetal
logging.basicConfig(filename='app.log', encoding='utf-8', level=logging.DEBUG)
UPLOAD_FOLDER = 'uploads' UPLOAD_FOLDER = 'static/img/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} UPLOAD_FOLDER2 = '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_FOLDER app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['UPLOAD_FOLDER2'] = UPLOAD_FOLDER2
db.init_app(app) db.init_app(app)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/") @app.route("/")
def index(): def index():
detals = PrintedDetal.query.all() detals = PrintedDetal.query.all()
print(detals[0].imgpath) logging.debug(detals[0].imgpath)
return render_template("index.html") return render_template("index.html")
@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 = secure_filename(image_file.filename) filename = image_file.filename
image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) path_img = img_folder + filename
print(filename)
filename = secure_filename(stl_file.filename) logging.debug(path_img)
stl_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print(filename) 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':
isprinered = True
else:
isprinered = False
detal = PrintedDetal(imgpath=str(path_img), stlpath=str(
path_stl), isprinted=isprinered)
db.session.add(detal)
db.session.commit()
logging.debug(path_img)
logging.debug(path_stl)
logging.debug(isprinered)
return render_template("adminpage.html") return render_template("adminpage.html")

View File

@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() db = SQLAlchemy()
class PrintedDetal(db.Model): class PrintedDetal(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True, autoincrement=True)
imgpath = db.Column(db.String(3000)) imgpath = db.Column(db.String(3000))
stlpath = db.Column(db.String(3000)) stlpath = db.Column(db.String(3000))
isprinted = db.Column(db.Boolean()) isprinted = db.Column(db.Boolean())