diff --git a/main.ipynb b/main.ipynb index 3ecc070..36f4d4d 100644 --- a/main.ipynb +++ b/main.ipynb @@ -1614,7 +1614,29 @@ "):\n", " exhibition = await exhibition_collection.find_one({'_id': ObjectId(exhibition_id)})\n", " if exhibition:\n", - " posts = await delivery_collection.find({'status': 'На выставке', \"exhibition_id\": exhibition_id}).to_list()\n", + " match_ = {\n", + " \"$or\": [\n", + " {\"sender._id\": user.get('_id')},\n", + " {\"user._id\": user.get('_id')},\n", + " {\"receiver._id\": user.get('_id')}\n", + " ],\n", + " 'prod': PROD,\n", + " }\n", + " if data.get('competition'):\n", + " match_.update({'competition_id': data.get('competition').get('_id')})\n", + " if data.get('exhibition_id'):\n", + " match_.update({'exhibition_id': data.get('exhibition_id')})\n", + " cursor = delivery_collection.aggregate([\n", + " {\"$match\": match_},\n", + " {\"$sort\": {\"_id\": -1}},\n", + " {\"$group\": {\n", + " \"_id\": \"$post_id\",\n", + " \"doc\": {\"$first\": \"$$ROOT\"}\n", + " }},\n", + " {\"$replaceRoot\": {\"newRoot\": \"$doc\"}},\n", + " {\"$sort\": {\"_id\": -1}},\n", + " ])\n", + " posts = [doc async for doc in cursor if doc.get('status') == 'На выставке']\n", " return templates.TemplateResponse(\n", " request=request, name=\"exhibition/index.html\", context={\"id\": 1, \"user\": user, \"exhibition\": exhibition, \"posts\": posts}\n", " )\n", diff --git a/main.py b/main.py index 8fc16a8..e173996 100644 --- a/main.py +++ b/main.py @@ -1201,7 +1201,29 @@ async def exhibition_members( ): exhibition = await exhibition_collection.find_one({'_id': ObjectId(exhibition_id)}) if exhibition: - posts = await delivery_collection.find({'status': 'На выставке', "exhibition_id": exhibition_id}).to_list() + match_ = { + "$or": [ + {"sender._id": user.get('_id')}, + {"user._id": user.get('_id')}, + {"receiver._id": user.get('_id')} + ], + 'prod': PROD, + } + if data.get('competition'): + match_.update({'competition_id': data.get('competition').get('_id')}) + if data.get('exhibition_id'): + match_.update({'exhibition_id': data.get('exhibition_id')}) + cursor = delivery_collection.aggregate([ + {"$match": match_}, + {"$sort": {"_id": -1}}, + {"$group": { + "_id": "$post_id", + "doc": {"$first": "$$ROOT"} + }}, + {"$replaceRoot": {"newRoot": "$doc"}}, + {"$sort": {"_id": -1}}, + ]) + posts = [doc async for doc in cursor if doc.get('status') == 'На выставке'] return templates.TemplateResponse( request=request, name="exhibition/index.html", context={"id": 1, "user": user, "exhibition": exhibition, "posts": posts} )