Compare commits
6 Commits
features/f
...
55ba627f6b
| Author | SHA1 | Date | |
|---|---|---|---|
| 55ba627f6b | |||
| b26485c5cc | |||
| 0022141684 | |||
| 7043743373 | |||
| 0c39af460f | |||
| 1222488aec |
9
business_rules/br.md
Normal file
9
business_rules/br.md
Normal file
@@ -0,0 +1,9 @@
|
||||
Создание заказа через API ABCP
|
||||
1. Логинимся под учеткой заказчика
|
||||
2. Получаем остатки
|
||||
3. Отсекаем не подходящие по сроку (дольше) и цене
|
||||
4. Подбираем позицию максимально близкую к цене из заказа
|
||||
- Приоритет отдаем складу, где есть все заказы
|
||||
- Приоритет отдаем позициям из наличия, потом с доставкой с других складов
|
||||
- По цене выбираем наиболее близкую к цене заказа (меньше или равно)
|
||||
- При невозможности заказать в одном месте разбиваем заказ из нескольких складов
|
||||
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Классы для работы с API платформы ABCP
|
||||
"""
|
||||
|
||||
from .abcp_provider import AbcpProvider
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import os
|
||||
import hashlib
|
||||
import requests
|
||||
import logging
|
||||
@@ -13,19 +12,31 @@ class AbcpProvider:
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
|
||||
def __init__(self, account="SYSTEM"):
|
||||
self.base_url = self.HOST
|
||||
def __init__(self, login: str, password: str):
|
||||
"""
|
||||
Инициализация AbcpProvider.
|
||||
|
||||
def get_stock(self, sku, manufacturer, partner="SYSTEM"):
|
||||
Args:
|
||||
login: Логин для доступа к API
|
||||
password: Пароль для доступа к API
|
||||
"""
|
||||
self.base_url = self.HOST
|
||||
self.login = login
|
||||
self.password = password
|
||||
|
||||
def get_stock(self, sku, manufacturer):
|
||||
method = "GET"
|
||||
path = "/search/articles"
|
||||
|
||||
params = {"number": sku, "brand": manufacturer, "withOutAnalogs": "1"}
|
||||
return self._execute(partner, path, method, params)
|
||||
return self._execute(path, method, params)
|
||||
|
||||
def _execute(self, partner, path, method="GET", params={}, data=None, ):
|
||||
params["userlogin"] = os.getenv(f"ABCP_LOGIN_{partner}")
|
||||
params["userpsw"] = hashlib.md5(os.getenv(f"ABCP_PASSWORD_{partner}").encode("utf-8")).hexdigest()
|
||||
def save_order(self, order):
|
||||
pass
|
||||
|
||||
def _execute(self, path, method="GET", params={}, data=None):
|
||||
params["userlogin"] = self.login
|
||||
params["userpsw"] = hashlib.md5(self.password.encode("utf-8")).hexdigest()
|
||||
|
||||
response = requests.request(method, self.HOST+path, data=data, headers=self.HEADERS, params=params)
|
||||
payload = response.json()
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
"""
|
||||
Пакет содержит реализацию для создания заказов из вложений.
|
||||
|
||||
На входе файл из вложения
|
||||
Его обработка производится хендлерами, коотрые настраиваются в конфиге
|
||||
На выходе - экземпляр класса, который в себе содержит
|
||||
- прочитанный файл в виде pandas DataFrame
|
||||
- распарсенный файл заказа с позициями
|
||||
- полученные остатки
|
||||
- результат проверки возможности создания заказа
|
||||
|
||||
Так же класс содержит методы
|
||||
- для создания заказа
|
||||
- для получения отредактированного файла
|
||||
|
||||
"""
|
||||
|
||||
from .processor import TaskProcessor
|
||||
@@ -1,9 +0,0 @@
|
||||
from .abcp_clients.check_stock import GetStock
|
||||
from .abcp_clients.create_order import InstantOrderTest
|
||||
|
||||
from .excel_parcers.basic_excel_parcer import BasicExcelParser
|
||||
|
||||
from .notifications.test_notifier import TestNotifier
|
||||
|
||||
|
||||
from .validators.price_quantity_ckecker import CheckOrder
|
||||
@@ -1,26 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
from mail_order_bot.attachment_handler.handlers.abstract_task import AbstractTask
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_stock(brand, part_number):
|
||||
return random.randint(0, 10)
|
||||
|
||||
class GetStock(AbstractTask):
|
||||
|
||||
def do(self) -> None:
|
||||
positions = self.order.positions
|
||||
for position in positions:
|
||||
self._update_stock(position)
|
||||
|
||||
def _update_stock(self, position):
|
||||
# Эмулируем получение данных
|
||||
max_stock = self.config.get('max_stock',10)
|
||||
stock = random.randint(0, max_stock)
|
||||
price = position.requested_price
|
||||
|
||||
position.stock_price = price
|
||||
position.stock_quantity = stock
|
||||
@@ -1,37 +0,0 @@
|
||||
import logging
|
||||
import requests
|
||||
from mail_order_bot.attachment_handler.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.attachment_handler.order.auto_part_order import OrderStatus
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class InstantOrderTest(AbstractTask):
|
||||
URL = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}"
|
||||
|
||||
def do(self) -> None:
|
||||
api_key = self.config["api_key"]
|
||||
chat_id = self.config["chat_id"]
|
||||
|
||||
if self.order.status == OrderStatus.IN_PROGRESS:
|
||||
positions = self.order.positions
|
||||
|
||||
message = f"Запрос на создание заказа от {self.context['client']}:\n"
|
||||
message += "\n".join(f"{pos.sku}: {pos.name} ({pos.order_quantity} x {pos.order_price} = {pos.total})" for pos in positions)
|
||||
|
||||
elif self.order.status == OrderStatus.OPERATOR_REQUIRED:
|
||||
message = f"Запрос на создание заказа от {self.context['client']} отклонен - необходима ручная обработка.\n"
|
||||
message += f"Причина: {self.order.reason}"
|
||||
|
||||
else:
|
||||
message = f"Запрос на создание заказа от {self.context['client']} отклонен.\n"
|
||||
message += f" Статус заказа: {self.order.status}"
|
||||
|
||||
#url = self.URL.format(api_key, chat_id, message)
|
||||
#resp = requests.get(url).json()
|
||||
print(message)
|
||||
#logger.info(resp)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import logging
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, Any
|
||||
from ..order.auto_part_order import AutoPartOrder
|
||||
|
||||
class AbstractTask(ABC):
|
||||
RESULT_SECTION = "section"
|
||||
"""
|
||||
Абстрактный базовый класс для всех хэндлеров.
|
||||
"""
|
||||
def __init__(self, config: Dict[str, Any], context: Dict[str, Any], order: AutoPartOrder, *args, **kwargs) -> None:
|
||||
self.config = config
|
||||
self.context = context
|
||||
self.order = order
|
||||
|
||||
@abstractmethod
|
||||
def do(self) -> None:
|
||||
"""
|
||||
Выполняет работу над заданием
|
||||
Входные и выходные данные - в self.context
|
||||
Конфиг задается при инициализации
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
import logging
|
||||
import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.attachment_handler.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.attachment_handler.handlers.abstract_task import AbstractTask
|
||||
|
||||
from ...order.auto_part_position import AutoPartPosition
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BasicExcelParser(AbstractTask):
|
||||
RESULT_SECTION = "positions"
|
||||
"""
|
||||
Универсальный парсер, настраиваемый через конфигурацию.
|
||||
Подходит для большинства стандартных случаев.
|
||||
"""
|
||||
|
||||
def do(self) -> None:
|
||||
|
||||
# todo сделать проверку на наличие файла и его тип
|
||||
file_bytes = BytesIO(self.context.get("attachment").content) # self.context.get("attachment") #
|
||||
try:
|
||||
df = self._make_dataframe(file_bytes)
|
||||
# Получаем маппинг колонок из конфигурации
|
||||
mapping = self.config['mapping']
|
||||
|
||||
# Парсим строки
|
||||
positions = []
|
||||
for idx, row in df.iterrows():
|
||||
try:
|
||||
position = self._parse_row(row, mapping)
|
||||
if position:
|
||||
positions.append(position)
|
||||
self.order.add_position(position)
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка парсинга строки {idx}: {e}, {row}")
|
||||
continue
|
||||
|
||||
logger.info(f"Успешно обработано {len(positions)} позиций из {len(df)} строк")
|
||||
|
||||
self.context[self.RESULT_SECTION] = positions
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обработке файла: {e}")
|
||||
raise Exception from e
|
||||
|
||||
def _parse_row(self, row: pd.Series, mapping: Dict[str, str]) -> Optional[AutoPartPosition]:
|
||||
"""Парсит одну строку Excel в OrderPosition"""
|
||||
|
||||
# Проверяем обязательные поля
|
||||
required_fields = ['article', 'price', 'quantity']
|
||||
|
||||
for field in required_fields:
|
||||
if pd.isna(row.get(mapping[field])):
|
||||
logger.warning(f"Позиция не создана - не заполнено поле {mapping[field]}")
|
||||
return None
|
||||
|
||||
price = Decimal(str(row[mapping['price']]).replace(",", ".").strip())
|
||||
quantity = int(row[mapping['quantity']])
|
||||
|
||||
if "total" in mapping.keys():
|
||||
total = Decimal(str(row[mapping['total']]).replace(",", ".").strip())
|
||||
else:
|
||||
total = price * quantity
|
||||
|
||||
if mapping.get('name', "") in mapping.keys():
|
||||
name = str(row[mapping.get('name', "")]).strip()
|
||||
else:
|
||||
name = ""
|
||||
|
||||
# Создаем объект позиции
|
||||
position = AutoPartPosition(
|
||||
sku=str(row[mapping['article']]).strip(),
|
||||
manufacturer=str(row[mapping.get('manufacturer', "")]).strip(),
|
||||
name=name,
|
||||
requested_price=price,
|
||||
requested_quantity=quantity,
|
||||
total=total,
|
||||
additional_attrs=self._extract_additional_attrs(row, mapping)
|
||||
)
|
||||
return position
|
||||
|
||||
def _extract_additional_attrs(self, row: pd.Series, mapping: Dict[str, str]) -> Dict[str, Any]:
|
||||
"""Извлекает дополнительные атрибуты, не входящие в основную модель"""
|
||||
additional = {}
|
||||
mapped_columns = set(mapping.values())
|
||||
|
||||
for col in row.index:
|
||||
if col not in mapped_columns and not pd.isna(row[col]):
|
||||
additional[col] = row[col]
|
||||
|
||||
return additional
|
||||
|
||||
def _make_dataframe(self, bio) -> pd.DataFrame:
|
||||
# Получаем все данные из файла
|
||||
sheet_name = self.config.get("sheet_name", 0)
|
||||
df_full = pd.read_excel(bio, sheet_name=sheet_name, header=None)
|
||||
|
||||
# Находим индекс строки с заголовком
|
||||
key_field = self.config.get("key_field")
|
||||
header_row_idx = df_full[
|
||||
df_full.apply(lambda row: row.astype(str).str.contains(key_field, case=False, na=False).any(),
|
||||
axis=1)].index[0]
|
||||
|
||||
# Считываем таблицу с правильным заголовком
|
||||
df = pd.read_excel(bio, header=header_row_idx, sheet_name=sheet_name, engine='calamine') # openpyxl calamine
|
||||
|
||||
# Находим индекс первой строки с пустым 'Артикул'
|
||||
first_empty_index = df[df[key_field].isna()].index.min()
|
||||
|
||||
# Обрезаем DataFrame до первой пустой строки (не включая её)
|
||||
df_trimmed = df.loc[:first_empty_index - 1]
|
||||
|
||||
return df_trimmed
|
||||
@@ -1,15 +0,0 @@
|
||||
import logging
|
||||
|
||||
from mail_order_bot.attachment_handler.handlers.abstract_task import AbstractTask
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class TestNotifier(AbstractTask):
|
||||
def do(self) -> None:
|
||||
|
||||
positions = self.context["positions"]
|
||||
|
||||
print(f"\nПолучено {len(positions)} позиций от {self.context["client"]}:")
|
||||
for pos in positions: # Первые 5
|
||||
print(f" - {pos.sku}: {pos.name} "
|
||||
f"({pos.requested_quantity} x {pos.requested_price} = {pos.total})")
|
||||
@@ -1,50 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
from mail_order_bot.attachment_handler.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.attachment_handler.order.auto_part_order import OrderStatus
|
||||
from decimal import Decimal
|
||||
import random
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CheckOrder(AbstractTask):
|
||||
|
||||
def do(self) -> None:
|
||||
refused = 0
|
||||
positions = self.order.positions
|
||||
for position in positions:
|
||||
self._set_order_price(position)
|
||||
self._set_order_quantity(position)
|
||||
|
||||
if position.order_price == 0 or position.order_quantity == 0:
|
||||
refused += 1
|
||||
|
||||
self._check_refusal_threshold(refused)
|
||||
|
||||
|
||||
def _set_order_price(self, position):
|
||||
# Эмулируем получение данных
|
||||
acceptable_price_reduction = self.config.get("acceptable_price_reduction")
|
||||
acceptable_price = position.stock_price* Decimal(str((1-acceptable_price_reduction/100)))
|
||||
|
||||
if position.requested_price < acceptable_price:
|
||||
position.order_price = 0
|
||||
else:
|
||||
position.order_price = position.requested_price
|
||||
|
||||
def _set_order_quantity(self, position):
|
||||
max_stock = self.config.get("max_stock", 100)
|
||||
min_stock = self.config.get("min_stock", 0)
|
||||
|
||||
stock_quantity = random.randint(min_stock, max_stock)
|
||||
|
||||
position.order_quantity = max(0, min(position.stock_quantity, stock_quantity))
|
||||
|
||||
def _check_refusal_threshold(self, refused):
|
||||
refusal_threshold_limit = self.config.get("refusal_threshold", 1)
|
||||
refusal_level = refused/len(self.order.positions)
|
||||
|
||||
if refusal_level > refusal_threshold_limit:
|
||||
self.order.status = OrderStatus.OPERATOR_REQUIRED
|
||||
self.order.reason = "Превышен порог отказов"
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
from typing import List, Optional
|
||||
from .auto_part_position import AutoPartPosition
|
||||
from enum import Enum
|
||||
|
||||
class OrderStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
FAILED = "failed"
|
||||
COMPLETED = "completed"
|
||||
OPERATOR_REQUIRED = "operator required"
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class AutoPartOrder:
|
||||
def __init__(self):
|
||||
self.positions: List[AutoPartPosition] = []
|
||||
self.status = OrderStatus.NEW
|
||||
self.reason = ""
|
||||
|
||||
def add_position(self, position: AutoPartPosition) -> None:
|
||||
self.positions.append(position)
|
||||
if self.status == OrderStatus.NEW:
|
||||
self.status = OrderStatus.IN_PROGRESS
|
||||
|
||||
def find_positions(self, brand: Optional[str] = None, sku: Optional[str] = None) -> List[AutoPartPosition]:
|
||||
results = self.positions
|
||||
if brand is not None:
|
||||
results = [p for p in results if p.manufacturer == brand]
|
||||
if sku is not None:
|
||||
results = [p for p in results if p.sku == sku]
|
||||
return results
|
||||
|
||||
def __len__(self):
|
||||
return len(self.positions)
|
||||
@@ -1,77 +0,0 @@
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, Any
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
@dataclass
|
||||
class AutoPartPosition:
|
||||
"""
|
||||
Унифицированная модель позиции для заказа.
|
||||
Все контрагенты приводятся к этой структуре.
|
||||
"""
|
||||
sku: str # Артикул товара
|
||||
manufacturer: str # Производитель
|
||||
name: str # Наименование
|
||||
requested_price: Decimal # Цена за единицу
|
||||
requested_quantity: int # Количество
|
||||
total: Decimal # Общая сумма
|
||||
stock_quantity: int = 0 # Остаток на складе
|
||||
stock_price: Decimal = Decimal('0.0') # Цена на складе
|
||||
order_quantity: int = 0 # Количество для заказа
|
||||
order_price: Decimal = Decimal('0.0') # Цена в заказе
|
||||
additional_attrs: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
"""Валидация после инициализации"""
|
||||
if self.requested_quantity < 0:
|
||||
raise ValueError(f"Количество не может быть отрицательным: {self.requested_quantity}")
|
||||
if self.requested_price < 0:
|
||||
raise ValueError(f"Цена не может быть отрицательной: {self.requested_price}")
|
||||
|
||||
|
||||
|
||||
class AutoPartPosition2:
|
||||
brand: str
|
||||
sku: str
|
||||
name: str
|
||||
customer_price: float
|
||||
customer_quantity: int
|
||||
supplier_price: float
|
||||
stock_remaining: int
|
||||
|
||||
def __init__(self, brand: str, sku: str, name: str,
|
||||
customer_price: float, customer_quantity: int,
|
||||
supplier_price: float, stock_remaining: int):
|
||||
self.brand = brand
|
||||
self.sku = sku
|
||||
self.name = name
|
||||
self.customer_price = customer_price
|
||||
self.customer_quantity = customer_quantity
|
||||
self.supplier_price = supplier_price
|
||||
self.stock_remaining = stock_remaining
|
||||
|
||||
def customer_cost(self) -> float:
|
||||
return self.customer_price * self.customer_quantity
|
||||
|
||||
def supplier_cost(self) -> float:
|
||||
return self.supplier_price * self.customer_quantity
|
||||
|
||||
def is_available(self) -> bool:
|
||||
return self.stock_remaining >= self.customer_quantity
|
||||
|
||||
def restock(self, amount: int) -> None:
|
||||
if amount < 0:
|
||||
raise ValueError("Restock amount must be non-negative")
|
||||
self.stock_remaining += amount
|
||||
|
||||
def __post_init__(self):
|
||||
if self.customer_price < 0:
|
||||
raise ValueError("Customer price cannot be negative")
|
||||
if self.customer_quantity < 0:
|
||||
raise ValueError("Customer quantity cannot be negative")
|
||||
if self.supplier_price < 0:
|
||||
raise ValueError("Supplier price cannot be negative")
|
||||
if self.stock_remaining < 0:
|
||||
raise ValueError("Stock remaining cannot be negative")
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
from mail_order_bot.attachment_handler.order.auto_part_order import AutoPartOrder
|
||||
|
||||
class Position:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
class AutoPartOrder
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
class OrderRequest:
|
||||
def __init__(self, attachment):
|
||||
self.attachment = attachment
|
||||
self.parsed = None
|
||||
self.positions = []
|
||||
self.status=None
|
||||
|
||||
def add_position(self, position):
|
||||
pass
|
||||
|
||||
def find_positions(self):
|
||||
pass
|
||||
|
||||
def __process(self):
|
||||
pass
|
||||
|
||||
def get_file(self):
|
||||
pass
|
||||
|
||||
def get_attachment(self):
|
||||
pass
|
||||
@@ -1,42 +0,0 @@
|
||||
import os
|
||||
import yaml
|
||||
import logging
|
||||
from typing import Dict, Any
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .order.auto_part_order import AutoPartOrder
|
||||
from .handlers import *
|
||||
|
||||
class TaskProcessor:
|
||||
def __init__(self, config_path: Path):
|
||||
self.config_path = config_path
|
||||
self.context = dict()
|
||||
self.order = None
|
||||
|
||||
def process(self, client, attachment):
|
||||
config = self._load_config(client)
|
||||
self.context = dict()
|
||||
self.order = AutoPartOrder()
|
||||
|
||||
self.context["client"] = client
|
||||
self.context["attachment"] = attachment
|
||||
|
||||
self.context["status"] = client
|
||||
|
||||
for stage in config["pipeline"]:
|
||||
handler_name = stage["handler"]
|
||||
logger.info(f"Processing handler: {handler_name}")
|
||||
task = globals()[handler_name](stage.get("config", None), self.context, self.order)
|
||||
task.do()
|
||||
|
||||
return self.context
|
||||
|
||||
|
||||
pass
|
||||
def _load_config(self, client) -> Dict[str, Any]:
|
||||
"""Загружает конфигурацию из YAML или JSON"""
|
||||
path = os.path.join(self.config_path, client + '.yml')
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
@@ -1,3 +1,13 @@
|
||||
#=========================================
|
||||
config:
|
||||
|
||||
|
||||
pipeline:
|
||||
-
|
||||
|
||||
|
||||
|
||||
#=========================================
|
||||
pipeline:
|
||||
# Настраиваем парсинг экселя
|
||||
- handler: BasicExcelParser
|
||||
@@ -16,8 +26,7 @@ pipeline:
|
||||
- handler: DeliveryPeriodLocalStore
|
||||
|
||||
# Запрос остатков со склада
|
||||
- handler: GetStock
|
||||
|
||||
- handler: APIGetStock
|
||||
|
||||
|
||||
|
||||
|
||||
108
src/mail_order_bot/credential_provider.py
Normal file
108
src/mail_order_bot/credential_provider.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import os
|
||||
import logging
|
||||
from typing import Optional, Tuple
|
||||
from mail_order_bot.context import Context
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CredentialProvider:
|
||||
"""
|
||||
Класс для получения учетных данных (логин и пароль) для доступа к API.
|
||||
|
||||
Учетные данные берутся из переменных окружения в формате:
|
||||
- {PREFIX}_LOGIN_{CLIENT_NAME} - логин для клиента
|
||||
- {PREFIX}_PASSWORD_{CLIENT_NAME} - пароль для клиента
|
||||
- {PREFIX}_LOGIN_SYSTEM - логин для системной учетной записи
|
||||
- {PREFIX}_PASSWORD_SYSTEM - пароль для системной учетной записи
|
||||
"""
|
||||
|
||||
SYSTEM_ACCOUNT = "SYSTEM"
|
||||
|
||||
def __init__(self, prefix: str = "ABCP", context: Optional[Context] = None):
|
||||
"""
|
||||
Инициализация CredentialProvider.
|
||||
|
||||
Args:
|
||||
prefix: Префикс для переменных окружения (по умолчанию "ABCP")
|
||||
context: Контекст приложения. Если не передан, будет получен через Context()
|
||||
"""
|
||||
self.prefix = prefix.upper()
|
||||
self.context = context if context is not None else Context()
|
||||
|
||||
def get_client_credentials(self, client_name: Optional[str] = None) -> Tuple[str, str]:
|
||||
"""
|
||||
Получает учетные данные для клиента.
|
||||
|
||||
Если client_name не указан, берется из контекста (context.data.get("client")).
|
||||
|
||||
Args:
|
||||
client_name: Имя клиента. Если None, берется из контекста.
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: Кортеж (логин, пароль)
|
||||
|
||||
Raises:
|
||||
ValueError: Если не удалось получить имя клиента или учетные данные не найдены
|
||||
"""
|
||||
if client_name is None:
|
||||
client_name = self.context.data.get("client")
|
||||
if client_name is None:
|
||||
raise ValueError("Имя клиента не указано и не найдено в контексте")
|
||||
|
||||
login_key = f"{self.prefix}_LOGIN_{client_name}"
|
||||
password_key = f"{self.prefix}_PASSWORD_{client_name}"
|
||||
|
||||
login = os.getenv(login_key)
|
||||
password = os.getenv(password_key)
|
||||
|
||||
if login is None or password is None:
|
||||
raise ValueError(
|
||||
f"Учетные данные для клиента '{client_name}' не найдены. "
|
||||
f"Проверьте переменные окружения: {login_key} и {password_key}"
|
||||
)
|
||||
|
||||
logger.debug(f"Получены учетные данные для клиента '{client_name}'")
|
||||
return login, password
|
||||
|
||||
def get_system_credentials(self) -> Tuple[str, str]:
|
||||
"""
|
||||
Получает учетные данные для системной учетной записи.
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: Кортеж (логин, пароль)
|
||||
|
||||
Raises:
|
||||
ValueError: Если учетные данные системной учетной записи не найдены
|
||||
"""
|
||||
login_key = f"{self.prefix}_LOGIN_{self.SYSTEM_ACCOUNT}"
|
||||
password_key = f"{self.prefix}_PASSWORD_{self.SYSTEM_ACCOUNT}"
|
||||
|
||||
login = os.getenv(login_key)
|
||||
password = os.getenv(password_key)
|
||||
|
||||
if login is None or password is None:
|
||||
raise ValueError(
|
||||
f"Учетные данные для системной учетной записи не найдены. "
|
||||
f"Проверьте переменные окружения: {login_key} и {password_key}"
|
||||
)
|
||||
|
||||
logger.debug("Получены учетные данные для системной учетной записи")
|
||||
return login, password
|
||||
|
||||
def get_credentials(self, use_system: bool = False, client_name: Optional[str] = None) -> Tuple[str, str]:
|
||||
"""
|
||||
Универсальный метод для получения учетных данных.
|
||||
|
||||
Args:
|
||||
use_system: Если True, возвращает учетные данные системной учетной записи.
|
||||
Если False, возвращает учетные данные клиента.
|
||||
client_name: Имя клиента. Если None и use_system=False, берется из контекста.
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: Кортеж (логин, пароль)
|
||||
"""
|
||||
if use_system:
|
||||
return self.get_system_credentials()
|
||||
else:
|
||||
return self.get_client_credentials(client_name)
|
||||
@@ -1,2 +1,3 @@
|
||||
from .client import EmailClient
|
||||
from .objects import EmailMessage, EmailAttachment
|
||||
from .utils import EmailUtils
|
||||
|
||||
@@ -11,6 +11,10 @@ from email.header import decode_header
|
||||
import imaplib
|
||||
import smtplib
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# from .objects import EmailMessage, EmailAttachment
|
||||
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
from email.header import decode_header, make_header
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
import email
|
||||
from email import encoders
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.base import MIMEBase
|
||||
from email.header import decode_header
|
||||
import imaplib
|
||||
import smtplib
|
||||
from email.header import make_header, decode_header
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .objects import EmailMessage, EmailAttachment
|
||||
|
||||
@@ -99,8 +94,3 @@ class EmailUtils:
|
||||
return None
|
||||
# убираем пробелы по краям и берём часть после '@'
|
||||
return email_message.strip().split("@", 1)[1]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
from .processor import EmailProcessor
|
||||
@@ -1,37 +0,0 @@
|
||||
import logging
|
||||
import requests
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_processor.order.auto_part_order import OrderStatus
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class InstantOrderTest(AbstractTask):
|
||||
URL = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}"
|
||||
|
||||
def do(self) -> None:
|
||||
api_key = self.config["api_key"]
|
||||
chat_id = self.config["chat_id"]
|
||||
|
||||
if self.order.status == OrderStatus.IN_PROGRESS:
|
||||
positions = self.order.positions
|
||||
|
||||
message = f"Запрос на создание заказа от {self.context['client']}:\n"
|
||||
message += "\n".join(f"{pos.sku}: {pos.name} ({pos.order_quantity} x {pos.order_price} = {pos.total})" for pos in positions)
|
||||
|
||||
elif self.order.status == OrderStatus.OPERATOR_REQUIRED:
|
||||
message = f"Запрос на создание заказа от {self.context['client']} отклонен - необходима ручная обработка.\n"
|
||||
message += f"Причина: {self.order.reason}"
|
||||
|
||||
else:
|
||||
message = f"Запрос на создание заказа от {self.context['client']} отклонен.\n"
|
||||
message += f" Статус заказа: {self.order.status}"
|
||||
|
||||
#url = self.URL.format(api_key, chat_id, message)
|
||||
#resp = requests.get(url).json()
|
||||
print(message)
|
||||
#logger.info(resp)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.abcp_api.abcp_provider import AbcpProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class APIGetStock(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.abcp_provider = AbcpProvider()
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
for position in order.positions:
|
||||
stock = self.get_stock(position.sku, position.manufacturer)
|
||||
position.update_stock(stock, order.delivery_period)
|
||||
position.fill_from_stock()
|
||||
logger.info(f"Получены позиции со склада для файла {attachment.get('name', "no name")}")
|
||||
|
||||
def get_stock(self, sku: str, manufacturer: str) -> int:
|
||||
return self.abcp_provider.get_stock(sku, manufacturer)
|
||||
@@ -1,141 +0,0 @@
|
||||
|
||||
import smtplib
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.base import MIMEBase
|
||||
from email.utils import formatdate
|
||||
from email import encoders
|
||||
from abc import ABC, abstractmethod
|
||||
import os
|
||||
|
||||
|
||||
class AbstractTask(ABC):
|
||||
"""Базовый класс для задач"""
|
||||
|
||||
def __init__(self, context, config):
|
||||
self.context = context
|
||||
self.config = config
|
||||
|
||||
@abstractmethod
|
||||
def do(self):
|
||||
"""Метод для реализации в подклассах"""
|
||||
pass
|
||||
|
||||
|
||||
class EmailReplyTask(AbstractTask):
|
||||
"""Класс для ответа на электронные письма"""
|
||||
|
||||
def do(self):
|
||||
"""
|
||||
Отправляет ответ на входящее письмо
|
||||
|
||||
Ожидает в self.context:
|
||||
- message: email.message.Message объект входящего письма
|
||||
- attachment: путь к файлу для вложения
|
||||
|
||||
Ожидает в self.config:
|
||||
- reply_to: адрес электронной почты для копии
|
||||
- smtp_host: хост SMTP сервера
|
||||
- smtp_port: порт SMTP сервера
|
||||
- smtp_user: пользователь SMTP
|
||||
- smtp_password: пароль SMTP
|
||||
- from_email: адрес отправителя
|
||||
"""
|
||||
|
||||
incoming_message = self.context.get("message")
|
||||
attachment_path = self.context.get("attacnment")
|
||||
|
||||
if not incoming_message:
|
||||
raise ValueError("В context не найдено письмо (message)")
|
||||
|
||||
# Получаем адрес отправителя входящего письма
|
||||
from_addr = incoming_message.get("From")
|
||||
if not from_addr:
|
||||
raise ValueError("Входящее письмо не содержит адреса отправителя")
|
||||
|
||||
# Создаем ответное письмо
|
||||
reply_message = MIMEMultipart()
|
||||
|
||||
# Заголовки ответного письма
|
||||
reply_message["From"] = self.config.get("from_email", "noreply@example.com")
|
||||
reply_message["To"] = from_addr
|
||||
reply_message["Cc"] = self.config.get("reply_to", "")
|
||||
reply_message["Subject"] = f"Re: {incoming_message.get('Subject', '')}"
|
||||
reply_message["Date"] = formatdate(localtime=True)
|
||||
|
||||
# Тело письма
|
||||
body = "Ваш заказ создан"
|
||||
reply_message.attach(MIMEText(body, "plain", "utf-8"))
|
||||
|
||||
# Добавляем вложение если указан путь к файлу
|
||||
if attachment_path and os.path.isfile(attachment_path):
|
||||
self._attach_file(reply_message, attachment_path)
|
||||
|
||||
# Отправляем письмо
|
||||
self._send_email(reply_message, from_addr)
|
||||
|
||||
def _attach_file(self, message, file_path):
|
||||
"""
|
||||
Добавляет файл в качестве вложения к письму
|
||||
|
||||
Args:
|
||||
message: MIMEMultipart объект
|
||||
file_path: путь к файлу для вложения
|
||||
"""
|
||||
try:
|
||||
with open(file_path, "rb") as attachment:
|
||||
part = MIMEBase("application", "octet-stream")
|
||||
part.set_payload(attachment.read())
|
||||
|
||||
encoders.encode_base64(part)
|
||||
|
||||
file_name = os.path.basename(file_path)
|
||||
part.add_header(
|
||||
"Content-Disposition",
|
||||
f"attachment; filename= {file_name}"
|
||||
)
|
||||
|
||||
message.attach(part)
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(f"Файл не найден: {file_path}")
|
||||
except Exception as e:
|
||||
raise Exception(f"Ошибка при добавлении вложения: {str(e)}")
|
||||
|
||||
def _send_email(self, message, recipient):
|
||||
"""
|
||||
Отправляет письмо через SMTP
|
||||
|
||||
Args:
|
||||
message: MIMEMultipart объект письма
|
||||
recipient: адрес получателя
|
||||
"""
|
||||
try:
|
||||
smtp_host = self.config.get("smtp_host")
|
||||
smtp_port = self.config.get("smtp_port", 587)
|
||||
smtp_user = self.config.get("smtp_user")
|
||||
smtp_password = self.config.get("smtp_password")
|
||||
|
||||
if not all([smtp_host, smtp_user, smtp_password]):
|
||||
raise ValueError("Не указаны параметры SMTP в config")
|
||||
|
||||
with smtplib.SMTP(smtp_host, smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(smtp_user, smtp_password)
|
||||
|
||||
# Получаем адреса получателей (основной + копия)
|
||||
recipients = [recipient]
|
||||
reply_to = self.config.get("reply_to")
|
||||
if reply_to:
|
||||
recipients.append(reply_to)
|
||||
|
||||
server.sendmail(
|
||||
self.config.get("from_email"),
|
||||
recipients,
|
||||
message.as_string()
|
||||
)
|
||||
except smtplib.SMTPException as e:
|
||||
raise Exception(f"Ошибка SMTP: {str(e)}")
|
||||
except Exception as e:
|
||||
raise Exception(f"Ошибка при отправке письма: {str(e)}")
|
||||
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_processor.order.auto_part_order import OrderStatus
|
||||
from mail_order_bot.email_processor.order.auto_part_position import AutoPartPosition, PositionStatus
|
||||
from decimal import Decimal
|
||||
import random
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LocalStoreOrder(AbstractTask):
|
||||
"""Сейчас логика такая
|
||||
- ищем на складе наш сапплиер код, берем самую дешевую позицию и делаем заказ из нее
|
||||
|
||||
Другие чуть более дорогие не рассматриваем
|
||||
|
||||
"""
|
||||
# это код нашего склада
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data["attachments"]
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
|
||||
@@ -64,6 +64,7 @@ async def main():
|
||||
#await app.stop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(os.getcwd())
|
||||
if os.environ.get("APP_ENV") != "PRODUCTION":
|
||||
logger.warning("Non production environment")
|
||||
load_dotenv()
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
"""Классы для работы с сущностью заказа и позиции"""
|
||||
from .auto_part_order import AutoPartOrder, OrderStatus
|
||||
from .auto_part_position import AutoPartPosition, PositionStatus
|
||||
@@ -1,8 +1,11 @@
|
||||
from typing import List, Optional
|
||||
from .auto_part_position import AutoPartPosition, PositionStatus
|
||||
|
||||
from mail_order_bot.task_processor.handlers.abcp.stock_selector import StockSelector
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class OrderStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
@@ -13,7 +16,8 @@ class OrderStatus(Enum):
|
||||
|
||||
|
||||
class AutoPartOrder:
|
||||
def __init__(self):
|
||||
def __init__(self, client_id):
|
||||
self.client_id = client_id
|
||||
self.positions: List[AutoPartPosition] = []
|
||||
self.status = OrderStatus.NEW
|
||||
self.delivery_period = 0
|
||||
@@ -35,11 +39,15 @@ class AutoPartOrder:
|
||||
self.delivery_period = delivery_period
|
||||
|
||||
def fill_from_local_supplier(self) -> None:
|
||||
"""
|
||||
Выбирает оптимального поставщика для всех позиций заказа.
|
||||
Предполагается, что остатки уже получены и обработаны.
|
||||
"""
|
||||
|
||||
|
||||
for position in self.positions:
|
||||
errors = position.fill_from_stock()
|
||||
self.errors += errors
|
||||
|
||||
|
||||
selector = StockSelector(position, self.delivery_period)
|
||||
selector.select_optimal_supplier()
|
||||
|
||||
def check_order(self, config) -> None:
|
||||
""" Проверяет заказ на возможность исполнения"""
|
||||
@@ -54,6 +62,5 @@ class AutoPartOrder:
|
||||
f"({refusal_positions_count} из {len(self.positions)})")
|
||||
self.status = OrderStatus.OPERATOR_REQUIRED
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return len(self.positions)
|
||||
@@ -4,24 +4,26 @@ from typing import Dict, Any
|
||||
from decimal import Decimal
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class PositionStatus(Enum):
|
||||
NEW = "new" # Новая позиция
|
||||
STOCK_RECIEVED = "stock_received" # Получен остаток
|
||||
STOCK_FAILED = "stock_failed" # Остаток не получен
|
||||
NO_AVAILABLE_STOCK = "no_available_stock" # Нет доступных складов
|
||||
READY = "ready"
|
||||
READY_PARTIAL = "ready_partial"
|
||||
ORDERED = "ordered" # Заказано
|
||||
REFUSED = "refused" # Отказано
|
||||
|
||||
|
||||
@dataclass
|
||||
class AutoPartPosition:
|
||||
DISTRIBUTOR_ID = "1577730"
|
||||
"""
|
||||
Унифицированная модель позиции для заказа.
|
||||
Все контрагенты приводятся к этой структуре.
|
||||
"""
|
||||
|
||||
DISTRIBUTOR_ID = 1577730 # ID локального склада
|
||||
|
||||
sku: str # Артикул товара
|
||||
manufacturer: str # Производитель
|
||||
|
||||
@@ -30,7 +32,7 @@ class AutoPartPosition:
|
||||
|
||||
total: Decimal = 0 # Общая сумма
|
||||
name: str = "" # Наименование
|
||||
|
||||
order_delivery_period: int = 0
|
||||
order_quantity: int = 0 # Количество для заказа
|
||||
order_price: Decimal = Decimal('0.0') # Цена в заказе
|
||||
order_item: Dict[str, Any] = field(default_factory=dict)
|
||||
@@ -48,27 +50,9 @@ class AutoPartPosition:
|
||||
if self.requested_price < 0:
|
||||
raise ValueError(f"Цена не может быть отрицательной: {self.requested_price}")
|
||||
|
||||
def update_stock(self, stock: Dict[str, Any], delivery_period: int = 0) -> None:
|
||||
if stock["success"]:
|
||||
available_distributors = stock["data"]
|
||||
|
||||
# Для доставки только с локального склада сперва убираем все остальные склады
|
||||
if delivery_period == 0:
|
||||
available_distributors = self._filter_only_local_storage(available_distributors)
|
||||
|
||||
#Отбираем склады по сроку доставки
|
||||
available_distributors = self._filter_proper_delivery_time(available_distributors, delivery_period)
|
||||
|
||||
# Убираем дорогие склады с ценой выше запрошенной
|
||||
available_distributors = self._filter_proper_price(available_distributors)
|
||||
|
||||
# Убираем отрицательные остатки
|
||||
available_distributors = self._filter_proper_availability(available_distributors)
|
||||
|
||||
# Сортируем по цене
|
||||
available_distributors.sort(key=lambda item: Decimal(item["price"]), reverse=False)
|
||||
|
||||
self.stock = available_distributors
|
||||
def set_stock(self, stock):
|
||||
if stock.get("success"):
|
||||
self.stock = stock["data"]
|
||||
if len(self.stock):
|
||||
self.status = PositionStatus.STOCK_RECIEVED
|
||||
else:
|
||||
@@ -76,43 +60,50 @@ class AutoPartPosition:
|
||||
else:
|
||||
self.status = PositionStatus.STOCK_FAILED
|
||||
|
||||
|
||||
def fill_from_stock(self):
|
||||
def set_order_item(self):
|
||||
"""Выбирает позицию для заказа"""
|
||||
if self.status == PositionStatus.STOCK_RECIEVED:
|
||||
available_distributors = self.stock
|
||||
|
||||
for distributor in self.stock:
|
||||
distributor["profit"] = int(distributor["availability"]) * self.requested_price - int(distributor["availability"]) * Decimal(distributor["price"])
|
||||
# BR-1. Отсекаем склады для заказов из наличия (только локальный склад)
|
||||
if self.order_delivery_period == 0:
|
||||
available_distributors = self._filter_only_local_storage(available_distributors)
|
||||
|
||||
self.stock.sort(key=lambda item: item["profit"], reverse=True)
|
||||
# BR-2. Цена не должна превышать цену из заказа
|
||||
available_distributors = self._filter_proper_price(available_distributors)
|
||||
|
||||
self.order_quantity = self.stock[0]["availability"]
|
||||
self.order_price = self.requested_price
|
||||
# BR-3. Срок доставки не должен превышать ожидаемый
|
||||
available_distributors = self._filter_proper_delivery_time(available_distributors)
|
||||
|
||||
# BR-4. Без отрицательных остатков
|
||||
available_distributors = self._filter_proper_availability(available_distributors)
|
||||
|
||||
# Приоритет на склады с полным стоком
|
||||
|
||||
# BR-5. Сначала оборачиваем локальный склад, потом удаленные
|
||||
|
||||
# BR-6. Выбираем цену максимально близкую к цене из заказа (максимальная)
|
||||
available_distributors.sort(key=lambda item: Decimal(item["price"]), reverse=True)
|
||||
|
||||
if len(available_distributors):
|
||||
self.order_item = self.stock[0]
|
||||
|
||||
self.status = PositionStatus.READY
|
||||
else:
|
||||
self.status = PositionStatus.NO_AVAILABLE_STOCK
|
||||
|
||||
def _filter_only_local_storage(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует только локальные склады"""
|
||||
|
||||
def _filter_only_local_storage(self, distributors):
|
||||
return [item for item in distributors if str(item["distributorId"]) == self.DISTRIBUTOR_ID]
|
||||
return [item for item in distributors if item["distributorId"] == self.DISTRIBUTOR_ID]
|
||||
|
||||
def _filter_proper_delivery_time(self, distributors, delivery_period):
|
||||
return [item for item in distributors if item["deliveryPeriod"] <= delivery_period]
|
||||
def _filter_proper_delivery_time(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады по сроку доставки"""
|
||||
return [item for item in distributors if item["deliveryPeriod"] <= self.order_delivery_period]
|
||||
|
||||
def _filter_proper_price(self, distributors):
|
||||
def _filter_proper_price(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады по цене (убирает дорогие)"""
|
||||
return [item for item in distributors if Decimal(item["price"]) <= self.requested_price]
|
||||
|
||||
def _filter_proper_availability(self, distributors):
|
||||
def _filter_proper_availability(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады с положительным остатком"""
|
||||
return [item for item in distributors if Decimal(item["availability"]) > 0]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
4
src/mail_order_bot/parsers/__init__.py
Normal file
4
src/mail_order_bot/parsers/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
"""Данный пакет содержит модули и классы дял парсинга объектов"""
|
||||
|
||||
from .excel_parcer import ExcelFileParcer
|
||||
from .order_parcer import OrderParser
|
||||
106
src/mail_order_bot/parsers/excel_parcer.py
Normal file
106
src/mail_order_bot/parsers/excel_parcer.py
Normal file
@@ -0,0 +1,106 @@
|
||||
import logging
|
||||
import pandas as pd
|
||||
from io import BytesIO
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ExcelFileParcer:
|
||||
def __init__(self, file_bytes, config):
|
||||
self.config = config
|
||||
self.bytes = file_bytes
|
||||
self.sheet_name = self.config.get("sheet_name", 0)
|
||||
self.df = self._parse_file(file_bytes)
|
||||
|
||||
def _parse_file(self, file_bytes):
|
||||
"""Парсит вложение в формате эл таблиц"""
|
||||
try:
|
||||
df = pd.read_excel(file_bytes, sheet_name=self.sheet_name, header=None)
|
||||
except Exception as e:
|
||||
df = None
|
||||
logger.warning("Не удалось распарсить значение файла")
|
||||
return df
|
||||
|
||||
def set_value(self, sku, manufacturer, column, value):
|
||||
"""Устанавливает значение в строке позиции в заданной колонке"""
|
||||
# Находим строку (ось Y)
|
||||
attr_row = self._get_attr_row(sku, manufacturer)
|
||||
|
||||
# Находим колонку (ось X)
|
||||
attr_col = self._get_attr_column(column)
|
||||
|
||||
self.df.iloc[attr_row, attr_col] = value
|
||||
logger.debug(
|
||||
f"Установлено значение {value} в колонке {column} для строки {attr_row} ( {sku} | {manufacturer} )")
|
||||
|
||||
def get_file_bytes(self):
|
||||
"Этот метод будет возвращать эксель из датафрейма в виде байтов"
|
||||
buf = BytesIO()
|
||||
|
||||
with pd.ExcelWriter(buf, engine="xlsxwriter") as writer:
|
||||
self.df.to_excel(writer, sheet_name="Sheet1", index=False, header=False)
|
||||
|
||||
buf.seek(0)
|
||||
return buf
|
||||
|
||||
def get_order_rows(self):
|
||||
"Будет такой метод или какой-то другой который формирует файл с заказом"
|
||||
# Получаем все данные из файла
|
||||
|
||||
# Находим индекс строки с заголовком
|
||||
key_field = self.config.get("key_field")
|
||||
header_row_idx = self.df[
|
||||
self.df.apply(lambda row: row.astype(str).str.contains(key_field, case=False, na=False).any(),
|
||||
axis=1)].index[0]
|
||||
|
||||
# Считываем таблицу с правильным заголовком
|
||||
df = pd.read_excel(self.bytes, header=header_row_idx, sheet_name=self.sheet_name, engine='calamine') # openpyxl calamine
|
||||
|
||||
# Находим индекс первой строки с пустым 'Артикул'
|
||||
first_empty_index = df[df[key_field].isna()].index.min()
|
||||
|
||||
# Обрезаем DataFrame до первой пустой строки (не включая её)
|
||||
df_trimmed = df.loc[:first_empty_index - 1]
|
||||
|
||||
return df_trimmed
|
||||
|
||||
def _get_header_row(self):
|
||||
"""Метод возвращает строку заголовка по наличию в ней ключевого слова. Поиск заголовка нужен при определении колонок с данными."""
|
||||
|
||||
key_column = self.config.get("key_field")
|
||||
header_row_idx = int(
|
||||
self.df.apply(lambda row: row.astype(str).str.contains(key_column, na=False).any(), axis=1).idxmax())
|
||||
# todo надо выкинуть ошибку если в файле не найдено ключевое поле
|
||||
# todo надо выкинуть ошибку если найдено несколько строк с CONFIG_KEY_COLUMN
|
||||
logger.debug(f"Индекс строки заголовка - {header_row_idx}")
|
||||
return header_row_idx
|
||||
|
||||
def _get_attr_column(self, col_name):
|
||||
"""Поиск по оси Х - метод возвращает индекс колонки по названию атрибута"""
|
||||
header_row_idx = self._get_header_row()
|
||||
|
||||
header_row = self.df.iloc[header_row_idx]
|
||||
|
||||
col_id = header_row[header_row == col_name].index[0]
|
||||
# todo добавить перехват ошибок и выброс понятного и сключения при отсутствии колонки
|
||||
logger.debug(f"Индекс колонки {col_name} - {col_id}")
|
||||
return int(col_id)
|
||||
|
||||
def _get_attr_row(self, sku, manufacturer):
|
||||
"""Поиск по оси Y - метод возвращает индекс строки по бренду и артикулу"""
|
||||
|
||||
sku_col_name = self.config["mapping"]["article"]
|
||||
sku_col_idx = self._get_attr_column(sku_col_name)
|
||||
|
||||
man_col_name = self.config["mapping"]["manufacturer"]
|
||||
man_col_idx = self._get_attr_column(man_col_name)
|
||||
|
||||
matching_rows = self.df[
|
||||
(self.df.iloc[:, sku_col_idx] == sku) & (self.df.iloc[:, man_col_idx] == manufacturer)].index
|
||||
|
||||
# todo сделать проверку на наличие дублей
|
||||
logger.info(f"Индекс строки позиции {sku}/{manufacturer} - {matching_rows}")
|
||||
|
||||
return matching_rows.values[0]
|
||||
|
||||
|
||||
83
src/mail_order_bot/parsers/order_parcer.py
Normal file
83
src/mail_order_bot/parsers/order_parcer.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import logging
|
||||
import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
|
||||
from mail_order_bot.order import AutoPartPosition
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OrderParser:
|
||||
"""
|
||||
Универсальный парсер, настраиваемый через конфигурацию.
|
||||
Подходит для большинства стандартных случаев.
|
||||
"""
|
||||
|
||||
def __init__(self, mapping, delivery_period):
|
||||
self.mapping = mapping
|
||||
self.delivery_period = delivery_period
|
||||
|
||||
def parse(self, order, df):
|
||||
# Парсим строки
|
||||
positions = []
|
||||
for idx, row in df.iterrows():
|
||||
position = self._parse_row(row, self.mapping)
|
||||
if position:
|
||||
order.add_position(position)
|
||||
|
||||
logger.info(f"Успешно обработано {len(order)} позиций из {len(df)} строк")
|
||||
|
||||
# except Exception as e:
|
||||
# logger.error(f"Ошибка при обработке файла: {e}")
|
||||
# else:
|
||||
return order
|
||||
|
||||
def _parse_row(self, row: pd.Series, mapping: Dict[str, str]) -> Optional[AutoPartPosition]:
|
||||
"""Парсит одну строку Excel в OrderPosition"""
|
||||
|
||||
# Проверяем обязательные поля
|
||||
required_fields = ['article', 'price', 'quantity']
|
||||
|
||||
for field in required_fields:
|
||||
if pd.isna(row.get(mapping[field])):
|
||||
logger.warning(f"Позиция не создана - не заполнено поле {mapping[field]}")
|
||||
return None
|
||||
|
||||
price = Decimal(str(row[mapping['price']]).replace(",", ".").strip())
|
||||
quantity = int(row[mapping['quantity']])
|
||||
|
||||
if "total" in mapping.keys():
|
||||
total = Decimal(str(row[mapping['total']]).replace(",", ".").strip())
|
||||
else:
|
||||
total = price * quantity
|
||||
|
||||
if "name" in mapping:
|
||||
name = str(row[mapping.get('name', "")]).strip()
|
||||
else:
|
||||
name = ""
|
||||
|
||||
# Создаем объект позиции
|
||||
position = AutoPartPosition(
|
||||
sku=str(row[mapping['article']]).strip(),
|
||||
manufacturer=str(row[mapping.get('manufacturer', "")]).strip(),
|
||||
name=name,
|
||||
requested_price=price,
|
||||
requested_quantity=quantity,
|
||||
total=total,
|
||||
order_delivery_period=self.delivery_period,
|
||||
additional_attrs=self._extract_additional_attrs(row, mapping)
|
||||
)
|
||||
return position
|
||||
|
||||
def _extract_additional_attrs(self, row: pd.Series, mapping: Dict[str, str]) -> Dict[str, Any]:
|
||||
"""Извлекает дополнительные атрибуты, не входящие в основную модель"""
|
||||
additional = {}
|
||||
mapped_columns = set(mapping.values())
|
||||
|
||||
for col in row.index:
|
||||
if col not in mapped_columns and not pd.isna(row[col]):
|
||||
additional[col] = row[col]
|
||||
|
||||
return additional
|
||||
1
src/mail_order_bot/task_processor/__init__.py
Normal file
1
src/mail_order_bot/task_processor/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .processor import TaskProcessor
|
||||
@@ -1,14 +1,8 @@
|
||||
|
||||
|
||||
from .attachment_handler.attachment_handler import AttachmentHandler
|
||||
from .excel_parcers.order_parcer_basic import BasicExcelParser
|
||||
from .destination_time.local_store import DeliveryPeriodLocalStore
|
||||
|
||||
|
||||
|
||||
from .abcp.api_get_stock import GetStock
|
||||
from .abcp.api_create_order import InstantOrderTest
|
||||
|
||||
|
||||
|
||||
from .notifications.test_notifier import TestNotifier
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import logging
|
||||
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
from mail_order_bot.abcp_api.abcp_provider import AbcpProvider
|
||||
from mail_order_bot.credential_provider import CredentialProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class APIGetStock(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
credential_provider = CredentialProvider(context=self.context)
|
||||
|
||||
# Создаем провайдер для учетной записи клиента
|
||||
client_login, client_password = credential_provider.get_client_credentials()
|
||||
self.client_provider = AbcpProvider(login=client_login, password=client_password)
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
for position in order.positions:
|
||||
# Получаем остатки из-под учетной записи клиента
|
||||
client_stock = self.client_provider.get_stock(position.sku, position.manufacturer)
|
||||
|
||||
position.set_stock(client_stock)
|
||||
position.set_order_item()
|
||||
|
||||
logger.info(f"Получены позиции со склада для файла {attachment.get('name', "no name")}")
|
||||
|
||||
def get_stock(self, sku: str, manufacturer: str) -> int:
|
||||
return self.client_provider.get_stock(sku, manufacturer)
|
||||
@@ -0,0 +1,95 @@
|
||||
import sys
|
||||
from turtle import position
|
||||
from typing import Dict, Any, List, Optional
|
||||
from decimal import Decimal
|
||||
from mail_order_bot.email_processor.order.auto_part_position import AutoPartPosition, PositionStatus
|
||||
|
||||
logger = __import__('logging').getLogger(__name__)
|
||||
|
||||
|
||||
"""
|
||||
1. Получить 2 вида складских остатков
|
||||
2. Добавляем цену закупки
|
||||
3. Применяем правила фильтрации
|
||||
- можно указать какие правила применены
|
||||
4. Выбираем цену по профиту
|
||||
- можно указать ограничения
|
||||
|
||||
|
||||
|
||||
"""
|
||||
class StockSelector:
|
||||
"""
|
||||
Класс для выбора оптимального поставщика для позиции заказа.
|
||||
Выполняет фильтрацию складов и выбор наилучшего варианта.
|
||||
"""
|
||||
|
||||
DISTRIBUTOR_ID = "1577730" # ID локального склада
|
||||
|
||||
def __init__(self, position: AutoPartPosition, delivery_period: int = 0):
|
||||
"""
|
||||
Инициализация StockSelector.
|
||||
|
||||
Args:
|
||||
position: Позиция заказа
|
||||
delivery_period: Период доставки в днях
|
||||
"""
|
||||
self.position = position
|
||||
self.delivery_period = delivery_period
|
||||
self.client_stock = None
|
||||
self.system_stock = None
|
||||
|
||||
|
||||
def filter_stock(self, client_stock) -> None:
|
||||
"""
|
||||
Обрабатывает результат запроса остатков из-под учетной записи клиента и обновляет позицию.
|
||||
|
||||
Args:
|
||||
client_stock: Результат запроса остатков от API из-под учетной записи клиента
|
||||
system_stock: Результат запроса остатков от API из-под системной учетной записи
|
||||
"""
|
||||
self.client_stock = client_stock
|
||||
|
||||
if client_stock["success"]:
|
||||
available_distributors = client_stock["data"]
|
||||
|
||||
# Для доставки только с локального склада сперва убираем все остальные склады
|
||||
if self.delivery_period == 0:
|
||||
available_distributors = self._filter_only_local_storage(available_distributors)
|
||||
|
||||
# Отбираем склады по сроку доставки
|
||||
available_distributors = self._filter_proper_delivery_time(available_distributors, self.delivery_period)
|
||||
|
||||
# Убираем дорогие склады с ценой выше запрошенной
|
||||
available_distributors = self._filter_proper_price(available_distributors)
|
||||
|
||||
# Убираем отрицательные остатки
|
||||
available_distributors = self._filter_proper_availability(available_distributors)
|
||||
|
||||
# Добавляем данные о закупочных ценах
|
||||
available_distributors = self._set_system_price(available_distributors)
|
||||
|
||||
# Сортируем по цене
|
||||
available_distributors.sort(key=lambda item: Decimal(item["price"]), reverse=True)
|
||||
|
||||
return available_distributors
|
||||
|
||||
else:
|
||||
return None
|
||||
|
||||
def _filter_only_local_storage(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует только локальные склады"""
|
||||
return [item for item in distributors if str(item["distributorId"]) == self.DISTRIBUTOR_ID]
|
||||
|
||||
def _filter_proper_delivery_time(self, distributors: List[Dict[str, Any]], delivery_period: int) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады по сроку доставки"""
|
||||
return [item for item in distributors if item["deliveryPeriod"] <= delivery_period]
|
||||
|
||||
def _filter_proper_price(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады по цене (убирает дорогие)"""
|
||||
return [item for item in distributors if Decimal(item["price"]) <= self.position.requested_price]
|
||||
|
||||
def _filter_proper_availability(self, distributors: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""Фильтрует склады с положительным остатком"""
|
||||
return [item for item in distributors if Decimal(item["availability"]) > 0]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
|
||||
import logging
|
||||
@@ -0,0 +1,91 @@
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
|
||||
import logging
|
||||
import re
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DeliveryPeriodFromSubject(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def do(self) -> None:
|
||||
"""
|
||||
Извлекает срок доставки из темы письма и сохраняет в каждый элемент attachments.
|
||||
|
||||
Правила парсинга:
|
||||
- Если есть слово "Наличие" - срок доставки = 0 дней
|
||||
- Если найдены оба варианта (диапазон и точное значение) - используется точное значение
|
||||
- Если есть только фраза "N-M дней/дня/день" (диапазон) - срок доставки = минимальное значение (N)
|
||||
- Если есть только фраза "N дней/дня/день" - срок доставки = N дней
|
||||
- Если ничего не указано - срок доставки = 0 дней
|
||||
- Срок переводится в часы (умножается на 24)
|
||||
"""
|
||||
# Получаем тему письма
|
||||
email_subj = self.context.data.get("email_subj", "")
|
||||
if not email_subj:
|
||||
logger.warning("Тема письма не найдена в контексте")
|
||||
email_subj = ""
|
||||
|
||||
# Парсим срок доставки
|
||||
delivery_days = self._parse_delivery_period(email_subj)
|
||||
|
||||
# Переводим в часы
|
||||
delivery_time = delivery_days * 24
|
||||
|
||||
logger.info(f"Извлечен срок доставки из темы: {delivery_days} дней ({delivery_time} часов)")
|
||||
|
||||
# Сохраняем в каждый элемент attachments
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
attachment["delivery_time"] = delivery_time
|
||||
|
||||
logger.debug(f"Срок доставки сохранен в {len(attachments)} вложений")
|
||||
|
||||
def _parse_delivery_period(self, subject: str) -> int:
|
||||
"""
|
||||
Парсит срок доставки из темы письма.
|
||||
|
||||
Args:
|
||||
subject: Тема письма
|
||||
|
||||
Returns:
|
||||
Количество дней доставки (0 по умолчанию)
|
||||
"""
|
||||
if not subject:
|
||||
return 0
|
||||
|
||||
subject_lower = subject.lower()
|
||||
|
||||
# Проверяем наличие слова "Наличие"
|
||||
if "наличие" in subject_lower:
|
||||
return 0
|
||||
|
||||
# Ищем оба паттерна одновременно
|
||||
range_pattern = r'(\d+)-(\d+)\s+(?:дней|дня|день)'
|
||||
single_pattern = r'(\d+)\s+(?:дней|дня|день)'
|
||||
|
||||
range_match = re.search(range_pattern, subject_lower)
|
||||
single_match = re.search(single_pattern, subject_lower)
|
||||
|
||||
# Если найдены оба варианта - используем точное значение (одиночное число)
|
||||
if range_match and single_match:
|
||||
days = int(single_match.group(1))
|
||||
logger.debug(f"Найдены оба варианта (диапазон и точное значение), используется точное: {days} дней")
|
||||
return days
|
||||
|
||||
# Если найден только диапазон - используем минимальное значение
|
||||
if range_match:
|
||||
min_days = int(range_match.group(1))
|
||||
max_days = int(range_match.group(2))
|
||||
logger.debug(f"Найден диапазон: {min_days}-{max_days} дней, используется минимальное: {min_days} дней")
|
||||
return min(min_days, max_days)
|
||||
|
||||
# Если найдено только одиночное число - используем его
|
||||
if single_match:
|
||||
days = int(single_match.group(1))
|
||||
logger.debug(f"Найдено точное значение: {days} дней")
|
||||
return days
|
||||
|
||||
# Если ничего не найдено, возвращаем 0 (из наличия)
|
||||
return 0
|
||||
@@ -1,5 +1,4 @@
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
|
||||
import logging
|
||||
|
||||
@@ -12,8 +11,7 @@ class DeliveryPeriodLocalStore(AbstractTask):
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data["attachments"]
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
order.set_delivery_period(0)
|
||||
attachment["delivery_period"] = 0
|
||||
logger.info(f"Доставка только с локального склада, срок 1 день.")
|
||||
|
||||
|
||||
@@ -3,16 +3,16 @@ import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.email_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
#from mail_order_bot.task_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
|
||||
from ...order.auto_part_position import AutoPartPosition
|
||||
from ...order.auto_part_order import AutoPartOrder
|
||||
from mail_order_bot.task_processor.order.auto_part_position import AutoPartPosition
|
||||
from mail_order_bot.task_processor.order.auto_part_order import AutoPartOrder
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BasicExcelParser(AbstractTask):
|
||||
class OrderParser(AbstractTask):
|
||||
"""
|
||||
Универсальный парсер, настраиваемый через конфигурацию.
|
||||
Подходит для большинства стандартных случаев.
|
||||
@@ -27,23 +27,27 @@ class BasicExcelParser(AbstractTask):
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
file_bytes = BytesIO(attachment['bytes']) # self.context.get("attachment") #
|
||||
try:
|
||||
delivery_period = attachment.get("delivery_period", 0)
|
||||
#try:
|
||||
df = self._make_dataframe(file_bytes)
|
||||
mapping = self.config['mapping']
|
||||
mapping = self.config["mapping"]
|
||||
client_id = self.config["client_id"]
|
||||
order = AutoPartOrder()
|
||||
attachment["order"] = order
|
||||
|
||||
# Парсим строки
|
||||
positions = []
|
||||
for idx, row in df.iterrows():
|
||||
position = self._parse_row(row, mapping)
|
||||
if position:
|
||||
position.order_delivery_period = delivery_period
|
||||
order.add_position(position)
|
||||
|
||||
logger.info(f"Успешно обработано {len(order)} позиций из {len(df)} строк")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обработке файла: {e}")
|
||||
else:
|
||||
#except Exception as e:
|
||||
# logger.error(f"Ошибка при обработке файла: {e}")
|
||||
#else:
|
||||
attachment["order"] = order
|
||||
|
||||
|
||||
@@ -67,7 +71,7 @@ class BasicExcelParser(AbstractTask):
|
||||
else:
|
||||
total = price * quantity
|
||||
|
||||
if mapping.get('name', "") in mapping.keys():
|
||||
if "name" in mapping:
|
||||
name = str(row[mapping.get('name', "")]).strip()
|
||||
else:
|
||||
name = ""
|
||||
@@ -3,7 +3,7 @@ import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.email_processor.handlers.order_position import OrderPosition
|
||||
#from mail_order_bot.task_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
|
||||
from ...order.auto_part_position import AutoPartPosition
|
||||
@@ -0,0 +1,34 @@
|
||||
import logging
|
||||
import pandas as pd
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.task_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.task_processor.abstract_task import AbstractTask
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ExcelExtractor(AbstractTask):
|
||||
"""
|
||||
Хендлер для каждого вложения считывает эксель файл и сохраняет его контекст
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def do(self) -> None:
|
||||
|
||||
# todo сделать проверку на наличие файла и его тип
|
||||
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
file_bytes = BytesIO(attachment['bytes'])
|
||||
|
||||
# Получаем все данные из файла
|
||||
sheet_name = self.config.get("sheet_name", 0)
|
||||
try:
|
||||
attachment["sheet"] = pd.read_excel(file_bytes, sheet_name=sheet_name, header=None)
|
||||
except Exception as e:
|
||||
attachment["sheet"] = None
|
||||
logger.warning("Не удалось распарсить значение файла")
|
||||
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ from mail_order_bot.context import Context
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
from enum import Enum
|
||||
|
||||
from mail_order_bot.email_processor.handlers import *
|
||||
from mail_order_bot.task_processor.handlers import *
|
||||
|
||||
from mail_order_bot.email_processor.handlers import AttachmentHandler
|
||||
from mail_order_bot.task_processor.handlers import AttachmentHandler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -24,7 +24,7 @@ class RequestStatus(Enum):
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class EmailProcessor:
|
||||
class TaskProcessor:
|
||||
def __init__(self, configs_path: str):
|
||||
super().__init__()
|
||||
self.context = Context()
|
||||
@@ -40,8 +40,17 @@ class EmailProcessor:
|
||||
|
||||
# Определить клиента
|
||||
email_body = EmailUtils.extract_body(email)
|
||||
self.context.data["email_body"] = email_body
|
||||
|
||||
email_from = EmailUtils.extract_first_sender(email_body)
|
||||
self.context.data["email_from"] = email_from
|
||||
|
||||
email_subj = EmailUtils.extract_header(email, "subj")
|
||||
self.context.data["email_subj"] = email_subj
|
||||
|
||||
client = EmailUtils.extract_domain(email_from)
|
||||
self.context.data["client"] = client
|
||||
|
||||
|
||||
try:
|
||||
# Определить конфиг для пайплайна
|
||||
@@ -13,10 +13,12 @@ if __name__ == "__main__":
|
||||
position = AutoPartPosition(sku="560300054", manufacturer="VST", requested_quantity=1)
|
||||
order.add_position(position)
|
||||
|
||||
provider = AbcpProvider()
|
||||
login = os.getenv('ABCP_LOGIN_SYSTEM')
|
||||
password = os.getenv('ABCP_PASSWORD_SYSTEM')
|
||||
provider = AbcpProvider(login=login, password=password)
|
||||
|
||||
provider.get_stock(order)
|
||||
result = provider.get_stock(position.sku, position.manufacturer)
|
||||
|
||||
print(order.positions[0].stock)
|
||||
|
||||
print(os.getenv('ABCP_LOGIN'))
|
||||
print(os.getenv('ABCP_LOGIN_SYSTEM'))
|
||||
Reference in New Issue
Block a user