contents.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. allow_sub_content: bool = False,
  99. ):
  100. self.slug = slug
  101. self.fa_icon = fa_icon
  102. self.hexcolor = hexcolor
  103. self.label = label
  104. self.creation_label = creation_label
  105. self.available_statuses = available_statuses
  106. self.slug_alias = slug_alias
  107. self.allow_sub_content = allow_sub_content
  108. thread_type = 'thread'
  109. file_type = 'file'
  110. markdownpluspage_type = 'markdownpage'
  111. html_documents_type = 'html-document'
  112. folder_type = 'folder'
  113. # TODO - G.M - 31-05-2018 - Set Better Event params
  114. event_type = ContentType(
  115. slug='event',
  116. fa_icon='',
  117. hexcolor='',
  118. label='Event',
  119. creation_label='Event',
  120. available_statuses=CONTENT_STATUS.get_all(),
  121. )
  122. # TODO - G.M - 31-05-2018 - Set Better Event params
  123. comment_type = ContentType(
  124. slug='comment',
  125. fa_icon='',
  126. hexcolor='',
  127. label='Comment',
  128. creation_label='Comment',
  129. available_statuses=CONTENT_STATUS.get_all(),
  130. )
  131. class ContentTypeList(object):
  132. """
  133. ContentType List
  134. """
  135. Any_SLUG = 'any'
  136. Comment = comment_type
  137. Event = event_type
  138. @property
  139. def Folder(self):
  140. return self.get_one_by_slug('folder')
  141. @property
  142. def File(self):
  143. return self.get_one_by_slug('file')
  144. @property
  145. def Page(self):
  146. return self.get_one_by_slug('html-document')
  147. @property
  148. def Thread(self):
  149. return self.get_one_by_slug('thread')
  150. def __init__(self, app_list: typing.List['Application']):
  151. self.app_list = app_list
  152. self._special_contents_types = [self.Comment]
  153. self._extra_slugs = [self.Any_SLUG]
  154. @property
  155. def _content_types(self):
  156. app_api = ApplicationApi(self.app_list)
  157. content_types = app_api.get_content_types()
  158. # content_types.extend(self._special_contents_types)
  159. return content_types
  160. def get_one_by_slug(self, slug: str) -> ContentType:
  161. """
  162. Get ContentType object according to slug
  163. match for both slug and slug_alias
  164. """
  165. content_types = self._content_types.copy()
  166. content_types.extend(self._special_contents_types)
  167. content_types.append(self.Event)
  168. for item in content_types:
  169. if item.slug == slug or (item.slug_alias and slug in item.slug_alias): # nopep8
  170. return item
  171. raise ContentTypeNotExist()
  172. def restricted_allowed_types_slug(self) -> typing.List[str]:
  173. """
  174. Return restricted list of content_type: don't return
  175. "any" slug, dont return content type slug alias , don't return event.
  176. Useful to restrict slug param in schema.
  177. """
  178. allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
  179. return allowed_type_slug
  180. def endpoint_allowed_types_slug(self) -> typing.List[str]:
  181. """
  182. Same as restricted_allowed_types_slug but with special content_type
  183. included like comments.
  184. """
  185. content_types = self._content_types
  186. content_types.extend(self._special_contents_types)
  187. allowed_type_slug = [contents_type.slug for contents_type in content_types] # nopep8
  188. return allowed_type_slug
  189. def query_allowed_types_slugs(self) -> typing.List[str]:
  190. """
  191. Return alls allowed types slug : content_type slug + all alias, any
  192. and special content_type like comment. Do not return event.
  193. Usefull allowed value to perform query to database.
  194. """
  195. allowed_types_slug = []
  196. content_types = self._content_types
  197. content_types.extend(self._special_contents_types)
  198. for content_type in content_types:
  199. allowed_types_slug.append(content_type.slug)
  200. if content_type.slug_alias:
  201. allowed_types_slug.extend(content_type.slug_alias)
  202. allowed_types_slug.extend(self._extra_slugs)
  203. return allowed_types_slug
  204. def default_allowed_content_properties(self, slug) -> dict:
  205. content_type = self.get_one_by_slug(slug)
  206. if content_type.allow_sub_content:
  207. sub_content_allowed = self.endpoint_allowed_types_slug()
  208. else:
  209. sub_content_allowed = [self.Comment.slug]
  210. properties_dict = {}
  211. for elem in sub_content_allowed:
  212. properties_dict[elem] = True
  213. return properties_dict
  214. CONTENT_TYPES = ContentTypeList(APP_LIST)