This commit is contained in:
2024-03-19 09:01:46 +03:00
parent aafa1050a7
commit 6ba041a839
23 changed files with 369 additions and 39 deletions

58
utils/import_services.py Normal file
View File

@@ -0,0 +1,58 @@
import asyncio
import openpyxl
from openpyxl.worksheet.worksheet import Worksheet
from sqlalchemy.ext.asyncio import AsyncSession
from backend.session import session_maker
from models import ServiceCategory, Service
async def write_services(services: dict):
async with session_maker() as session:
session: AsyncSession
for raw_category in services.keys():
category = ServiceCategory(name=raw_category)
session.add(category)
await session.flush()
print(category.id)
for raw_service in services[raw_category]:
service = Service(name=raw_service['name'],
price=raw_service['price'],
category_id=category.id)
session.add(service)
await session.flush()
await session.commit()
async def main():
workbook = openpyxl.load_workbook('services.xlsx')
sheet: Worksheet = workbook.active
START_ROW = 12
current_category = None
services = {}
merged_cells = sheet.merged_cells.ranges
for row in range(START_ROW, sheet.max_row):
first_column = sheet.cell(row, 1)
first_column_value = first_column.value.strip()
second_column = sheet.cell(row, 2)
second_column_value = second_column.value
is_category = any([second_column.coordinate in merged_cell for merged_cell in merged_cells])
if is_category:
current_category = first_column_value
services[current_category] = []
continue
price = second_column_value
name = first_column_value
services[current_category].append({
'name': name,
'price': price
})
await write_services(services)
if __name__ == '__main__':
asyncio.run(main())

BIN
utils/services.xlsx Normal file

Binary file not shown.