Spaces:
Sleeping
Sleeping
| from datetime import datetime | |
| from typing import Optional | |
| from bson import ObjectId | |
| class User: | |
| """User model for MongoDB.""" | |
| collection_name = "users" | |
| def __init__( | |
| self, | |
| email: str, | |
| hashed_password: str, | |
| created_at: Optional[datetime] = None, | |
| _id: Optional[ObjectId] = None | |
| ): | |
| self._id = _id or ObjectId() | |
| self.email = email | |
| self.hashed_password = hashed_password | |
| self.created_at = created_at or datetime.utcnow() | |
| def to_dict(self) -> dict: | |
| """Convert user to dictionary for MongoDB insertion.""" | |
| return { | |
| "_id": self._id, | |
| "email": self.email, | |
| "hashed_password": self.hashed_password, | |
| "created_at": self.created_at | |
| } | |
| def from_dict(cls, data: dict) -> "User": | |
| """Create User instance from MongoDB document.""" | |
| return cls( | |
| _id=data.get("_id"), | |
| email=data.get("email"), | |
| hashed_password=data.get("hashed_password"), | |
| created_at=data.get("created_at") | |
| ) | |
| def id(self) -> str: | |
| """Get string representation of user ID.""" | |
| return str(self._id) | |