Merge branch 'Waybill' of ParkSuMin/Cursovaya into main

This commit is contained in:
2024-12-03 22:38:05 +00:00
committed by Gogs
27 changed files with 425 additions and 174 deletions

View File

@@ -12,6 +12,7 @@ def auth():
auth_data = auth_model(data)
if auth_data.status:
session.update({
'user_id': auth_data.result[0]['user_ID'],
'login': auth_data.result[0]['login'],
'access_user': data['access'],
'role': auth_data.result[0]['user_role'],

View File

@@ -1,4 +1,4 @@
SELECT login, user_role FROM $access
SELECT user_ID, login, user_role FROM $access
WHERE login = '$login'
AND password = '$password'
LIMIT 1;

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<html lang="en" data-bs-theme="dark">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

View File

@@ -1,11 +1,47 @@
from flask import request, Blueprint, render_template, session, current_app, redirect, url_for
from flask import request, Blueprint, render_template, session, redirect, url_for
from checker import check_auth
waybill_bp = Blueprint('waybill_bp', __name__, template_folder='templates')
from .model import index_waybill, form_waybill, clear, button_click, transaction_order_model
from datetime import date
@waybill_bp.route('/', methods=['GET', 'POST'])
waybill_bp = Blueprint('waybill_bp', __name__, template_folder='templates', static_folder='static')
@waybill_bp.route('/', methods=['GET'])
@check_auth
def waybill():
if request.method == 'GET':
return render_template('waybill.html')
print(request.form)
return 'OK'
def index():
lst = index_waybill()
if lst is not None:
waybill = form_waybill()
return render_template('waybill.html', items=lst, waybill=waybill)
else:
return render_template('error.html', error_message="Ошибка в подключении к СУБД")
@waybill_bp.route('/', methods=['POST'])
@check_auth
def waybill_main():
status = button_click(request)
if status:
return redirect(url_for('waybill_bp.index'))
else:
return render_template("error.html", error_message="Товар не был добавлен в корзину")
@waybill_bp.route('/clear', methods=['GET'])
@check_auth
def clear_waybill():
clear()
return redirect(url_for('waybill_bp.index'))
@waybill_bp.route('/save_order')
@check_auth
def save_order():
if not session.get('waybill',{}):
return redirect(url_for('waybill_bp.index'))
user_id = session.get('user_id',"")
current_date = date.today().strftime("%Y-%m-%d") + ' ' + date.today().strftime("%H:%M:%S")
result = transaction_order_model(user_id, current_date)
if result.status:
print("Order success")
return render_template("order_finish.html", order_id = result.result[0])
else:
return render_template("error.html", error_message=result.error_message)

42
App/Waybill/cache/__init__.py vendored Normal file
View File

@@ -0,0 +1,42 @@
import json
from redis import Redis, ConnectionError, DataError
class RedisCache:
def __init__(self, config: dict):
self.config = config
self.conn = self._connect()
def _connect(self):
try:
conn = Redis(**self.config)
return conn
except DataError as err:
print(err)
return None
def set_value(self, name: str, value_dict: dict, ttl: int):
try:
value_js = json.dumps(value_dict)
self.conn.set(name=name, value=value_js)
if ttl > 0:
self.conn.expire(name, ttl)
return True
except ConnectionError as err:
print(err)
return False
def get_value(self, name: str):
try:
value_js = self.conn.get(name)
except:
return None
value_js = self.conn.get(name)
if value_js:
value_dict = json.loads(value_js)
return value_dict
else:
return None
def __exit__(self, exc_type, exc_val, exc_tb):
self.conn.close()
return True

26
App/Waybill/cache/wrapper.py vendored Normal file
View File

@@ -0,0 +1,26 @@
from functools import wraps
from Waybill.cache import RedisCache
def fetch_from_cache(cache_name: str, cache_config: dict):
cache_conn = RedisCache(cache_config['redis'])
ttl = cache_config['ttl']
def decorator(f):
# как оно работает
# лезем в кэш и смотрим, есть ли там что-то
# если есть, то возвращаем кэшированную информацию
# если нет, то запускаем декорируемую функцию
# достаём оттуда информацию
# заносим её в кэш
# возвращаем кэшированную информацию
@wraps(f)
def wrapper(*args, **kwargs):
cached_value = cache_conn.get_value(cache_name)
print("cached_value=", cached_value)
if cached_value:
return cached_value
response = f(*args, **kwargs)
print("response=", response)
cache_conn.set_value(cache_name,response,ttl)
return response
return wrapper
return decorator

View File

@@ -0,0 +1,34 @@
import pymysql
from pymysql.err import *
class DBContextManager:
def __init__(self, db_config : dict):
self.db_config = db_config
self.connection = None
self.cursor = None
def __enter__(self):
try:
self.connection = pymysql.connect(
host=self.db_config['host'],
port=self.db_config['port'],
user=self.db_config['user'],
password=self.db_config['password'],
db=self.db_config['db']
)
self.cursor = self.connection.cursor()
return self.cursor
except (OperationalError, KeyError) as err:
print(err.args)
return None
def __exit__(self, exc_type, exc_val, exc_tb):
if self.connection and self.cursor:
if exc_type:
print(exc_type, '\n', exc_val)
self.connection.rollback()
else:
self.connection.commit()
self.cursor.close()
self.connection.close()
return True

View File

View File

@@ -0,0 +1,14 @@
import os
from string import Template
class SQLProvider:
def __init__(self, file_path):
self.scripts = {}
for file in os.listdir(file_path):
_sql = open(f'{file_path}/{file}').read()
self.scripts[file] = Template(_sql)
def get(self, name, params) -> dict:
if name not in self.scripts:
raise ValueError(f'SQL template {name} not found')
return self.scripts[name].substitute(**params)

16
App/Waybill/db/work.py Normal file
View File

@@ -0,0 +1,16 @@
from .DBconnect import DBContextManager
def select_list(db_config, sql) -> list:
with DBContextManager(db_config) as cursor:
if cursor is None:
raise ValueError("Cursor not created")
else:
cursor.execute(sql)
result = cursor.fetchall()
schema = [item[0] for item in cursor.description]
lst = [dict(zip(schema, row)) for row in result]
return lst
def transaction(cursor, sql):
cursor.execute(sql)
return True

124
App/Waybill/model.py Normal file
View File

@@ -0,0 +1,124 @@
from .db.sql_provider import SQLProvider
from .db.work import select_list, transaction
from .db.DBconnect import DBContextManager
from flask import current_app, session
from dataclasses import dataclass
from datetime import date
from .cache.wrapper import fetch_from_cache
import os
@dataclass
class InfoRespronse:
result: tuple
error_message: str
status: bool
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql'))
def clear():
if session.get('waybill',{}):
session.pop('waybill')
def form_waybill() -> list:
current_waybill = session.get('waybill',{})
waybill = []
for k,v in current_waybill.items():
_sql = sql_provider.get('one_good.sql', dict(work_id=k))
product = select_list(current_app.config['db_config'], _sql)[0]
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', {})
products = cache_select(db_config, _sql)
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)
result = select_list(db_config, _sql)
if result == 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)
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'])
session.modified = True
# elif request.form.get('product_display_plus'):
# # increasing count in waybill
# amount = int(session['waybill'][str(product['work_id'])])
# session['waybill'][str(product['work_id'])] = str(amount + 1)
# session.modified = True
elif request.form.get('product_display_minus'):
# decreasing count in waybill
amount = int(session['waybill'][str(product['work_id'])])
if amount == 1:
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.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)
# Чтобы всё это шло как одна транзакция
with DBContextManager(db_config) as cursor:
data = dict(e_user_id=user_id, e_order_date=current_date, e_total=total)
_sql = sql_provider.get('create_order.sql', data)
try:
cursor.execute(_sql)
except:
return InfoRespronse((), error_message="Заказ не был создан", status=False)
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_prod_id = int(key),
e_amount = int(value)))
try:
cursor.execute(_sql)
except:
return InfoRespronse((), error_message="Заказ не был создан", status=False)
result = tuple([order_id])
clear()
return InfoRespronse(result, error_message="", status=True)

