From 300176cdc69d728fc27a01a35356a2d37eb51186 Mon Sep 17 00:00:00 2001 From: Konstantinos Karmas Date: Wed, 14 Jul 2021 13:03:03 +0200 Subject: [PATCH] Check for the maximum **private** projects When checking whether the user is allowed to create a new library project, we need to retrieve the **private** (aka non-shared) projects that are linked to the user's account. If the user has reached the maximum private projects, then they are no longer allowed to create new ones. **Note**: We need to set the `pagination_manager` to `None` when doing this get request, or else the next/previous links of the pagination will become mixed up with the pagination links of the list of projects shown to the user, corrupting them and creating the wrong "get more projects" link. CURA-8112 --- .../src/DigitalFactoryApiClient.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/plugins/DigitalLibrary/src/DigitalFactoryApiClient.py b/plugins/DigitalLibrary/src/DigitalFactoryApiClient.py index c0c6099b0d..91f1daf2f7 100644 --- a/plugins/DigitalLibrary/src/DigitalFactoryApiClient.py +++ b/plugins/DigitalLibrary/src/DigitalFactoryApiClient.py @@ -89,9 +89,9 @@ class DigitalFactoryApiClient: def callbackWrap(response: Optional[Any] = None, *args, **kwargs) -> None: if response is not None: - if self._library_max_private_projects == -1 or isinstance(response, DigitalFactoryProjectResponse): + if isinstance(response, DigitalFactoryProjectResponse): # The user has only one private project callback(True) - elif isinstance(response, list) and all(isinstance(r, DigitalFactoryProjectResponse) for r in response) and self._library_max_private_projects is not None: + elif isinstance(response, list) and all(isinstance(r, DigitalFactoryProjectResponse) for r in response): callback(len(response) < self._library_max_private_projects) else: Logger.warning(f"Digital Factory: Incorrect response type received when requesting private projects: {str(response)}") @@ -100,7 +100,21 @@ class DigitalFactoryApiClient: Logger.warning(f"Digital Factory: Response is empty, likely an error: {str(response)}") callback(False) - self.getProjectsFirstPage(on_finished = callbackWrap, failed = callbackWrap) + if self._library_max_private_projects is not None and self._library_max_private_projects > 0: + # The user has a limit in the number of private projects they can create. Check whether they have already + # reached that limit. + # Note: Set the pagination manager to None when doing this get request, or else the next/previous links + # of the pagination will become corrupted + url = f"{self.CURA_API_ROOT}/projects?shared=false&limit={self._library_max_private_projects}" + self._http.get(url, + scope = self._scope, + callback = self._parseCallback(callbackWrap, DigitalFactoryProjectResponse, callbackWrap, pagination_manager = None), + error_callback = callbackWrap, + timeout = self.DEFAULT_REQUEST_TIMEOUT) + else: + # If the limit is -1, then the user is allowed unlimited projects. If its 0 then they are not allowed to + # create any projects + callback(self._library_max_private_projects == -1) def getProject(self, library_project_id: str, on_finished: Callable[[DigitalFactoryProjectResponse], Any], failed: Callable) -> None: """