fix: reworked cards indexing system
This commit is contained in:
+21
-7
@@ -15,9 +15,11 @@ class MongoWorker:
|
|||||||
self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'),
|
self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'),
|
||||||
port = int(os.getenv('MONGO_PORT')),
|
port = int(os.getenv('MONGO_PORT')),
|
||||||
username = os.getenv('MONGO_USER'),
|
username = os.getenv('MONGO_USER'),
|
||||||
password = os.getenv('MONGO_PASS'))
|
password = os.getenv('MONGO_PASS'),
|
||||||
|
uuidRepresentation="standard")
|
||||||
self.db = self.client["data"]
|
self.db = self.client["data"]
|
||||||
self.users_data = self.db["users_data"]
|
self.users_data = self.db["users_data"]
|
||||||
|
self.counters = self.db["counters"]
|
||||||
self.game_data = self.db["game_data"]
|
self.game_data = self.db["game_data"]
|
||||||
|
|
||||||
|
|
||||||
@@ -44,19 +46,30 @@ class MongoWorker:
|
|||||||
return User.model_validate(self.users_data.find_one({"user_id": user_id}))
|
return User.model_validate(self.users_data.find_one({"user_id": user_id}))
|
||||||
|
|
||||||
|
|
||||||
|
def get_and_update_counter(self, counter_name: str) -> int:
|
||||||
|
counter = self.counters.find_one_and_update(
|
||||||
|
{"counter_name": counter_name},
|
||||||
|
{"$inc": {"counter": 1}},
|
||||||
|
upsert=True,
|
||||||
|
return_document=True)
|
||||||
|
return counter["counter"]
|
||||||
|
|
||||||
|
|
||||||
def add_card(self, choice_A: str, choice_B: str, author_id: int) -> Card:
|
def add_card(self, choice_A: str, choice_B: str, author_id: int) -> Card:
|
||||||
new_card = Card(choice_A=choice_A,
|
new_card = Card(card_id=self.get_and_update_counter(counter_name="card"),
|
||||||
|
choice_A=choice_A,
|
||||||
choice_B=choice_B,
|
choice_B=choice_B,
|
||||||
author_id=author_id,
|
author_id=author_id,
|
||||||
creation_date=datetime.now().isoformat())
|
creation_date=datetime.now().isoformat())
|
||||||
try:
|
try:
|
||||||
self.game_data.insert_one(new_card.model_dump(by_alias=True))
|
self.game_data.insert_one(new_card.model_dump())
|
||||||
return new_card
|
return new_card
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
|
print(exception)
|
||||||
return new_card
|
return new_card
|
||||||
|
|
||||||
def get_card(self, card_id: ObjectId) -> Optional[Card]:
|
def get_card(self, card_id: int) -> Optional[Card]:
|
||||||
document = self.game_data.find_one({"_id": card_id})
|
document = self.game_data.find_one({"card_id": card_id})
|
||||||
if document:
|
if document:
|
||||||
return Card.model_validate(document)
|
return Card.model_validate(document)
|
||||||
else:
|
else:
|
||||||
@@ -67,7 +80,7 @@ class MongoWorker:
|
|||||||
{"$sample": {"size": amount}}]
|
{"$sample": {"size": amount}}]
|
||||||
raw_items = list(self.game_data.aggregate(pipeline))
|
raw_items = list(self.game_data.aggregate(pipeline))
|
||||||
|
|
||||||
if not raw_items:
|
if raw_items:
|
||||||
validated_items = [Card.model_validate(item) for item in raw_items]
|
validated_items = [Card.model_validate(item) for item in raw_items]
|
||||||
return validated_items
|
return validated_items
|
||||||
else:
|
else:
|
||||||
@@ -79,4 +92,5 @@ if __name__ == "__main__":
|
|||||||
#print(mongo.add_user(123, "VolochayIgor", "Igor", "Volochay", "photo_0.jpg"))
|
#print(mongo.add_user(123, "VolochayIgor", "Igor", "Volochay", "photo_0.jpg"))
|
||||||
#print(mongo.get_user(123))
|
#print(mongo.get_user(123))
|
||||||
print(mongo.add_card("A", "B", 123))
|
print(mongo.add_card("A", "B", 123))
|
||||||
print(mongo.get_random_cards(10, active_status=False))
|
#print(mongo.get_random_cards(10, active_status=False))
|
||||||
|
#print(mongo.update_counter("cards_counter"))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from pydantic import BaseModel, Field
|
import uuid
|
||||||
from bson.objectid import ObjectId
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
user_id: int
|
user_id: int
|
||||||
@@ -17,7 +18,7 @@ class User(BaseModel):
|
|||||||
registration_date: str
|
registration_date: str
|
||||||
|
|
||||||
class Card(BaseModel):
|
class Card(BaseModel):
|
||||||
card_id: ObjectId = Field(alias="_id", default=ObjectId())
|
card_id: int
|
||||||
|
|
||||||
choice_A: str
|
choice_A: str
|
||||||
choice_B: str
|
choice_B: str
|
||||||
@@ -33,7 +34,4 @@ class Card(BaseModel):
|
|||||||
author_id: int
|
author_id: int
|
||||||
creation_date: str
|
creation_date: str
|
||||||
moderation_date: str = "Not moderated"
|
moderation_date: str = "Not moderated"
|
||||||
active_status: bool = False
|
active_status: bool = False
|
||||||
|
|
||||||
class Config:
|
|
||||||
arbitrary_types_allowed = True
|
|
||||||
Reference in New Issue
Block a user