View File

@@ -0,0 +1 @@
INSERT INTO `waybill` (`user_id`,`waybill_date`, `total`) VALUES ($e_user_id, '$e_order_date', $e_total);

View File

@@ -0,0 +1 @@
SELECT work_id, name, price, material, count FROM workpiece

View File

@@ -0,0 +1,2 @@
INSERT INTO `waybill_lines` VALUES ($e_order_id, $e_prod_id,
(SELECT price FROM workpiece WHERE work_id = $e_prod_id), $e_amount);

View File

@@ -0,0 +1,3 @@
SELECT `work_id`, `name`, `price`, `weight`
FROM `workpiece`
WHERE `work_id` = $work_id;

View File

@@ -0,0 +1,10 @@
.card {
background: #fff;
border: 1px solid #ddd;
color: black;
border-radius: 10px;
width: 250px;
padding: 15px;
margin-bottom: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

View File

@@ -0,0 +1,24 @@
{% macro render_item(item, show_amount = False, show_form = False) %}
<div class="card">
<div class="card-body">
<h6 class="card-title">{{ item['name'] }}</h6>
<p class="card-text">Цена: {{ item['price'] }} ₽</p>
{% if show_amount %}
<span>Количество: {{item['amount']}}</span><br>
<form method="POST" action="">
<input type="hidden" name="product_display" value="{{item['work_id']}}" />
<button type="submit" name="product_display_minus" value="minus" class="btn btn-danger">-</button>
<!-- <button type="submit" name="product_display_plus" value="plus" class="btn btn-success">+</button> -->
</form>
{% endif %}
{% if show_form %}
<form method="POST" action="">
<input type="hidden" name="product_display" value="{{item['work_id']}}" />
<input type="submit" class="btn btn-primary" value="Добавить" name="add" />
</form>
{% endif %}
</div>
</div>
{% endmacro %}

View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<title>Накладная оформлена</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="d-flex flex-column h-100">
<main class="flex-shrink-0">
<div class="container">
<h1 class="mt-5">Накладная оформлена</h1>
<p class="lead">Номер вашей накладной: {{ order_id }}</p>
<div class="mt-4">
<a href="{{ url_for('index') }}" class="btn btn-primary me-2">На главную страницу</a>
</div>
</div>
</main>
</body>
</html>

View File

@@ -1,48 +1,48 @@
{% import 'card.html' as card %}
<!DOCTYPE html>
<html lang="en">
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<title>Авторизация</title>
<link href="/static/css/waybill.css" type="text/css" rel="stylesheet">
<link href="/static/css/main.css" type="text/css" rel="stylesheet">
<title>Оформление накладной</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="static/css/waybill.css" type="text/css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h1>Заглушка для примера составления накладной</h1>
<form method="POST">
<div class="container">
<!-- Пример карточки -->
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 1</h3>
<p class="price">Цена: 500 руб.</p>
<p class="quantity">Количество: 10 шт.</p>
<div class="input-container">
<input type="number" name="1" placeholder="Введите количество" min="1" max="1000" value="0">
<div class="container mt-4">
<div class="row">
<!-- Секция заготовок -->
<div class="col-md-8">
<h4>Список заготовок</h4>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
{% for item in items %}
<div class="col">
{{ card.render_item(item, show_form = True, show_amount = False) }}
</div>
{% endfor %}
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 2</h3>
<p class="price">Цена: 1200 руб.</p>
<p class="quantity">Количество: 5 шт.</p>
<div class="input-container">
<input type="number" name="2" placeholder="Введите количество" min="0" max="1000" value="0">
<!-- Секция накладной -->
<div class="col-md-4">
<h4>Накладная</h4>
{% if waybill %}
{% for item in waybill %}
{{ card.render_item(item, show_form = False, show_amount = True) }}
{% endfor %}
<div class="d-grid gap-2 mt-3">
<a href="{{url_for('waybill_bp.save_order')}}"><button class="btn btn-primary">Оформить накладную</button></a>
<a href="{{url_for('index')}}"><button class="btn btn-secondary">На главную</button></a>
</div>
</div>
<div class="card">
<img src="https://via.placeholder.com/250x150" alt="Фото товара">
<h3>Название товара 3</h3>
<p class="price">Цена: 800 руб.</p>
<p class="quantity">Количество: 7 шт.</p>
<div class="input-container">
<input type="number" name="3" placeholder="Введите количество" min="1" max="1000" value="0">
{% else %}
<span>Ваша накладная пуста</span>
<div class="d-grid gap-2 mt-3">
<a href="{{url_for('index')}}"><button class="btn btn-secondary">На главную</button></a>
</div>
{% endif %}
</div>
</div>
<div class="button-container">
<button type="submit">Создать накладную</button>
<a href="{{ url_for('index') }}"><button>На главную страницу</button></a>
</div>
</form>
</div>
</body>
</html>
</html>

View File

@@ -1,23 +0,0 @@
from Database.sql_provider import SQLProvider
from Database.select import select_list
from flask import current_app
from dataclasses import dataclass
import os
@dataclass
class InfoRespronse:
result: tuple
error_message: str
status: bool
sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql'))
def waybill_model(input_data) -> InfoRespronse:
_sql = sql_provider.get('waybill.sql', input_data)
result = select_list(current_app.config['db_config'], _sql)
if result is None:
return InfoRespronse((),
error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором',
status=False)
return InfoRespronse(result, error_message='', status=True)

View File

@@ -10,7 +10,8 @@ app.secret_key = 'suplex'
app.config.update(
db_config=json.load(open(os.path.join(os.path.dirname(__file__), 'data/config.json'))),
db_access=json.load(open(os.path.join(os.path.dirname(__file__), 'data/db_access.json')))
db_access=json.load(open(os.path.join(os.path.dirname(__file__), 'data/db_access.json'))),
cache_config=json.load(open(os.path.join(os.path.dirname(__file__), 'data/redis_config.json')))
)
app.register_blueprint(requests_bp, url_prefix='/requests')

View File

@@ -1,5 +1,5 @@
{
"host": "localhost",
"host": "89.46.42.74",
"port": 3306,
"user": "manager",
"password": "ilikepizza",

View File

@@ -0,0 +1,8 @@
{
"redis": {
"host" :"89.46.42.74",
"port": 6379,
"db" : 0
},
"ttl": 60
}

View File

@@ -1,14 +0,0 @@
table {
border: 1px solid black;
border-collapse: collapse;
background: white;
margin: 0 auto;
}
th {
background-color: #d3d3d3;
}
th, td {
border: 1px solid black;
padding: 5px;
text-align: center;
}

View File

@@ -1,48 +0,0 @@
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
justify-content: center;
}
.card {
background: #fff;
border: 1px solid #ddd;
border-radius: 10px;
width: 250px;
padding: 15px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.card img {
width: 100%;
height: 150px;
object-fit: cover;
border-radius: 5px;
}
.card h3 {
margin: 10px 0;
font-size: 18px;
}
.price {
font-weight: bold;
color: #28a745;
}
.quantity {
margin: 10px 0;
font-weight: bold;
color: #007bff;
}
.input-container {
margin: 10px 0;
}
.input-container input {
width: 80%;
padding: 8px;
font-size: 14px;
border: 1px solid #ddd;
border-radius: 5px;
}
.button-container {
text-align: center;
margin: 20px 0;
}

View File

@@ -35,7 +35,20 @@
</header>
<main class="flex-shrink-0">
<div class="d-flex gap-2 justify-content-center py-5">
{% if ses['access_user'] == 'internal_users' %}
{% if 'role' not in ses %}
<div class="text-center">
<h2 class="fs-2 text-body-emphasis mb-4">Для доступа к функциям системы необходимо авторизоваться</h2>
</div>
{% endif %}
{% if 'role' in ses and ses['access_user'] == 'external_users' %}
<div class="text-center">
<h2 class="fs-2 text-body-emphasis mb-4">Новая накладная</h2>
<a href="{{ url_for('waybill_bp.index') }}">
<button class="btn btn-primary btn-lg">Перейти</button>
</a>
</div>
{% endif %}
{% if 'role' in ses and ses['access_user'] == 'internal_users' %}
<div class="d-flex justify-content-center">
<div class="feature col">
<div
@@ -43,11 +56,6 @@
>
</div>
<h3 class="fs-2 text-body-emphasis">Запросы</h3>
<!-- <p>
Paragraph of text beneath the heading to explain the heading. We'll
add onto it with another sentence and probably just keep going until
we run out of words.
</p> -->
<a href="{{ url_for('requests_bp.requests') }}">
<button class="btn btn-primary">Перейти</button>
</a>
@@ -57,49 +65,14 @@
>
</div>
<h3 class="fs-2 text-body-emphasis">Отчёты</h3>
<!-- <p>
Paragraph of text beneath the heading to explain the heading. We'll
add onto it with another sentence and probably just keep going until
we run out of words.
</p> -->
<a href="{{ url_for('report_bp.menu') }}">
<button class="btn btn-primary">Перейти</button>
</a>
</div>
</div>
</div>
{% else %}
<div class="feature col">
<div
class="feature-icon d-inline-flex align-items-center justify-content-center text-bg-primary bg-gradient fs-2 mb-3"
>
</div>
{% endif %}
</div>
</main>
<!-- {% if 'role' in ses %}
<label class="display-5 fw-bold">Добро пожаловать, username</label>
<h1>Здравствуйте, {{ ses['login'] }}!</h1>
{% if ses['access_user'] == 'internal_users' %}
<h2> Ваша роль: {{ ses['role'] }}</h2>
<div class="buttons_menu">
<a href="{{ url_for('requests_bp.requests') }}"><button>Запросы</button></a>
<a href="{{ url_for('report_bp.menu') }}"><button>Отчеты</button></a>
</div>
{% else %}
<div class="buttons_menu">
<a href="{{ url_for('waybill_bp.waybill') }}"><button>Новая накладная</button></a>
</div>
{% endif %}
{% else %}
<div class="login">
<a href="{{ url_for('auth_bp.auth') }}"><button>Авторизация</button></a>
</div>
<h1>Здравствуйте!</h1>
<p>Для использования системы, пожалуйста, пройдите процедуру авторизации</p>
{% endif %} -->
</body>
</html>

Binary file not shown.