contents.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from enum import Enum
  4. from tracim_backend.extensions import APP_LIST
  5. from tracim_backend.exceptions import ContentTypeNotExist
  6. from tracim_backend.exceptions import ContentStatusNotExist
  7. ####
  8. # Content Status
  9. from tracim_backend.lib.core.application import ApplicationApi
  10. class GlobalStatus(Enum):
  11. OPEN = 'open'
  12. CLOSED = 'closed'
  13. class ContentStatus(object):
  14. """
  15. ContentStatus object class
  16. """
  17. def __init__(
  18. self,
  19. slug: str,
  20. global_status: str,
  21. label: str,
  22. fa_icon: str,
  23. hexcolor: str,
  24. ):
  25. self.slug = slug
  26. self.global_status = global_status
  27. self.label = label
  28. self.fa_icon = fa_icon
  29. self.hexcolor = hexcolor
  30. open_status = ContentStatus(
  31. slug='open',
  32. global_status=GlobalStatus.OPEN.value,
  33. label='Open',
  34. fa_icon='square-o',
  35. hexcolor='#3f52e3',
  36. )
  37. closed_validated_status = ContentStatus(
  38. slug='closed-validated',
  39. global_status=GlobalStatus.CLOSED.value,
  40. label='Validated',
  41. fa_icon='check-square-o',
  42. hexcolor='#008000',
  43. )
  44. closed_unvalidated_status = ContentStatus(
  45. slug='closed-unvalidated',
  46. global_status=GlobalStatus.CLOSED.value,
  47. label='Cancelled',
  48. fa_icon='close',
  49. hexcolor='#f63434',
  50. )
  51. closed_deprecated_status = ContentStatus(
  52. slug='closed-deprecated',
  53. global_status=GlobalStatus.CLOSED.value,
  54. label='Deprecated',
  55. fa_icon='warning',
  56. hexcolor='#ababab',
  57. )
  58. class ContentStatusList(object):
  59. OPEN = open_status
  60. def __init__(self, extend_content_status: typing.List[ContentStatus]):
  61. self._content_status = [self.OPEN]
  62. self._content_status.extend(extend_content_status)
  63. def get_one_by_slug(self, slug: str) -> ContentStatus:
  64. for item in self._content_status:
  65. if item.slug == slug:
  66. return item
  67. raise ContentStatusNotExist()
  68. def get_all_slugs_values(self) -> typing.List[str]:
  69. """ Get alls slugs"""
  70. return [item.slug for item in self._content_status]
  71. def get_all(self) -> typing.List[ContentStatus]:
  72. """ Get all status"""
  73. return [item for item in self._content_status]
  74. def get_default_status(self) -> ContentStatus:
  75. return self.OPEN
  76. CONTENT_STATUS = ContentStatusList(
  77. [
  78. closed_validated_status,
  79. closed_unvalidated_status,
  80. closed_deprecated_status,
  81. ]
  82. )
  83. ####
  84. # ContentType
  85. class ContentType(object):
  86. """
  87. Future ContentType object class
  88. """
  89. def __init__(
  90. self,
  91. slug: str,
  92. fa_icon: str,
  93. hexcolor: str,
  94. label: str,
  95. creation_label: str,
  96. available_statuses: typing.List[ContentStatus],
  97. slug_alias: typing.List[str] = None,
  98. ):
  99. self.slug = slug
  100. self.fa_icon = fa_icon
  101. self.hexcolor = hexcolor
  102. self.label = label
  103. self.creation_label = creation_label
  104. self.available_statuses = available_statuses
  105. self.slug_alias = slug_alias
  106. thread_type = 'thread'
  107. file_type = 'file'
  108. markdownpluspage_type = 'markdownpage'
  109. html_documents_type = 'html-document'
  110. folder_type = 'folder'
  111. # TODO - G.M - 31-05-2018 - Set Better Event params
  112. event_type = ContentType(
  113. slug='event',
  114. fa_icon='',
  115. hexcolor='',
  116. label='Event',
  117. creation_label='Event',
  118. available_statuses=CONTENT_STATUS.get_all(),
  119. )
  120. # TODO - G.M - 31-05-2018 - Set Better Event params
  121. comment_type = ContentType(
  122. slug='comment',
  123. fa_icon='',
  124. hexcolor='',
  125. label='Comment',
  126. creation_label='Comment',
  127. available_statuses=CONTENT_STATUS.get_all(),
  128. )
  129. class ContentTypeList(object):
  130. """
  131. ContentType List
  132. """
  133. Any_SLUG = 'any'
  134. Comment = comment_type
  135. Event = event_type
  136. @property
  137. def Folder(self):
  138. return self.get_one_by_slug('folder')
  139. @property
  140. def File(self):
  141. return self.get_one_by_slug('file')
  142. @property
  143. def Page(self):
  144. return self.get_one_by_slug('html-document')
  145. @property
  146. def Thread(self):
  147. return self.get_one_by_slug('thread')
  148. def __init__(self, app_list: typing.List['Application']):
  149. self.app_list = app_list
  150. self._special_contents_types = [self.Comment]
  151. self._extra_slugs = [self.Any_SLUG]
  152. @property
  153. def _content_types(self):
  154. app_api = ApplicationApi(self.app_list)
  155. content_types = app_api.get_content_types()
  156. content_types.extend(self._special_contents_types)
  157. return content_types
  158. def get_one_by_slug(self, slug: str) -> ContentType:
  159. """
  160. Get ContentType object according to slug
  161. match for both slug and slug_alias
  162. """
  163. content_types = self._content_types.copy()
  164. content_types.extend(self._special_contents_types)
  165. for item in content_types:
  166. if item.slug == slug or (item.slug_alias and slug in item.slug_alias): # nopep8
  167. return item
  168. raise ContentTypeNotExist()
  169. def endpoint_allowed_types_slug(self) -> typing.List[str]:
  170. """
  171. Return restricted list of content_type:
  172. dont return special content_type like comment, don't return
  173. "any" slug, dont return content type slug alias , don't return event.
  174. Useful to restrict slug param in schema.
  175. """
  176. allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
  177. return allowed_type_slug
  178. def query_allowed_types_slugs(self) -> typing.List[str]:
  179. """
  180. Return alls allowed types slug : content_type slug + all alias, any
  181. and special content_type like comment. Do not return event.
  182. Usefull allowed value to perform query to database.
  183. """
  184. allowed_types_slug = []
  185. for content_type in self._content_types:
  186. allowed_types_slug.append(content_type.slug)
  187. if content_type.slug_alias:
  188. allowed_types_slug.extend(content_type.slug_alias)
  189. for content_type in self._special_contents_types:
  190. allowed_types_slug.append(content_type.slug)
  191. allowed_types_slug.extend(self._extra_slugs)
  192. return allowed_types_slug
  193. CONTENT_TYPES = ContentTypeList(APP_LIST)