from typing import List, Optional, Dict from pydantic import BaseModel, Field, confloat, constr class SumNumberTwoFormat(BaseModel): __root__: Optional[confloat(ge=0.0, le=100000000.0, multiple_of=0.01)] class NumberPrice(BaseModel): __root__: confloat(ge=0.0, le=42949673.0, multiple_of=0.01) class PhoneNumber(BaseModel): __root__: constr(regex=r'^([^\s\\]{0,17}|\+[^\s\\]{1,18})$') class NumberTwoFormat(BaseModel): __root__: confloat(ge=0.0, le=100000000.0, multiple_of=0.01) class NumberThreeFormat(BaseModel): __root__: confloat(ge=0.0, le=100000.0, multiple_of=0.001) class Service(BaseModel): callback_url: Optional[constr(max_length=256)] = None class Warnings(BaseModel): callback_url: str = None class PayingAgent(BaseModel): operation: Optional[str] = None phones: Optional[List[PhoneNumber]] = None class SupplierInfo(BaseModel): phones: Optional[List[PhoneNumber]] = None name: Optional[str] = None inn: Optional[str] = None class ReceivePaymentsOperator(BaseModel): phones: Optional[List[PhoneNumber]] = None class MoneyTransferOperator(BaseModel): phones: Optional[List[PhoneNumber]] = None name: Optional[str] = None address: Optional[str] = None inn: Optional[constr(regex=r'(^[0-9]{10}$)|(^[0-9]{12}$)')] = None class Company(BaseModel): email: Optional[constr(max_length=64)] = None sno: Optional[str] = None inn: constr(max_length=12) payment_address: constr(max_length=256) class ClientItem(BaseModel): email: constr(max_length=64) = None phone: Optional[constr(max_length=64)] = None class AdditionalUserProps(BaseModel): name: constr(max_length=64) value: constr(max_length=256) class AgentInfo(BaseModel): type: Optional[str] = None paying_agent: Optional[PayingAgent] = None receive_payments_operator: Optional[ReceivePaymentsOperator] = None money_transfer_operator: Optional[MoneyTransferOperator] = None class Error(BaseModel): error_id: Optional[str] = None code: int text: str type: Optional[str] = None class Payload(BaseModel): fiscal_receipt_number: int shift_number: int receipt_datetime: str total: float fn_number: str ecr_registration_number: str fiscal_document_number: int fiscal_document_attribute: int fns_site: str class Vat(BaseModel): type: Optional[str] = None sum: Optional[SumNumberTwoFormat] = None class CorrectionInfo(BaseModel): type: str base_date: str base_number: str class Payment(BaseModel): type: int sum: SumNumberTwoFormat class Item(BaseModel): name: str price: NumberPrice quantity: NumberThreeFormat sum: SumNumberTwoFormat measurement_unit: Optional[constr(max_length=16)] = None payment_method: Optional[str] = None payment_object: Optional[str] = None nomenclature_code: Optional[str] = None vat: Optional[Vat] = None agent_info: Optional[AgentInfo] = None supplier_info: Optional[SupplierInfo] = None user_data: Optional[constr(max_length=64)] = None excise: Optional[confloat(ge=0.0)] = None country_code: Optional[constr( regex=r'^[0-9]*$', min_length=1, max_length=3)] = None declaration_number: Optional[constr(min_length=1, max_length=32)] = None class Receipt(BaseModel): client: ClientItem company: Company agent_info: Optional[AgentInfo] = None supplier_info: Optional[SupplierInfo] = None items: List[Item] = Field(..., min_items=1) payments: List[Payment] = Field(..., max_items=10, min_items=1) vats: Optional[List[Vat]] = Field(None, max_items=6, min_items=1) total: NumberTwoFormat additional_check_props: Optional[constr(max_length=16)] = None cashier: Optional[constr(max_length=64)] = None additional_user_props: Optional[AdditionalUserProps] = None class Correction(BaseModel): company: Company cashier: Optional[constr(max_length=64)] = None correction_info: CorrectionInfo payments: List[int] = Field(..., max_items=10, min_items=1) vats: List[Vat] = Field(..., max_items=6, min_items=1) class DocModel(BaseModel): uuid: str timestamp: str group_code: str daemon_code: str device_code: str external_id: Optional[str] = None callback_url: Optional[str] = None status: Optional[str] = None error: Error = None warnings: Optional[Warnings] = None payload: Payload = None class SellModel(BaseModel): external_id: constr(max_length=128) receipt: Receipt service: Optional[Service] = None timestamp: str class CorrectionModel(BaseModel): timestamp: str external_id: constr(max_length=128) service: Optional[Service] = None correction: Correction class SellBaseModel(BaseModel): def parse(self, db_dict_list): sell_list = [] for db_dict in db_dict_list: receipt = db_dict.get('reciept') sell = db_dict.get('sell') vats = db_dict.get('vats') payments = db_dict.get('payments') items = db_dict.get('items') company = db_dict.get('company') client = db_dict.get('client') payments_model = [Payment(**payment) for payment in payments] items_model = [Item(**item) for item in items] #vats_model = [Vat(**vat) for vat in vats] receipt['client'] = ClientItem( **client if client.get('phone') != None else {'email': 'test@test.ru'}) #receipt['vats'] = vats_model if len(vats_model) > 0 else None receipt['items'] = items_model receipt['vats'] = None receipt['payments'] = payments_model receipt['company'] = Company(**company) receipt_model = Receipt(**receipt) sell['receipt'] = receipt_model sell['timestamp'] = sell['timestamp'].strftime( '%d.%m.%Y %H:%M:%S') sellmodel = SellModel(**sell) sell_list.append(sellmodel) return sell_list