From 8189aaedd47b97a6055a00aa9a5a6649300893e4 Mon Sep 17 00:00:00 2001 From: pi3c Date: Wed, 14 Feb 2024 15:34:24 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20TypeHints=20=D0=B8=20=D1=83=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=20=D0=BD=D0=B5=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=D1=8B=D0=B5=20=D1=81=D1=83=D1=89=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bg_tasks/parser.py | 5 ++++- bg_tasks/updater.py | 11 ++++++++--- compose_test.yml | 2 -- creds.json | 13 ------------- fastfood/repository/dish.py | 9 +-------- fastfood/repository/submenu.py | 6 ++---- fastfood/repository/summary.py | 4 +++- fastfood/routers/dish.py | 9 ++------- fastfood/routers/menu.py | 7 +------ fastfood/routers/submenu.py | 7 +------ fastfood/routers/summary.py | 3 +-- fastfood/service/dish.py | 13 ++++--------- fastfood/service/submenu.py | 10 ++++------ fastfood/service/summary.py | 2 +- 14 files changed, 32 insertions(+), 69 deletions(-) delete mode 100644 creds.json diff --git a/bg_tasks/parser.py b/bg_tasks/parser.py index 29d616f..a64410d 100644 --- a/bg_tasks/parser.py +++ b/bg_tasks/parser.py @@ -1,4 +1,5 @@ import os +from typing import Any import gspread import openpyxl @@ -43,7 +44,9 @@ async def local_xlsx_to_rows() -> list[list[str | int | float]]: return data -async def rows_to_dict(rows: list[list]) -> tuple: +async def rows_to_dict( + rows: list[list], +) -> tuple[dict[int, Any], dict[Any, Any], dict[Any, Any]]: """Парсит строки полученные и источников в словарь""" menus = {} diff --git a/bg_tasks/updater.py b/bg_tasks/updater.py index cbad0d3..bd84d6a 100644 --- a/bg_tasks/updater.py +++ b/bg_tasks/updater.py @@ -1,5 +1,6 @@ import os import pickle +from typing import Any import redis.asyncio as redis # type: ignore from sqlalchemy import delete, update @@ -43,7 +44,9 @@ async def is_changed_xls() -> bool: return True -async def on_menu_change(new_menu: dict, old_menu: dict, session: AsyncSession) -> dict: +async def on_menu_change( + new_menu: dict, old_menu: dict, session: AsyncSession +) -> dict[str, Any]: """Изменение, удаление или создание меню""" if new_menu and not old_menu: # Создаем меню @@ -104,7 +107,7 @@ async def menus_updater(menus: dict, session: AsyncSession) -> None: async def on_submenu_change( new_sub: dict, old_sub: dict, session: AsyncSession -) -> dict: +) -> dict[str, Any]: if new_sub and not old_sub: # Создаем подменю submenu = SubMenu( @@ -183,7 +186,9 @@ async def submenus_updater(submenus: dict, session: AsyncSession) -> None: await redis.set('ALL_SUBMENUS', pickle.dumps(submenus)) -async def on_dish_change(new_dish: dict, old_dish, session: AsyncSession) -> dict: +async def on_dish_change( + new_dish: dict, old_dish, session: AsyncSession +) -> dict[str, Any]: if new_dish and not old_dish: dish = Dish( title=new_dish['data']['title'], diff --git a/compose_test.yml b/compose_test.yml index 60155c5..b8056ca 100644 --- a/compose_test.yml +++ b/compose_test.yml @@ -55,8 +55,6 @@ services: redis: condition: service_healthy - restart: always - volumes: - .:/usr/src/fastfood diff --git a/creds.json b/creds.json deleted file mode 100644 index ad13043..0000000 --- a/creds.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "type": "service_account", - "project_id": "psyched-ceiling-413920", - "private_key_id": "d19492eea6a030092cf8ad767b62d7909734ccb1", - "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDPP2UrfDE+UOlD\nA761Jemy1IKKdIanRKXSziGvDYJ7zbY5gPFxY8Vo+9fsh1oumvROXqEH4+1LiR0J\nnUiy33R1KDOHETeP4H/FJ3u8+gHoL8wsA7SN5pIX9AR8AyBZCBjg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+14PgjU1MQYC52MEPuv6sbvItX1Oxq/FnRhAoGAHWYK\ncbBSvJzuKtY+CC3gPa0i5cfq07VIVU8Pm7OosM7Q0CR/y88ntgVsscC0qJFwr/EU\ny7aJyBY9TInYqDPzMTeJVXsUwQ5gJut4ngFWk6kitDsJwFqqNFKmeLOj4repY5ee\n79U6kEHJzkOE8VgsH5nW4sjzDEQ9hmhOJ3tFz0ECgYEA8N+7yq1tK/99S8ThYW1J\n9mvUXRhAcFamBYp+8bIBdnQlrM9bGd9j8gYzQj+RBcvfCpVHFM20z8CC8oN0bitk\nh5MEjLBkw1vaywFlA/hcnA8A3g+5/IgHl03Y1tPWnyAtB77vE2M2ThklZ5l4E8eT\nP1vYw9RUSAPjtd43XDTqPNQ=\n-----END PRIVATE KEY-----\n", - "client_email": "tester@psyched-ceiling-413920.iam.gserviceaccount.com", - "client_id": "100697987276606879445", - "auth_uri": "https://accounts.google.com/o/oauth2/auth", - "token_uri": "https://oauth2.googleapis.com/token", - "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", - "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/tester%40psyched-ceiling-413920.iam.gserviceaccount.com", - "universe_domain": "googleapis.com" -} diff --git a/fastfood/repository/dish.py b/fastfood/repository/dish.py index 37e5c29..96f3f9d 100644 --- a/fastfood/repository/dish.py +++ b/fastfood/repository/dish.py @@ -13,7 +13,7 @@ class DishRepository: def __init__(self, session: AsyncSession = Depends(get_async_session)) -> None: self.db = session - async def get_dishes(self, menu_id: UUID, submenu_id: UUID) -> list[Dish]: + async def get_dishes(self, submenu_id: UUID) -> list[Dish]: query = select(Dish).where( Dish.parent_submenu == submenu_id, ) @@ -22,7 +22,6 @@ class DishRepository: async def create_dish_item( self, - menu_id: UUID, submenu_id: UUID, dish_data: Dish_db, ) -> Dish: @@ -35,8 +34,6 @@ class DishRepository: async def get_dish_item( self, - menu_id: UUID, - submenu_id: UUID, dish_id: UUID, ) -> Dish | None: query = select(Dish).where(Dish.id == dish_id) @@ -45,8 +42,6 @@ class DishRepository: async def update_dish_item( self, - menu_id: UUID, - submenu_id: UUID, dish_id: UUID, dish_data: Dish_db, ) -> Dish | None: @@ -59,8 +54,6 @@ class DishRepository: async def delete_dish_item( self, - menu_id: UUID, - submenu_id: UUID, dish_id: UUID, ) -> None: query = delete(Dish).where(Dish.id == dish_id) diff --git a/fastfood/repository/submenu.py b/fastfood/repository/submenu.py index 28695fc..236a19c 100644 --- a/fastfood/repository/submenu.py +++ b/fastfood/repository/submenu.py @@ -32,14 +32,13 @@ class SubMenuRepository: await self.db.commit() await self.db.refresh(new_submenu) - full_sub = await self.get_submenu_item(menu_id, new_submenu.id) + full_sub = await self.get_submenu_item(new_submenu.id) if full_sub is None: raise TypeError return full_sub async def get_submenu_item( self, - menu_id: UUID, submenu_id: UUID, ) -> SubMenu | None: s = aliased(SubMenu) @@ -56,7 +55,6 @@ class SubMenuRepository: async def update_submenu_item( self, - menu_id: UUID, submenu_id: UUID, submenu_data: MenuBase, ) -> SubMenu | None: @@ -71,7 +69,7 @@ class SubMenuRepository: updated_submenu = await self.db.execute(qr) return updated_submenu.scalar_one_or_none() - async def delete_submenu_item(self, menu_id: UUID, submenu_id: UUID) -> None: + async def delete_submenu_item(self, submenu_id: UUID) -> None: query = delete(SubMenu).where( SubMenu.id == submenu_id, ) diff --git a/fastfood/repository/summary.py b/fastfood/repository/summary.py index a2fef0a..cc8c0b9 100644 --- a/fastfood/repository/summary.py +++ b/fastfood/repository/summary.py @@ -1,3 +1,5 @@ +from typing import Any + from fastapi import Depends from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession @@ -11,7 +13,7 @@ class SummaryRepository: def __init__(self, session: AsyncSession = Depends(get_async_session)) -> None: self.db = session - async def get_data(self): + async def get_data(self) -> list[Any]: query = select(Menu).options( selectinload(Menu.submenus).selectinload(SubMenu.dishes) ) diff --git a/fastfood/routers/dish.py b/fastfood/routers/dish.py index e8d0594..ae62503 100644 --- a/fastfood/routers/dish.py +++ b/fastfood/routers/dish.py @@ -1,6 +1,6 @@ from uuid import UUID -from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException from fastfood.schemas import Dish, DishBase from fastfood.service.dish import DishService @@ -19,7 +19,6 @@ async def get_dishes( menu_id: UUID, submenu_id: UUID, dish: DishService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> list[Dish]: result = await dish.read_dishes(menu_id, submenu_id) return result @@ -35,7 +34,6 @@ async def create_dish( submenu_id: UUID, dish_data: DishBase, dish: DishService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> Dish: return await dish.create_dish( menu_id, @@ -53,7 +51,6 @@ async def get_dish( submenu_id: UUID, dish_id: UUID, dish: DishService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> Dish | None: result = await dish.read_dish( menu_id, @@ -78,7 +75,6 @@ async def update_dish( dish_id: UUID, dish_data: DishBase, dish: DishService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> Dish: result = await dish.update_dish( menu_id, @@ -102,6 +98,5 @@ async def delete_dish( submenu_id: UUID, dish_id: UUID, dish: DishService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> None: - await dish.del_dish(menu_id, submenu_id, dish_id) + await dish.del_dish(menu_id, dish_id) diff --git a/fastfood/routers/menu.py b/fastfood/routers/menu.py index 963f2a0..d6ba69c 100644 --- a/fastfood/routers/menu.py +++ b/fastfood/routers/menu.py @@ -1,6 +1,6 @@ from uuid import UUID -from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException from fastfood.schemas import MenuBase, MenuRead from fastfood.service.menu import MenuService @@ -18,7 +18,6 @@ router = APIRouter( ) async def get_menus( menu: MenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> list[MenuRead]: return await menu.read_menus() @@ -31,7 +30,6 @@ async def get_menus( async def add_menu( menu: MenuBase, responce: MenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> MenuRead: return await responce.create_menu(menu) @@ -43,7 +41,6 @@ async def add_menu( async def get_menu( menu_id: UUID, responce: MenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> MenuRead: result = await responce.read_menu(menu_id=menu_id) @@ -63,7 +60,6 @@ async def update_menu( menu_id: UUID, menu: MenuBase, responce: MenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> MenuRead: result = await responce.update_menu( menu_id=menu_id, @@ -85,6 +81,5 @@ async def update_menu( async def delete_menu( menu_id: UUID, menu: MenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> None: await menu.del_menu(menu_id) diff --git a/fastfood/routers/submenu.py b/fastfood/routers/submenu.py index b52e523..8b0ba65 100644 --- a/fastfood/routers/submenu.py +++ b/fastfood/routers/submenu.py @@ -1,6 +1,6 @@ from uuid import UUID -from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException from fastfood.schemas import MenuBase, SubMenuRead from fastfood.service.submenu import SubmenuService @@ -18,7 +18,6 @@ router = APIRouter( async def get_submenus( menu_id: UUID, submenu: SubmenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> list[SubMenuRead]: result = await submenu.read_submenus(menu_id=menu_id) return result @@ -33,7 +32,6 @@ async def create_submenu_item( menu_id: UUID, submenu_data: MenuBase, submenu: SubmenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> SubMenuRead: result = await submenu.create_submenu( menu_id=menu_id, @@ -50,7 +48,6 @@ async def get_submenu( menu_id: UUID, submenu_id: UUID, submenu: SubmenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> SubMenuRead: result = await submenu.read_menu( menu_id=menu_id, @@ -73,7 +70,6 @@ async def update_submenu( submenu_id: UUID, submenu_data: MenuBase, submenu: SubmenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> SubMenuRead: result = await submenu.update_submenu( menu_id=menu_id, @@ -96,6 +92,5 @@ async def delete_submenu( menu_id: UUID, submenu_id: UUID, submenu: SubmenuService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> None: await submenu.del_menu(menu_id=menu_id, submenu_id=submenu_id) diff --git a/fastfood/routers/summary.py b/fastfood/routers/summary.py index ee36330..95d1337 100644 --- a/fastfood/routers/summary.py +++ b/fastfood/routers/summary.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, BackgroundTasks, Depends +from fastapi import APIRouter, Depends from fastfood.schemas import MenuSummary from fastfood.service.summary import SummaryService @@ -12,6 +12,5 @@ router = APIRouter( @router.get('/', response_model=list[MenuSummary]) async def get_summary( sum: SummaryService = Depends(), - background_tasks: BackgroundTasks = BackgroundTasks(), ) -> list[MenuSummary]: return await sum.read_data() diff --git a/fastfood/service/dish.py b/fastfood/service/dish.py index 981cf09..b4bb8e9 100644 --- a/fastfood/service/dish.py +++ b/fastfood/service/dish.py @@ -42,7 +42,7 @@ class DishService: if cached_dishes is not None: return cached_dishes - data = await self.dish_repo.get_dishes(menu_id, submenu_id) + data = await self.dish_repo.get_dishes(submenu_id) response = [] for row in data: dish = await self._convert_dish_to_dict(row) @@ -67,7 +67,6 @@ class DishService: ) -> Dish: dish_db = Dish_db(**dish_data.model_dump()) data = await self.dish_repo.create_dish_item( - menu_id, submenu_id, dish_db, ) @@ -95,7 +94,7 @@ class DishService: if cached_dish is not None: return cached_dish - data = await self.dish_repo.get_dish_item(menu_id, submenu_id, dish_id) + data = await self.dish_repo.get_dish_item(dish_id) if data is None: return None dish = await self._convert_dish_to_dict(data) @@ -116,9 +115,7 @@ class DishService: self, menu_id: UUID, submenu_id: UUID, dish_id, dish_data: DishBase ) -> Dish | None: dish_db = Dish_db(**dish_data.model_dump()) - data = await self.dish_repo.update_dish_item( - menu_id, submenu_id, dish_id, dish_db - ) + data = await self.dish_repo.update_dish_item(dish_id, dish_db) if data is None: return None @@ -139,10 +136,8 @@ class DishService: return dish - async def del_dish(self, menu_id: UUID, submenu_id: UUID, dish_id: UUID) -> None: + async def del_dish(self, menu_id: UUID, dish_id: UUID) -> None: await self.dish_repo.delete_dish_item( - menu_id, - submenu_id, dish_id, ) await self.cache.delete(key=str(menu_id), bg_task=self.bg_tasks) diff --git a/fastfood/service/submenu.py b/fastfood/service/submenu.py index d020434..ea7aebb 100644 --- a/fastfood/service/submenu.py +++ b/fastfood/service/submenu.py @@ -33,7 +33,7 @@ class SubmenuService: submenus = [] for r in data: submenu = r.__dict__ - subq = await self.submenu_repo.get_submenu_item(menu_id, r.id) + subq = await self.submenu_repo.get_submenu_item(r.id) if subq is not None: submenu['dishes_count'] = len(subq.dishes) submenu = SubMenuRead(**submenu) @@ -73,7 +73,7 @@ class SubmenuService: if cached_submenu is not None: return cached_submenu - data = await self.submenu_repo.get_submenu_item(menu_id, submenu_id) + data = await self.submenu_repo.get_submenu_item(submenu_id) if data is None: return None submenu = data.__dict__ @@ -90,9 +90,7 @@ class SubmenuService: async def update_submenu( self, menu_id: UUID, submenu_id: UUID, submenu_data: MenuBase ) -> SubMenuRead | None: - data = await self.submenu_repo.update_submenu_item( - menu_id, submenu_id, submenu_data - ) + data = await self.submenu_repo.update_submenu_item(submenu_id, submenu_data) if data is None: return None @@ -111,7 +109,7 @@ class SubmenuService: return submenu async def del_menu(self, menu_id: UUID, submenu_id: UUID) -> None: - await self.submenu_repo.delete_submenu_item(menu_id, submenu_id) + await self.submenu_repo.delete_submenu_item(submenu_id) await self.cache.delete( key=self.key( 'submenu', diff --git a/fastfood/service/summary.py b/fastfood/service/summary.py index b5cddef..32a6969 100644 --- a/fastfood/service/summary.py +++ b/fastfood/service/summary.py @@ -19,7 +19,7 @@ class SummaryService: self.key = get_key self.bg_tasks = background_tasks - async def read_data(self): + async def read_data(self) -> list[MenuSummary]: result = []