From 5f8a1bebf7946daa6df418996f00352052b9cc5064bb87cb8e633c7eeb809910 Mon Sep 17 00:00:00 2001 From: vladislav Date: Mon, 14 Sep 2026 08:12:52 -0400 Subject: [PATCH] feat --- delivery_router.py | 2 +- main.ipynb | 45 ++++++++++++++++++------- main.py | 43 ++++++++++++++++------- templates/profile/delivery.html | 2 +- templates/profile/delivery_history.html | 2 +- templates/profile/delivery_old.html | 2 +- templates/profile/invitation.html | 14 ++++---- 7 files changed, 74 insertions(+), 36 deletions(-) diff --git a/delivery_router.py b/delivery_router.py index 8b7b2be..df3f449 100644 --- a/delivery_router.py +++ b/delivery_router.py @@ -125,7 +125,7 @@ async def accept_delivery( new_doc = { **existing, - "status": "Получено", + "status": "Получено галерей" if 'partner' in current_user.get('roles', []) else "Получено художником", "notify": notify.upserted_id, "received_at": datetime.utcnow(), "updated_at": datetime.utcnow(), diff --git a/main.ipynb b/main.ipynb index 978aa16..3a26c15 100644 --- a/main.ipynb +++ b/main.ipynb @@ -180,23 +180,42 @@ "metadata": {}, "outputs": [], "source": [ - "def get_delivery_aggregate(_filter: dict = {}, _status: str = None):\n", + "from typing import Any, AsyncIterator\n", + "\n", + "\n", + "def get_delivery_aggregate(\n", + " _filter: dict[str, Any] | None = None,\n", + " _status: str | list[str] | None = None,\n", + "):\n", " cursor = delivery_collection.aggregate([\n", - " {\"$match\": {\n", - " \"prod\": PROD, **_filter\n", - " }},\n", + " {\n", + " \"$match\": {\n", + " \"prod\": PROD,\n", + " **(_filter or {}),\n", + " }\n", + " },\n", " {\"$sort\": {\"_id\": -1}},\n", - " {\"$group\": {\n", - " \"_id\": \"$post_id\",\n", - " \"doc\": {\"$first\": \"$$ROOT\"}\n", - " }},\n", + " {\n", + " \"$group\": {\n", + " \"_id\": \"$post_id\",\n", + " \"doc\": {\"$first\": \"$$ROOT\"},\n", + " }\n", + " },\n", " {\"$replaceRoot\": {\"newRoot\": \"$doc\"}},\n", " {\"$sort\": {\"_id\": -1}},\n", " ])\n", - " if _status:\n", - " return (doc async for doc in cursor if doc.get('status') == _status)\n", - " else:\n", - " return cursor\n" + "\n", + " if _status is None:\n", + " return cursor\n", + "\n", + " statuses = {_status} if isinstance(_status, str) else set(_status)\n", + "\n", + " async def filtered_cursor() -> AsyncIterator[dict[str, Any]]:\n", + " async for doc in cursor:\n", + " if doc.get(\"status\") in statuses:\n", + " yield doc\n", + "\n", + " return filtered_cursor()\n" ], "display_name": ".venv" }, @@ -1689,7 +1708,7 @@ "):\n", " exhibition = await exhibition_collection.find_one({'_id': ObjectId(exhibition_id)})\n", " if exhibition:\n", - " posts = [doc async for doc in get_delivery_aggregate({\"exhibition_id\": exhibition_id}, 'На выставке')]\n", + " posts = [doc async for doc in get_delivery_aggregate({\"exhibition_id\": exhibition_id}, ['Получено', 'Получено галерей', 'Продано', 'На выставке'])]\n", " \n", " return templates.TemplateResponse(\n", " request=request, name=\"exhibition/index.html\", context={\"id\": 1, \"user\": user, \"exhibition\": exhibition, \"posts\": posts, 'now': datetime.now()}\n", diff --git a/main.py b/main.py index 9756cf0..d4a453e 100644 --- a/main.py +++ b/main.py @@ -81,24 +81,43 @@ def transliterate(text: str) -> str: return ''.join(RUS_TO_LAT.get(ch, '' if ch not in string.ascii_letters else ch) for ch in text) -def get_delivery_aggregate(_filter: dict = {}, _status: str = None): +from typing import Any, AsyncIterator + + +def get_delivery_aggregate( + _filter: dict[str, Any] | None = None, + _status: str | list[str] | None = None, +): cursor = delivery_collection.aggregate([ - {"$match": { - "prod": PROD, **_filter - }}, + { + "$match": { + "prod": PROD, + **(_filter or {}), + } + }, {"$sort": {"_id": -1}}, - {"$group": { - "_id": "$post_id", - "doc": {"$first": "$$ROOT"} - }}, + { + "$group": { + "_id": "$post_id", + "doc": {"$first": "$$ROOT"}, + } + }, {"$replaceRoot": {"newRoot": "$doc"}}, {"$sort": {"_id": -1}}, ]) - if _status: - return (doc async for doc in cursor if doc.get('status') == _status) - else: + + if _status is None: return cursor + statuses = {_status} if isinstance(_status, str) else set(_status) + + async def filtered_cursor() -> AsyncIterator[dict[str, Any]]: + async for doc in cursor: + if doc.get("status") in statuses: + yield doc + + return filtered_cursor() + async def get_latest_news() -> List[dict]: cursor = news_collection.find({'publish_on_platform': True}).sort("_id", -1).limit(30) @@ -1260,7 +1279,7 @@ async def exhibition_members( ): exhibition = await exhibition_collection.find_one({'_id': ObjectId(exhibition_id)}) if exhibition: - posts = [doc async for doc in get_delivery_aggregate({"exhibition_id": exhibition_id}, 'На выставке')] + posts = [doc async for doc in get_delivery_aggregate({"exhibition_id": exhibition_id}, ['Получено', 'Получено галерей', 'Продано', 'На выставке'])] return templates.TemplateResponse( request=request, name="exhibition/index.html", context={"id": 1, "user": user, "exhibition": exhibition, "posts": posts, 'now': datetime.now()} diff --git a/templates/profile/delivery.html b/templates/profile/delivery.html index 09750ba..90b8e83 100644 --- a/templates/profile/delivery.html +++ b/templates/profile/delivery.html @@ -17,7 +17,7 @@
{{doc.status}}
- {% elif doc.status == 'Получено' %} + {% elif Получено' in (doc.status or '') %}
{{doc.status}}
diff --git a/templates/profile/delivery_history.html b/templates/profile/delivery_history.html index 19810b9..c314832 100644 --- a/templates/profile/delivery_history.html +++ b/templates/profile/delivery_history.html @@ -33,7 +33,7 @@
Запрос на отправку
- {% elif doc.status == 'Получено' %} + {% elif Получено' in (doc.status or '') %}
{{doc.status}}
diff --git a/templates/profile/delivery_old.html b/templates/profile/delivery_old.html index 07ad6d8..79aa7e2 100644 --- a/templates/profile/delivery_old.html +++ b/templates/profile/delivery_old.html @@ -17,7 +17,7 @@
{{doc.status}}
- {% elif doc.status == 'Получено' %} + {% elif Получено' in (doc.status or '') %}
{{doc.status}}
diff --git a/templates/profile/invitation.html b/templates/profile/invitation.html index e87262f..5b712ed 100644 --- a/templates/profile/invitation.html +++ b/templates/profile/invitation.html @@ -12,21 +12,21 @@
- {% if doc.status == 'Ожидает отправки' %} + {% if (doc.status or '') == 'Ожидает отправки' %}
- {{doc.status}} + {{(doc.status or '')}}
- {% elif doc.status == 'Получено' %} + {% elif 'Получено' in (doc.status or '') %}
- {{doc.status}} + {{(doc.status or '')}}
- {% elif doc.status == 'Ожидает получения' %} + {% elif (doc.status or '') == 'Ожидает получения' %}
- {{doc.status}} + {{(doc.status or '')}}
{% else %}
- {{doc.status}} + {{(doc.status or '')}}
{% endif %} Посмотреть историю