DigitalFactoryFileModel.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import List, Dict, Callable
  4. from PyQt6.QtCore import Qt, pyqtSignal
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from .DigitalFactoryFileResponse import DigitalFactoryFileResponse
  8. DIGITAL_FACTORY_DISPLAY_DATETIME_FORMAT = "%d-%m-%Y %H:%M"
  9. class DigitalFactoryFileModel(ListModel):
  10. FileNameRole = Qt.ItemDataRole.UserRole + 1
  11. FileIdRole = Qt.ItemDataRole.UserRole + 2
  12. FileSizeRole = Qt.ItemDataRole.UserRole + 3
  13. LibraryProjectIdRole = Qt.ItemDataRole.UserRole + 4
  14. DownloadUrlRole = Qt.ItemDataRole.UserRole + 5
  15. UsernameRole = Qt.ItemDataRole.UserRole + 6
  16. UploadedAtRole = Qt.ItemDataRole.UserRole + 7
  17. dfFileModelChanged = pyqtSignal()
  18. def __init__(self, parent = None):
  19. super().__init__(parent)
  20. self.addRoleName(self.FileNameRole, "fileName")
  21. self.addRoleName(self.FileIdRole, "fileId")
  22. self.addRoleName(self.FileSizeRole, "fileSize")
  23. self.addRoleName(self.LibraryProjectIdRole, "libraryProjectId")
  24. self.addRoleName(self.DownloadUrlRole, "downloadUrl")
  25. self.addRoleName(self.UsernameRole, "username")
  26. self.addRoleName(self.UploadedAtRole, "uploadedAt")
  27. self._files = [] # type: List[DigitalFactoryFileResponse]
  28. self._filters = {} # type: Dict[str, Callable]
  29. def setFiles(self, df_files_in_project: List[DigitalFactoryFileResponse]) -> None:
  30. if self._files == df_files_in_project:
  31. return
  32. self.clear()
  33. self._files = df_files_in_project
  34. self._update()
  35. def clearFiles(self) -> None:
  36. self.clear()
  37. self._files.clear()
  38. self.dfFileModelChanged.emit()
  39. def _update(self) -> None:
  40. filtered_files_list = self.getFilteredFilesList()
  41. for file in filtered_files_list:
  42. self.appendItem({
  43. "fileName" : file.file_name,
  44. "fileId" : file.file_id,
  45. "fileSize": file.file_size,
  46. "libraryProjectId": file.library_project_id,
  47. "downloadUrl": file.download_url,
  48. "username": file.username,
  49. "uploadedAt": file.uploaded_at.strftime(DIGITAL_FACTORY_DISPLAY_DATETIME_FORMAT)
  50. })
  51. self.dfFileModelChanged.emit()
  52. def setFilters(self, filters: Dict[str, Callable]) -> None:
  53. """
  54. Sets the filters and updates the files model to contain only the files that meet all of the filters.
  55. :param filters: The filters to be applied
  56. example:
  57. {
  58. "attribute_name1": function_to_be_applied_on_DigitalFactoryFileResponse_attribute1,
  59. "attribute_name2": function_to_be_applied_on_DigitalFactoryFileResponse_attribute2
  60. }
  61. """
  62. self.clear()
  63. self._filters = filters
  64. self._update()
  65. def clearFilters(self) -> None:
  66. """
  67. Clears all the model filters
  68. """
  69. self.setFilters({})
  70. def getFilteredFilesList(self) -> List[DigitalFactoryFileResponse]:
  71. """
  72. Lists the files that meet all the filters specified in the self._filters. This is achieved by applying each
  73. filter function on the corresponding attribute for all the filters in the self._filters. If all of them are
  74. true, the file is added to the filtered files list.
  75. In order for this to work, the self._filters should be in the format:
  76. {
  77. "attribute_name": function_to_be_applied_on_the_DigitalFactoryFileResponse_attribute
  78. }
  79. :return: The list of files that meet all the specified filters
  80. """
  81. if not self._filters:
  82. return self._files
  83. filtered_files_list = []
  84. for file in self._files:
  85. filter_results = []
  86. for attribute, filter_func in self._filters.items():
  87. try:
  88. filter_results.append(filter_func(getattr(file, attribute)))
  89. except AttributeError:
  90. Logger.log("w", "Attribute '{}' doesn't exist in objects of type '{}'".format(attribute, type(file)))
  91. all_filters_met = all(filter_results)
  92. if all_filters_met:
  93. filtered_files_list.append(file)
  94. return filtered_files_list