Commit 4f384e59 authored by Yury's avatar Yury

document: change collection to property

parent 40092d3c
Pipeline #328 passed with stages
in 4 minutes and 51 seconds
......@@ -94,7 +94,7 @@ class DocumentRelations:
elif field == second_document_field:
related_to_field.append(
DocumentField(first_document, relation["first_document_field_name"],
not relation["first_is_parent"]))
not relation["first_is_parent"]))
return related_to_field
def search_by_document(self, document: "Document"):
......@@ -124,7 +124,8 @@ class MetaDocument(type):
client: AsyncIOMotorClient
collection: AsyncIOMotorCollection
def _collection(cls) -> AsyncIOMotorCollection:
@property
def collection(cls) -> AsyncIOMotorCollection:
for field in ("client", "database", "collection"):
if field not in cls.__dict__:
raise type("{}NotFound".format(field.capitalize()), (DocumentException,), {})(
......@@ -140,8 +141,6 @@ class MetaDocument(type):
def __getattribute__(cls, item: str):
if item in ("database", "client"):
raise AttributeError(item)
elif item == "collection":
return super().__getattribute__("_collection")
return super().__getattribute__(item)
@property
......@@ -213,7 +212,7 @@ class Document(metaclass=MetaDocument):
for self_field, other_field in related_fields:
if self_field.parent:
await other_field.document_cls._collection().update_many({
await other_field.document_cls.collection.update_many({
other_field.name: self.shadow_copy[self_field.name]
}, {"$set": {other_field.name: self[self_field.name]}})
......@@ -222,7 +221,7 @@ class Document(metaclass=MetaDocument):
"""Force update document."""
await self._update_related()
return await self.__class__._collection().replace_one(
return await self.__class__.collection.replace_one(
{"_id": self.shadow_copy._id}, self._document_)
@classmethod
......@@ -230,7 +229,7 @@ class Document(metaclass=MetaDocument):
async def exists(cls, *args, **kwargs):
"""Checks that document exists."""
if await cls._collection().find_one(*args, **kwargs):
if await cls.collection.find_one(*args, **kwargs):
return True
return False
......@@ -239,7 +238,7 @@ class Document(metaclass=MetaDocument):
async def one(cls, **kwargs):
"""Finds one document based on kwargs."""
document = await cls._collection().find_one(kwargs)
document = await cls.collection.find_one(kwargs)
if not document:
raise DocumentDoesntExist()
return cls(**document)
......@@ -250,7 +249,7 @@ class Document(metaclass=MetaDocument):
"""Finds multiple documents based on kwargs."""
result_list = []
cursor = cls._collection().find(kwargs)
cursor = cls.collection.find(kwargs)
async for doc in cursor:
result_list.append(cls(**doc))
return result_list
......@@ -260,7 +259,7 @@ class Document(metaclass=MetaDocument):
async def create(cls, **kwargs) -> "Document":
"""Create new document."""
await cls._collection().insert_one(kwargs)
await cls.collection.insert_one(kwargs)
return cls(**kwargs)
async def _delete_by_prop(self, relations):
......@@ -281,14 +280,14 @@ class Document(metaclass=MetaDocument):
for self_field, other_field in related_fields:
if self_field.parent:
await other_field.document_cls._collection().delete_many({
await other_field.document_cls.collection.delete_many({
other_field.name: self[self_field.name]
})
async def delete(self):
"""Delete current document and related fields or documents base on related."""
result = await self.__class__._collection().delete_one({"_id": self._id})
result = await self.__class__.collection.delete_one({"_id": self._id})
await self._delete_related()
return result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment