From 5ef6aaeb6f2c067d08ad32f4aa93ef19fa32ca6a Mon Sep 17 00:00:00 2001 From: pi3c Date: Tue, 13 Feb 2024 13:09:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B0=D0=BB=D0=B5=D0=BD=D1=8C=D0=BA?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/.~lock.Menu.xlsx# | 1 - admin/Menu.xlsx | Bin 10003 -> 10004 bytes bg_tasks/updater.py | 43 ++++++++++++++++++---------------------- example.env | 14 ------------- 4 files changed, 19 insertions(+), 39 deletions(-) delete mode 100644 admin/.~lock.Menu.xlsx# delete mode 100644 example.env diff --git a/admin/.~lock.Menu.xlsx# b/admin/.~lock.Menu.xlsx# deleted file mode 100644 index 9e281c7..0000000 --- a/admin/.~lock.Menu.xlsx# +++ /dev/null @@ -1 +0,0 @@ -,pi3c,pi3code,13.02.2024 12:37,file:///home/pi3c/.config/libreoffice/4; diff --git a/admin/Menu.xlsx b/admin/Menu.xlsx index b0969a4565cbf402c56741dee7a240c6a3de6bb3..8345362ef8b11cf0bc59c1071602d0ff99540a50 100644 GIT binary patch delta 962 zcmbR2H^q-9z?+#xgn@&DgTd2pBabR0lc(QiV@5Y75M9k2$PA*-u}Of%U$D2b0Qr;i zc~t9T{SPaM)P4^)*|U90UR}$U*hOjndWKeuKk`0Y=gh6@aQ*Fl@7-(j3iH3uE4G+k ztuQ|~R5tg3)y)@76IEOVGwyM$T+Oss_xVTJt~u#Z8+Kg`SRiX*qP@N-Pa|K)=Jv6M zuR<{*JzEyv5y^PFccFy!KDMJVLLycZRv*)P=A5maQ!nv9QS^b1Z~g|R^DLivzPD|Z zl)ZQ`=)=WJTeivZupAF)W_l~D#(A(lia%^;H#^%imz7KIOYYFHowT^>^%IRZyYybF zHJjX-|DgZq<<}dsbV}t#*+ZT+1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UB6mus2jRa7aVknnVQ<0 z*ptB0ai>1E;e}7kyT^M25`TW&w;_{X@1sVhqxDgxfTs1C*~-CduQRJY*{bkb<0n;Ttal zLrQ*fKv8}{v3_DffnG&!PJlNflL!MW1NlZgyvEQl9jNOg5DTN|)}NfHrX&xt;gR)r s{uH1GuCSu3VV}GRs^*KF|JiOK28Nh7j0_Se=B%0gSWTO)Rs|#o01za6H~;_u diff --git a/bg_tasks/updater.py b/bg_tasks/updater.py index 00fe632..cbad0d3 100644 --- a/bg_tasks/updater.py +++ b/bg_tasks/updater.py @@ -43,9 +43,8 @@ async def is_changed_xls() -> bool: return True -async def on_menu_change( - new_menu: dict, old_menu: dict, session: AsyncSession -) -> dict | None: +async def on_menu_change(new_menu: dict, old_menu: dict, session: AsyncSession) -> dict: + """Изменение, удаление или создание меню""" if new_menu and not old_menu: # Создаем меню menu = Menu( @@ -55,6 +54,7 @@ async def on_menu_change( session.add(menu) await session.flush() new_menu['id'] = str(menu.id) + elif new_menu and old_menu: # Обновляем меню await session.execute( @@ -67,10 +67,6 @@ async def on_menu_change( await session.execute(delete(Menu).where(Menu.id == old_menu['id'])) await session.commit() - # Чистим кэш - await clear_cache('MENUS*') - await clear_cache('summary') - return new_menu @@ -110,7 +106,7 @@ async def on_submenu_change( new_sub: dict, old_sub: dict, session: AsyncSession ) -> dict: if new_sub and not old_sub: - # Создаем меню + # Создаем подменю submenu = SubMenu( title=new_sub['data']['title'], description=new_sub['data']['description'], @@ -121,8 +117,9 @@ async def on_submenu_change( await session.flush() new_sub['id'] = str(submenu.id) new_sub['parent_menu'] = str(submenu.parent_menu) + elif new_sub and old_sub: - # Обновляем меню + # Обновляем подменю await session.execute( update(SubMenu) .where(SubMenu.id == old_sub['id']) @@ -132,12 +129,9 @@ async def on_submenu_change( new_sub['parent_menu'] = old_sub['parent_menu'] else: - # Удаляем меню + # Удаляем подменю await session.execute(delete(SubMenu).where(SubMenu.id == old_sub['id'])) - await clear_cache('MENUS*') - await clear_cache('summary') - await session.commit() return new_sub @@ -146,7 +140,7 @@ async def submenus_updater(submenus: dict, session: AsyncSession) -> None: """Проверяет пункты подменю на изменения При необходимости запускае обновление БД """ - # Получаем Меню из кэша для получения их ID по померу в таблице + # Получаем меню из кэша для получения их ID по померу в таблице cached_menus = await redis.get('ALL_MENUS') if cached_menus is not None: cached_menus = pickle.loads(cached_menus) @@ -204,7 +198,7 @@ async def on_dish_change(new_dish: dict, old_dish, session: AsyncSession) -> dic new_dish['parent_submenu'] = str(dish.parent_submenu) new_dish['data']['price'] = str(dish.price) elif new_dish and old_dish: - # Обновляем меню + # Обновляем блюдо await session.execute( update(Dish).where(Dish.id == old_dish['id']).values(**(new_dish['data'])) ) @@ -213,12 +207,9 @@ async def on_dish_change(new_dish: dict, old_dish, session: AsyncSession) -> dic new_dish['data']['price'] = old_dish['data']['price'] else: - # Удаляем меню + # Удаляем блюдо await session.execute(delete(Dish).where(Dish.id == old_dish['id'])) - await clear_cache('MENUS*') - await clear_cache('summary') - await session.commit() return new_dish @@ -233,7 +224,7 @@ async def dishes_updater(dishes: dict, session: AsyncSession) -> None: else: cached_submenus = {} - # Получаем подмен из кэша + # Получаем блюда из кэша cached_dishes = await redis.get('ALL_DISHES') if cached_dishes is not None: @@ -244,7 +235,7 @@ async def dishes_updater(dishes: dict, session: AsyncSession) -> None: await clear_cache('DISCONT*') for key in {k: cached_dishes[k] for k in set(cached_dishes) - set(dishes)}: - # Проверяем на удаленные меню + # Проверяем на удаленные блюда и обновляемся await on_dish_change({}, cached_dishes.pop(key), session) for key in dishes.keys(): @@ -252,13 +243,13 @@ async def dishes_updater(dishes: dict, session: AsyncSession) -> None: dishes[key]['parent_submenu'] = parent if key not in cached_dishes.keys(): - # Получаем и ставим UUID parent_menu + # Получаем и ставим UUID parent_submenu dishes[key]['parent_submenu'] = parent dish = await on_dish_change(dishes[key], {}, session) dishes[key] = dish elif key in cached_dishes.keys(): - # Обновление меню + # Обновление блюда if dishes[key].get('data') != cached_dishes[key].get('data'): dish = await on_dish_change(dishes[key], cached_dishes[key], session) dishes[key] = dish @@ -274,12 +265,15 @@ async def dishes_updater(dishes: dict, session: AsyncSession) -> None: await redis.set('ALL_DISHES', pickle.dumps(dishes)) -async def updater(rows): +async def updater(rows) -> None: menus, submenus, dishes = await rows_to_dict(rows) async with async_session_maker() as session: await menus_updater(menus, session) await submenus_updater(submenus, session) await dishes_updater(dishes, session) + # Чистим кэш + await clear_cache('MENUS*') + await clear_cache('summary') async def main() -> None: @@ -291,5 +285,6 @@ async def main() -> None: async def main_gsheets() -> None: + """Главная функция фоновой задачи для работы с Google""" rows = await gsheets_to_rows() await updater(rows) diff --git a/example.env b/example.env deleted file mode 100644 index 51d748a..0000000 --- a/example.env +++ /dev/null @@ -1,14 +0,0 @@ -# PosgreSQL адрес сервера -POSTGRES_HOST=127.0.0.1 -POSTGRES_PORT=5432 -# Пользователь БД Postgres -POSTGRES_USER=testuser -POSTGRES_PASSWORD=test -# БД рабочая и тестовая -POSTGRES_DB=fastfood_db -POSTGRES_DB_TEST=fastfood_db_test - -# Redis -REDIS_HOST=127.0.0.1 -REDIS_PORT=6379 -REDIS_DB=0