Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7fdd3c56cb | |||
| 052c93928c | |||
| e44696ce04 | |||
| 72f766ab46 | |||
| 9ecb6a83ba | |||
| 951e4c7e45 | |||
| 7786680f43 | |||
| 33c3b81f6e | |||
| 6006e074d8 | |||
| 771f7e0549 | |||
| fd1b7df93a | |||
| 88c4460da9 | |||
| 51bf856434 | |||
| 0ded143c55 | |||
| 056d800c4f | |||
| 3be4556844 | |||
| 928677fea8 | |||
| e80c5645f1 | |||
| 70041fa58e | |||
| 6f478d717a | |||
| be5732bc32 | |||
| 4ad7869e27 | |||
| 67c06a0203 | |||
| df5317432d | |||
| 96a310a80d | |||
| ec985a808d | |||
| f93b94531c | |||
| 22da586aec | |||
| 5058df8b7d | |||
| 3a118c51b5 |
+35
@@ -0,0 +1,35 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from model.user import User
|
||||
from db.repositories.user_repository import UserRepository # FIXME: authenticate_user as get_user
|
||||
from utils.token import generateKey as getToken
|
||||
|
||||
auth = Blueprint("auth", __name__)
|
||||
|
||||
|
||||
@auth.route('/login', methods = ['POST'])
|
||||
def login():
|
||||
if request.is_json:
|
||||
req = request.json
|
||||
|
||||
email = req.get('email')
|
||||
password = req.get('password')
|
||||
|
||||
if not email or not password:
|
||||
return "Request must have email and password", 400
|
||||
|
||||
if len(email.strip()) < 4 or '@' not in email or '.' not in email:
|
||||
return "Email is incorrect", 400
|
||||
|
||||
if len(password.strip()) < 8:
|
||||
return "Password is too short", 400
|
||||
|
||||
user = UserRepository().authenticate_user(email, password)
|
||||
if not user:
|
||||
return "Wrong credentials", 400
|
||||
|
||||
token = getToken(user)
|
||||
|
||||
return jsonify({'token': token, 'user': {'id': user.id, 'name': user.name, 'role': user.role}})
|
||||
|
||||
else:
|
||||
return "Request is not a json", 400
|
||||
@@ -1,12 +0,0 @@
|
||||
from flask import Blueprint, request
|
||||
from model.user import user
|
||||
|
||||
loginBP = Blueprint("loginapi", __name__)
|
||||
|
||||
@loginBP.route('/api/login', methods = ['POST'])
|
||||
def login():
|
||||
email = request.form['email']
|
||||
password = request.form['password']
|
||||
#if(isvalid(email, password)):
|
||||
us = user.initialize(email, password)
|
||||
return us.toJSON()
|
||||
@@ -1,6 +1,11 @@
|
||||
from sys import exit
|
||||
from flask import Flask
|
||||
from api.loginapi import loginBP
|
||||
from api.auth import auth
|
||||
from utils.loadDotEnv import initializeENV
|
||||
|
||||
if not initializeENV():
|
||||
exit(-1)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
app.register_blueprint(loginBP)
|
||||
app.register_blueprint(auth, url_prefix='/api/auth')
|
||||
|
||||
+31
-27
@@ -1,21 +1,29 @@
|
||||
import psycopg2
|
||||
import os
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from typing import Generator
|
||||
from loguru import logger
|
||||
|
||||
from utils.loadDotEnv import initializeENV
|
||||
|
||||
initializeENV()
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def PSQLConnect():
|
||||
conn = psycopg2.connect(os.getenv('POSTDRESS_CONNECTION'))
|
||||
conn_str = os.getenv('POSTGRES_CONNECTION')
|
||||
|
||||
if not conn_str:
|
||||
logger.error("POSTGRES_CONNECTION не найден в .env файле")
|
||||
raise ValueError("POSTGRES_CONNECTION не найден в .env файле")
|
||||
|
||||
conn = psycopg2.connect(conn_str)
|
||||
logger.debug("Подключение к БД установлено")
|
||||
return conn
|
||||
|
||||
|
||||
def PSQLCursor(conn):
|
||||
cur = conn.cursor()
|
||||
cur = conn.cursor()
|
||||
logger.debug("Курсор БД создан")
|
||||
return cur
|
||||
|
||||
|
||||
@@ -24,38 +32,34 @@ def get_connection() -> Generator[psycopg2.extensions.connection, None, None]:
|
||||
conn = None
|
||||
try:
|
||||
conn = PSQLConnect()
|
||||
logger.debug("Подключение к БД установлено")
|
||||
logger.debug("Контекст подключения к БД открыт")
|
||||
yield conn
|
||||
except psycopg2.OperationalError as e:
|
||||
logger.error(f"Ошибка подключения к БД: {e}")
|
||||
raise
|
||||
except psycopg2.Error as e:
|
||||
logger.error(f"Ошибка PostgreSQL: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Неожиданная ошибка при работе с БД: {e}")
|
||||
logger.error(f"Ошибка в контексте подключения: {e}")
|
||||
if conn:
|
||||
conn.rollback()
|
||||
logger.debug("Откат транзакции выполнен")
|
||||
raise
|
||||
finally:
|
||||
if conn:
|
||||
try:
|
||||
conn.close()
|
||||
logger.debug("Соединение с БД закрыто")
|
||||
except Exception as e:
|
||||
logger.warning(f"Ошибка при закрытии соединения: {e}")
|
||||
conn.close()
|
||||
logger.debug("Подключение к БД закрыто")
|
||||
|
||||
|
||||
def test_connection() -> bool:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
cur = PSQLCursor(conn)
|
||||
try:
|
||||
cur.execute("SELECT version();")
|
||||
version = cur.fetchone()
|
||||
logger.debug(f"Версия PostgreSQL: {version[0]}")
|
||||
return True
|
||||
finally:
|
||||
cur.close()
|
||||
logger.debug("Курсор закрыт")
|
||||
cur.execute("SELECT version();")
|
||||
version = cur.fetchone()
|
||||
logger.info(f"Подключение к БД успешно: {version[0]}")
|
||||
cur.close()
|
||||
logger.debug("Курсор БД закрыт")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Тест подключения к БД провален: {e}")
|
||||
logger.error(f"Ошибка подключения к БД: {e}")
|
||||
return False
|
||||
print(test_connection())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_connection()
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
from db.connection import get_connection
|
||||
from loguru import logger
|
||||
|
||||
def create_tables():
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
# Пользователи
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(50) NOT NULL DEFAULT 'viewer',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
|
||||
# Роботы
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS robots (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
status VARCHAR(50) DEFAULT 'active',
|
||||
battery_level INTEGER DEFAULT 100,
|
||||
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
current_zone VARCHAR(10),
|
||||
current_row INTEGER,
|
||||
current_shelf INTEGER
|
||||
)
|
||||
""")
|
||||
|
||||
# Товары
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
category VARCHAR(100),
|
||||
min_stock INTEGER DEFAULT 10,
|
||||
optimal_stock INTEGER DEFAULT 100
|
||||
)
|
||||
""")
|
||||
|
||||
# История инвентаризации
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS inventory_history (
|
||||
id SERIAL PRIMARY KEY,
|
||||
robot_id VARCHAR(50) REFERENCES robots(id),
|
||||
product_id VARCHAR(50) REFERENCES products(id),
|
||||
quantity INTEGER NOT NULL,
|
||||
zone VARCHAR(10) NOT NULL,
|
||||
row_number INTEGER,
|
||||
shelf_number INTEGER,
|
||||
status VARCHAR(50),
|
||||
scanned_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
|
||||
# Прогнозы ИИ
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS ai_predictions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
product_id VARCHAR(50) REFERENCES products(id),
|
||||
prediction_date DATE NOT NULL,
|
||||
days_until_stockout INTEGER,
|
||||
recommended_order INTEGER,
|
||||
confidence_score DECIMAL(3,2),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
logger.debug("Все таблицы успешно созданы")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка создания таблиц: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def create_indexes():
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("CREATE INDEX IF NOT EXISTS idx_inventory_scanned ON inventory_history(scanned_at DESC)")
|
||||
cur.execute("CREATE INDEX IF NOT EXISTS idx_inventory_product ON inventory_history(product_id)")
|
||||
cur.execute("CREATE INDEX IF NOT EXISTS idx_inventory_zone ON inventory_history(zone)")
|
||||
|
||||
conn.commit()
|
||||
logger.debug("Индексы созданы")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка создания индексов: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def insert_sample_data():
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("""
|
||||
INSERT INTO users (email, password_hash, name, role)
|
||||
VALUES
|
||||
('admin@warehouse.com', 'hash1', 'Администратор', 'admin'),
|
||||
('operator@warehouse.com', 'hash2', 'Оператор Иванов', 'operator'),
|
||||
('viewer@warehouse.com', 'hash3', 'Наблюдатель Петров', 'viewer')
|
||||
ON CONFLICT (email) DO NOTHING
|
||||
""")
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO robots (id, status, battery_level, current_zone)
|
||||
VALUES
|
||||
('RB-001', 'active', 85, 'A'),
|
||||
('RB-002', 'active', 45, 'B'),
|
||||
('RB-003', 'maintenance', 100, NULL)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
""")
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO products (id, name, category, min_stock, optimal_stock)
|
||||
VALUES
|
||||
('TEL-1234', 'Смартфон X', 'Электроника', 5, 50),
|
||||
('NOTE-567', 'Ноутбук Pro', 'Электроника', 3, 20),
|
||||
('ACC-999', 'Чехол для телефона', 'Аксессуары', 10, 100)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
logger.debug("Тестовые данные добавлены")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка добавления тестовых данных: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def initialize_database():
|
||||
logger.info("Начинаем инициализацию базы данных...")
|
||||
|
||||
create_tables()
|
||||
create_indexes()
|
||||
insert_sample_data()
|
||||
|
||||
logger.debug("База данных успешно инициализирована!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
initialize_database()
|
||||
@@ -1,11 +1,9 @@
|
||||
from typing import List, Optional
|
||||
from datetime import datetime, date
|
||||
import logging
|
||||
from loguru import logger
|
||||
from db.connection import get_connection
|
||||
from model.ai_prediction import AIPrediction
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class AIPredictionsRepository:
|
||||
def get_all(self) -> List[AIPrediction]:
|
||||
try:
|
||||
@@ -77,12 +75,14 @@ class AIPredictionsRepository:
|
||||
logger.error(f"Ошибка получения последних прогнозов по товарам: {e}")
|
||||
return []
|
||||
|
||||
def create_prediction(self,
|
||||
product_id: str,
|
||||
prediction_date: date,
|
||||
days_until_stockout: int,
|
||||
recommended_order: int,
|
||||
confidence_score: float) -> Optional[int]:
|
||||
def create_prediction(
|
||||
self,
|
||||
product_id: str,
|
||||
prediction_date: date,
|
||||
days_until_stockout: int,
|
||||
recommended_order: int,
|
||||
confidence_score: float
|
||||
) -> Optional[int]:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
# db/repositories/inventory_repository.py
|
||||
from typing import List, Optional, Tuple
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from loguru import logger
|
||||
from db.connection import get_connection
|
||||
from model.inventory import InventoryRecord
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class InventoryRepository:
|
||||
def get_all(self) -> List[InventoryRecord]:
|
||||
try:
|
||||
@@ -48,15 +46,17 @@ class InventoryRepository:
|
||||
logger.error(f"Ошибка получения записи инвентаризации {record_id}: {e}")
|
||||
return None
|
||||
|
||||
def create_record(self,
|
||||
robot_id: str,
|
||||
product_id: str,
|
||||
quantity: int,
|
||||
zone: str,
|
||||
row_number: int,
|
||||
shelf_number: int,
|
||||
status: str,
|
||||
scanned_at: datetime) -> Optional[int]:
|
||||
def create_record(
|
||||
self,
|
||||
robot_id: str,
|
||||
product_id: str,
|
||||
quantity: int,
|
||||
zone: str,
|
||||
row_number: int,
|
||||
shelf_number: int,
|
||||
status: str,
|
||||
scanned_at: datetime
|
||||
) -> Optional[int]:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
from loguru import logger
|
||||
from db.connection import get_connection
|
||||
from model.product import Product
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class ProductRepository:
|
||||
def get_all(self) -> List[Product]:
|
||||
try:
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
from loguru import logger
|
||||
from db.connection import get_connection
|
||||
from model.robot import Robot
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RobotRepository:
|
||||
def get_all(self) -> List[Robot]:
|
||||
try:
|
||||
@@ -43,13 +41,15 @@ class RobotRepository:
|
||||
logger.error(f"Ошибка получения робота {robot_id}: {e}")
|
||||
return None
|
||||
|
||||
def update_robot(self,
|
||||
robot_id: str,
|
||||
status: str = None,
|
||||
battery_level: int = None,
|
||||
current_zone: str = None,
|
||||
current_row: int = None,
|
||||
current_shelf: int = None) -> bool:
|
||||
def update_robot(
|
||||
self,
|
||||
robot_id: str,
|
||||
status: str = None,
|
||||
battery_level: int = None,
|
||||
current_zone: str = None,
|
||||
current_row: int = None,
|
||||
current_shelf: int = None
|
||||
) -> bool:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
@@ -131,10 +131,12 @@ class RobotRepository:
|
||||
logger.error(f"Ошибка получения роботов в зоне {zone}: {e}")
|
||||
return []
|
||||
|
||||
def create_robot(self,
|
||||
robot_id: str,
|
||||
status: str = 'active',
|
||||
battery_level: int = 100) -> bool:
|
||||
def create_robot(
|
||||
self,
|
||||
robot_id: str,
|
||||
status: str = 'active',
|
||||
battery_level: int = 100
|
||||
) -> bool:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
from loguru import logger
|
||||
from model.user import User
|
||||
from db.connection import get_connection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class UserRepository:
|
||||
def get_all(self) -> List[User]:
|
||||
try:
|
||||
@@ -57,11 +55,13 @@ class UserRepository:
|
||||
logger.error(f"Ошибка получения пользователя по email {email}: {e}")
|
||||
return None
|
||||
|
||||
def create_user(self,
|
||||
email: str,
|
||||
password_hash: str,
|
||||
name: str,
|
||||
role: str) -> Optional[User]:
|
||||
def create_user(
|
||||
self,
|
||||
email: str,
|
||||
password_hash: str,
|
||||
name: str,
|
||||
role: str
|
||||
) -> Optional[User]:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
@@ -166,6 +166,9 @@ class UserRepository:
|
||||
return False
|
||||
|
||||
def authenticate_user(self, email: str, password_hash: str) -> Optional[User]:
|
||||
if not self.user_exists(email):
|
||||
return
|
||||
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
@@ -183,24 +186,6 @@ class UserRepository:
|
||||
logger.error(f"Ошибка аутентификации пользователя {email}: {e}")
|
||||
return None
|
||||
|
||||
def is_valid_authenticate(self, email: str, password_hash: str) -> bool:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("""
|
||||
SELECT 1 FROM users
|
||||
WHERE email = %s AND password_hash = %s
|
||||
""", (email, password_hash))
|
||||
is_valid = cur.fetchone() is not None
|
||||
if is_valid:
|
||||
logger.debug(f"Валидные учетные данные для пользователя {email}")
|
||||
else:
|
||||
logger.warning(f"Невалидные учетные данные для пользователя {email}")
|
||||
return is_valid
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка проверки учетных данных пользователя {email}: {e}")
|
||||
return False
|
||||
|
||||
def user_exists(self, email: str) -> bool:
|
||||
try:
|
||||
with get_connection() as conn:
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
flask==3.1.2
|
||||
python-dotenv
|
||||
psycopg-binary
|
||||
psycopg2-binary
|
||||
pyjwt
|
||||
loguru
|
||||
|
||||
+10
-8
@@ -1,12 +1,14 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from loguru import logger as log
|
||||
|
||||
def initializeENV():
|
||||
dotenv_path = '../.env'
|
||||
if os.path.exists(dotenv_path):
|
||||
load_dotenv(dotenv_path)
|
||||
print('.env is loaded')
|
||||
return 1
|
||||
DOTENV_PATH = '.env'
|
||||
|
||||
def initializeENV() -> bool:
|
||||
if os.path.exists(DOTENV_PATH):
|
||||
load_dotenv(DOTENV_PATH)
|
||||
log.info('.env is loaded')
|
||||
return True
|
||||
else:
|
||||
print('.env isn`t loaded')
|
||||
return 0
|
||||
log.error('.env isn`t loaded')
|
||||
return False
|
||||
|
||||
+13
-2
@@ -1,7 +1,18 @@
|
||||
import jwt
|
||||
import os
|
||||
from time import time
|
||||
from model.user import User
|
||||
|
||||
def generateKey(email, passwd):
|
||||
def generateKey(user: User) -> dict:
|
||||
key = os.getenv('KEY')
|
||||
encoded = jwt.encode({f"{email}": f"{passwd}"}, key, algorithm="HS256")
|
||||
encoded = jwt.encode(
|
||||
{
|
||||
'id': user.id,
|
||||
'name': user.name,
|
||||
'role': user.role,
|
||||
'iat': time()
|
||||
},
|
||||
key,
|
||||
algorithm="HS256"
|
||||
)
|
||||
return encoded
|
||||
|
||||
Reference in New Issue
Block a user