Files
Excel-project/server/backend/handlers/ozon_purchases_handler.py
2026-01-03 19:48:18 +03:00

32 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pydantic import ValidationError
from server.backend.schemas.pydantic import ExcelInfo, settings,Translit
from server.backend.api.nomenclature import processing
import re
def process_sheet(df, real_arti:int, real_quantity:int, real_sum_1:int):
#Выборка
df = df[[real_arti, real_quantity, real_sum_1]].copy().dropna() #copy and drop all NA values
df = df[(df != 0).all(axis=1)] #drop all 0 values
df = df[[real_arti, real_quantity, real_sum_1]]
df.rename(columns={real_arti: 'arti', real_quantity: 'counts', real_sum_1: 'price'}, inplace=True) #переименовываем для pydantic
#Нормализация
df['arti'] = df['arti'].replace(Translit.TRANSLIT, regex=True)
df['arti'] = df['arti'].astype(str).str.upper().str.extract(f'({settings.PATTERN})') #arti под regex
df['price'] = df['price'].astype(float) #Float to Int, if exists
df['counts'] = df['counts'].astype(int) #Float to Int, if exists
#Группировка
df = processing(df) #vlookup for ref_keys
validated_rows, errors = [], []
for i, row in df.iterrows(): #проходит построчно по df, где i - индекс строки, row - данные строки
try:
validated_rows.append(ExcelInfo(**row.to_dict())) #добавляем в список проверенные данные полученные от pydantic, которые туда передаются в виде dict
except ValidationError as e:
errors.append((i, e.errors())) #выводит ошибку и пишет номер строки
if errors:
raise Exception(f"There are some errors with validation in Отчет о выкупленных товарах, check it ", errors)
return validated_rows
def evaluating(dfs):
validated_rows_1 = process_sheet(dfs["Отчет о выкупленных товарах"], real_arti=3,real_quantity=10, real_sum_1=11) # номера столбцов от озона
return validated_rows_1