pre-commit changes
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
from Database.sql_provider import SQLProvider
|
||||
from Database.work import select_list
|
||||
from Database.DBconnect import DBContextManager
|
||||
|
||||
from flask import current_app, session
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from datetime import date
|
||||
|
||||
from cache.wrapper import fetch_from_cache
|
||||
import os
|
||||
from Database.DBconnect import DBContextManager
|
||||
from Database.sql_provider import SQLProvider
|
||||
from Database.work import select_list
|
||||
from flask import current_app, session
|
||||
|
||||
|
||||
@dataclass
|
||||
class InfoRespronse:
|
||||
@@ -14,107 +15,115 @@ class InfoRespronse:
|
||||
error_message: str
|
||||
status: bool
|
||||
|
||||
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql'))
|
||||
|
||||
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), "sql"))
|
||||
|
||||
|
||||
def clear():
|
||||
if session.get('waybill',{}):
|
||||
session.pop('waybill')
|
||||
if session.get("waybill", {}):
|
||||
session.pop("waybill")
|
||||
|
||||
|
||||
def form_waybill() -> list:
|
||||
db_config = current_app.config['db_config']
|
||||
cache_config = current_app.config['cache_config']
|
||||
db_config = current_app.config["db_config"]
|
||||
cache_config = current_app.config["cache_config"]
|
||||
|
||||
current_waybill = session.get('waybill',{})
|
||||
current_waybill = session.get("waybill", {})
|
||||
waybill = []
|
||||
for k,v in current_waybill.items():
|
||||
_sql = sql_provider.get('one_good.sql', dict(work_id=k))
|
||||
cache_select = fetch_from_cache(f'product_{k}', cache_config)(select_list)
|
||||
for k, v in current_waybill.items():
|
||||
_sql = sql_provider.get("one_good.sql", dict(work_id=k))
|
||||
cache_select = fetch_from_cache(f"product_{k}", cache_config)(select_list)
|
||||
product = cache_select(db_config, _sql)[0]
|
||||
product['amount'] = v
|
||||
product["amount"] = v
|
||||
waybill.append(product)
|
||||
return waybill
|
||||
|
||||
def index_waybill() -> list:
|
||||
db_config = current_app.config['db_config']
|
||||
cache_config = current_app.config['cache_config']
|
||||
|
||||
cache_select = fetch_from_cache('items_cached', cache_config)(select_list)
|
||||
_sql = sql_provider.get('goods.sql', {})
|
||||
def index_waybill() -> list:
|
||||
db_config = current_app.config["db_config"]
|
||||
cache_config = current_app.config["cache_config"]
|
||||
|
||||
cache_select = fetch_from_cache("items_cached", cache_config)(select_list)
|
||||
_sql = sql_provider.get("goods.sql", {})
|
||||
products = cache_select(db_config, _sql)
|
||||
if products == None:
|
||||
if products is None:
|
||||
return []
|
||||
|
||||
|
||||
return products
|
||||
|
||||
def button_click(request):
|
||||
db_config = current_app.config['db_config']
|
||||
data = dict(work_id=int(request.form['product_display']))
|
||||
|
||||
_sql = sql_provider.get('one_good.sql', data)
|
||||
def button_click(request):
|
||||
db_config = current_app.config["db_config"]
|
||||
data = dict(work_id=int(request.form["product_display"]))
|
||||
|
||||
_sql = sql_provider.get("one_good.sql", data)
|
||||
result = select_list(db_config, _sql)
|
||||
if result == None:
|
||||
if result is None:
|
||||
return False
|
||||
|
||||
|
||||
product = result[0]
|
||||
|
||||
if request.form.get('add'):
|
||||
if 'waybill' not in session:
|
||||
session['waybill'] = dict()
|
||||
session['total'] = '0'
|
||||
|
||||
if str(product['work_id']) in session['waybill']:
|
||||
pr_id = product['work_id']
|
||||
price = product['price']
|
||||
amount = int(session['waybill'][str(pr_id)])
|
||||
session['waybill'][str(pr_id)] = str(amount+1)
|
||||
session['total'] = str(int(session['total']) + price)
|
||||
if request.form.get("add"):
|
||||
if "waybill" not in session:
|
||||
session["waybill"] = dict()
|
||||
session["total"] = "0"
|
||||
|
||||
if str(product["work_id"]) in session["waybill"]:
|
||||
pr_id = product["work_id"]
|
||||
price = product["price"]
|
||||
amount = int(session["waybill"][str(pr_id)])
|
||||
session["waybill"][str(pr_id)] = str(amount + 1)
|
||||
session["total"] = str(int(session["total"]) + price)
|
||||
session.modified = True
|
||||
else:
|
||||
print("NEW WORKPIECE")
|
||||
pr_id = product['work_id']
|
||||
price = product['price']
|
||||
session['waybill'][str(pr_id)] = '1'
|
||||
session['total'] = str(int(session['total']) + price)
|
||||
print(session['waybill'])
|
||||
pr_id = product["work_id"]
|
||||
price = product["price"]
|
||||
session["waybill"][str(pr_id)] = "1"
|
||||
session["total"] = str(int(session["total"]) + price)
|
||||
print(session["waybill"])
|
||||
session.modified = True
|
||||
|
||||
elif request.form.get('product_display_minus'):
|
||||
elif request.form.get("product_display_minus"):
|
||||
# decreasing count in waybill
|
||||
|
||||
amount = int(session['waybill'][str(product['work_id'])])
|
||||
|
||||
amount = int(session["waybill"][str(product["work_id"])])
|
||||
if amount == 1:
|
||||
session['waybill'].pop(str(product['work_id']))
|
||||
session["waybill"].pop(str(product["work_id"]))
|
||||
else:
|
||||
session['waybill'][str(product['work_id'])] = str(amount-1)
|
||||
session['total'] = str(int(session['total']) - product['price'])
|
||||
session["waybill"][str(product["work_id"])] = str(amount - 1)
|
||||
session["total"] = str(int(session["total"]) - product["price"])
|
||||
session.modified = True
|
||||
return True
|
||||
|
||||
def transaction_order_model(user_id: int, current_date: date):
|
||||
|
||||
db_config = current_app.config['db_config']
|
||||
waybill = session.get('waybill',{})
|
||||
total = session.get('total', 0)
|
||||
def transaction_order_model(user_id: int, current_date: date):
|
||||
db_config = current_app.config["db_config"]
|
||||
waybill = session.get("waybill", {})
|
||||
total = session.get("total", 0)
|
||||
|
||||
# Чтобы всё это шло как одна транзакция
|
||||
with DBContextManager(db_config) as cursor:
|
||||
|
||||
data = dict(e_user_id=user_id, e_order_date=current_date, e_total=total)
|
||||
try:
|
||||
_sql = sql_provider.get('create_order.sql', data)
|
||||
_sql = sql_provider.get("create_order.sql", data)
|
||||
cursor.execute(_sql)
|
||||
|
||||
|
||||
order_id = cursor.lastrowid
|
||||
for key, value in waybill.items():
|
||||
_sql = sql_provider.get('insert_order_line.sql',
|
||||
dict(e_order_id = order_id,
|
||||
e_price = 0,
|
||||
e_prod_id = int(key),
|
||||
e_amount = int(value)))
|
||||
_sql = sql_provider.get(
|
||||
"insert_order_line.sql",
|
||||
dict(
|
||||
e_order_id=order_id,
|
||||
e_price=0,
|
||||
e_prod_id=int(key),
|
||||
e_amount=int(value),
|
||||
),
|
||||
)
|
||||
cursor.execute(_sql)
|
||||
except:
|
||||
return InfoRespronse((), error_message="Заказ не был создан", status=False)
|
||||
|
||||
result = tuple([order_id])
|
||||
clear()
|
||||
return InfoRespronse(result, error_message="", status=True)
|
||||
return InfoRespronse(result, error_message="", status=True)
|
||||
|
||||
Reference in New Issue
Block a user