♻️(documents) inherit manager from queryset

During a code review, I saw we are overriding the MP_NodeManager and
redefine the queryset filters:

- The MP_NodeManager sorts the queryset by `path` by default and it's
  not done on our side, is it on purpose?
- The fact we need to redefine `readable_per_se` as a boilerplate is
  surprising.

I suggest we use the Django mechanism to generate the manager from the
queryset.
This commit is contained in:
Quentin BEY
2025-03-24 12:22:08 +01:00
committed by Manuel Raynaud
parent a1914c6259
commit 2929e98260

View File

@@ -445,28 +445,15 @@ class DocumentQuerySet(MP_NodeQuerySet):
return self.filter(link_reach=LinkReachChoices.PUBLIC) return self.filter(link_reach=LinkReachChoices.PUBLIC)
class DocumentManager(MP_NodeManager): class DocumentManager(MP_NodeManager.from_queryset(DocumentQuerySet)):
""" """
Custom manager for the Document model, enabling the use of the custom Custom manager for the Document model, enabling the use of the custom
queryset methods directly from the model manager. queryset methods directly from the model manager.
""" """
def get_queryset(self): def get_queryset(self):
""" """Sets the custom queryset as the default."""
Overrides the default get_queryset method to return a custom queryset. return self._queryset_class(self.model).order_by("path")
:return: An instance of DocumentQuerySet.
"""
return DocumentQuerySet(self.model, using=self._db)
def readable_per_se(self, user):
"""
Filters documents based on user permissions using the custom queryset.
:param user: The user for whom readable documents are to be fetched.
:return: A queryset of documents for which the user has direct access,
team access or link access. This will not return all the documents
that a user can read because it can be obtained via an ancestor.
"""
return self.get_queryset().readable_per_se(user)
class Document(MP_Node, BaseModel): class Document(MP_Node, BaseModel):