Prebuild release 1.0 #2

Merged
IgorVolochay merged 48 commits from frontend into prebuild 2026-08-13 09:04:08 +00:00
2 changed files with 26 additions and 14 deletions
Showing only changes of commit 839476b9dc - Show all commits
+21 -7
View File
@@ -15,9 +15,11 @@ class MongoWorker:
self.client = pymongo.MongoClient(host = os.getenv('MONGO_HOST'),
port = int(os.getenv('MONGO_PORT')),
username = os.getenv('MONGO_USER'),
password = os.getenv('MONGO_PASS'))
password = os.getenv('MONGO_PASS'),
uuidRepresentation="standard")
self.db = self.client["data"]
self.users_data = self.db["users_data"]
self.counters = self.db["counters"]
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}))
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:
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,
author_id=author_id,
creation_date=datetime.now().isoformat())
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
except Exception as exception:
print(exception)
return new_card
def get_card(self, card_id: ObjectId) -> Optional[Card]:
document = self.game_data.find_one({"_id": card_id})
def get_card(self, card_id: int) -> Optional[Card]:
document = self.game_data.find_one({"card_id": card_id})
if document:
return Card.model_validate(document)
else:
@@ -67,7 +80,7 @@ class MongoWorker:
{"$sample": {"size": amount}}]
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]
return validated_items
else:
@@ -79,4 +92,5 @@ if __name__ == "__main__":
#print(mongo.add_user(123, "VolochayIgor", "Igor", "Volochay", "photo_0.jpg"))
#print(mongo.get_user(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"))
+5 -7
View File
@@ -1,5 +1,6 @@
from pydantic import BaseModel, Field
from bson.objectid import ObjectId
import uuid
IgorVolochay commented 2026-08-13 08:57:21 +00:00 (Migrated from github.com)
Review

commet -> comment

commet -> comment
from pydantic import BaseModel
class User(BaseModel):
user_id: int
@@ -17,7 +18,7 @@ class User(BaseModel):
registration_date: str
class Card(BaseModel):
card_id: ObjectId = Field(alias="_id", default=ObjectId())
card_id: int
choice_A: str
choice_B: str
@@ -33,7 +34,4 @@ class Card(BaseModel):
author_id: int
creation_date: str
moderation_date: str = "Not moderated"
active_status: bool = False
class Config:
arbitrary_types_allowed = True
active_status: bool = False