contents.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from enum import Enum
  4. from tracim_backend.exceptions import ContentTypeNotExist
  5. from tracim_backend.exceptions import ContentStatusNotExist
  6. from tracim_backend.models.applications import html_documents
  7. from tracim_backend.models.applications import _file
  8. from tracim_backend.models.applications import thread
  9. from tracim_backend.models.applications import markdownpluspage
  10. ####
  11. # Content Status
  12. class GlobalStatus(Enum):
  13. OPEN = 'open'
  14. CLOSED = 'closed'
  15. class ContentStatus(object):
  16. """
  17. ContentStatus object class
  18. """
  19. def __init__(
  20. self,
  21. slug: str,
  22. global_status: str,
  23. label: str,
  24. fa_icon: str,
  25. hexcolor: str,
  26. ):
  27. self.slug = slug
  28. self.global_status = global_status
  29. self.label = label
  30. self.fa_icon = fa_icon
  31. self.hexcolor = hexcolor
  32. open_status = ContentStatus(
  33. slug='open',
  34. global_status=GlobalStatus.OPEN.value,
  35. label='Open',
  36. fa_icon='square-o',
  37. hexcolor='#3f52e3',
  38. )
  39. closed_validated_status = ContentStatus(
  40. slug='closed-validated',
  41. global_status=GlobalStatus.CLOSED.value,
  42. label='Validated',
  43. fa_icon='check-square-o',
  44. hexcolor='#008000',
  45. )
  46. closed_unvalidated_status = ContentStatus(
  47. slug='closed-unvalidated',
  48. global_status=GlobalStatus.CLOSED.value,
  49. label='Cancelled',
  50. fa_icon='close',
  51. hexcolor='#f63434',
  52. )
  53. closed_deprecated_status = ContentStatus(
  54. slug='closed-deprecated',
  55. global_status=GlobalStatus.CLOSED.value,
  56. label='Deprecated',
  57. fa_icon='warning',
  58. hexcolor='#ababab',
  59. )
  60. class ContentStatusList(object):
  61. OPEN = open_status
  62. def __init__(self, extend_content_status: typing.List[ContentStatus]):
  63. self._content_status = [self.OPEN]
  64. self._content_status.extend(extend_content_status)
  65. def get_one_by_slug(self, slug: str) -> ContentStatus:
  66. for item in self._content_status:
  67. if item.slug == slug:
  68. return item
  69. raise ContentStatusNotExist()
  70. def get_all_slugs_values(self) -> typing.List[str]:
  71. """ Get alls slugs"""
  72. return [item.slug for item in self._content_status]
  73. def get_all(self) -> typing.List[ContentStatus]:
  74. """ Get all status"""
  75. return [item for item in self._content_status]
  76. def get_default_status(self) -> ContentStatus:
  77. return self.OPEN
  78. CONTENT_STATUS = ContentStatusList(
  79. [
  80. closed_validated_status,
  81. closed_unvalidated_status,
  82. closed_deprecated_status,
  83. ]
  84. )
  85. ####
  86. # ContentType
  87. class ContentType(object):
  88. """
  89. Future ContentType object class
  90. """
  91. def __init__(
  92. self,
  93. slug: str,
  94. fa_icon: str,
  95. hexcolor: str,
  96. label: str,
  97. creation_label: str,
  98. available_statuses: typing.List[ContentStatus],
  99. slug_alias: typing.List[str] = None,
  100. allow_sub_content: bool = False,
  101. ):
  102. self.slug = slug
  103. self.fa_icon = fa_icon
  104. self.hexcolor = hexcolor
  105. self.label = label
  106. self.creation_label = creation_label
  107. self.available_statuses = available_statuses
  108. self.slug_alias = slug_alias
  109. self.allow_sub_content = allow_sub_content
  110. thread_type = ContentType(
  111. slug='thread',
  112. fa_icon=thread.fa_icon,
  113. hexcolor=thread.hexcolor,
  114. label='Thread',
  115. creation_label='Discuss about a topic',
  116. available_statuses=CONTENT_STATUS.get_all(),
  117. )
  118. file_type = ContentType(
  119. slug='file',
  120. fa_icon=_file.fa_icon,
  121. hexcolor=_file.hexcolor,
  122. label='File',
  123. creation_label='Upload a file',
  124. available_statuses=CONTENT_STATUS.get_all(),
  125. )
  126. markdownpluspage_type = ContentType(
  127. slug='markdownpage',
  128. fa_icon=markdownpluspage.fa_icon,
  129. hexcolor=markdownpluspage.hexcolor,
  130. label='Rich Markdown File',
  131. creation_label='Create a Markdown document',
  132. available_statuses=CONTENT_STATUS.get_all(),
  133. )
  134. html_documents_type = ContentType(
  135. slug='html-document',
  136. fa_icon=html_documents.fa_icon,
  137. hexcolor=html_documents.hexcolor,
  138. label='Text Document',
  139. creation_label='Write a document',
  140. available_statuses=CONTENT_STATUS.get_all(),
  141. slug_alias=['page']
  142. )
  143. # TODO - G.M - 31-05-2018 - Set Better folder params
  144. folder_type = ContentType(
  145. slug='folder',
  146. fa_icon=thread.fa_icon,
  147. hexcolor=thread.hexcolor,
  148. label='Folder',
  149. creation_label='Create collection of any documents',
  150. available_statuses=CONTENT_STATUS.get_all(),
  151. )
  152. # TODO - G.M - 31-05-2018 - Set Better Event params
  153. event_type = ContentType(
  154. slug='event',
  155. fa_icon=thread.fa_icon,
  156. hexcolor=thread.hexcolor,
  157. label='Event',
  158. creation_label='Event',
  159. available_statuses=CONTENT_STATUS.get_all(),
  160. )
  161. # TODO - G.M - 31-05-2018 - Set Better Event params
  162. comment_type = ContentType(
  163. slug='comment',
  164. fa_icon=thread.fa_icon,
  165. hexcolor=thread.hexcolor,
  166. label='Comment',
  167. creation_label='Comment',
  168. available_statuses=CONTENT_STATUS.get_all(),
  169. )
  170. class ContentTypeList(object):
  171. """
  172. ContentType List
  173. """
  174. Any_SLUG = 'any'
  175. Folder = folder_type
  176. Comment = comment_type
  177. Event = event_type
  178. File = file_type
  179. Page = html_documents_type
  180. Thread = thread_type
  181. def __init__(self, extend_content_status: typing.List[ContentType]):
  182. self._content_types = [self.Folder]
  183. self._content_types.extend(extend_content_status)
  184. self._special_contents_types = [self.Comment]
  185. self._extra_slugs = [self.Any_SLUG]
  186. def get_one_by_slug(self, slug: str) -> ContentType:
  187. """
  188. Get ContentType object according to slug
  189. match for both slug and slug_alias
  190. """
  191. content_types = self._content_types.copy()
  192. content_types.extend(self._special_contents_types)
  193. content_types.append(self.Event)
  194. for item in content_types:
  195. if item.slug == slug or (item.slug_alias and slug in item.slug_alias): # nopep8
  196. return item
  197. raise ContentTypeNotExist()
  198. def endpoint_allowed_types_slug(self) -> typing.List[str]:
  199. """
  200. Return restricted list of content_type:
  201. dont return special content_type like comment, don't return
  202. "any" slug, dont return content type slug alias , don't return event.
  203. Useful to restrict slug param in schema.
  204. """
  205. allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
  206. return allowed_type_slug
  207. def extended_endpoint_allowed_types_slug(self) -> typing.List[str]:
  208. allowed_types_slug = self.endpoint_allowed_types_slug().copy()
  209. for content_type in self._special_contents_types:
  210. allowed_types_slug.append(content_type.slug)
  211. return allowed_types_slug
  212. def query_allowed_types_slugs(self) -> typing.List[str]:
  213. """
  214. Return alls allowed types slug : content_type slug + all alias, any
  215. and special content_type like comment. Do not return event.
  216. Usefull allowed value to perform query to database.
  217. """
  218. allowed_types_slug = []
  219. for content_type in self._content_types:
  220. allowed_types_slug.append(content_type.slug)
  221. if content_type.slug_alias:
  222. allowed_types_slug.extend(content_type.slug_alias)
  223. for content_type in self._special_contents_types:
  224. allowed_types_slug.append(content_type.slug)
  225. allowed_types_slug.extend(self._extra_slugs)
  226. return allowed_types_slug
  227. def default_allowed_content_properties(self, slug) -> dict:
  228. content_type = self.get_one_by_slug(slug)
  229. if content_type.allow_sub_content:
  230. sub_content_allowed = self.extended_endpoint_allowed_types_slug()
  231. else:
  232. sub_content_allowed = [self.Comment.slug]
  233. properties_dict = {}
  234. for elem in sub_content_allowed:
  235. properties_dict[elem] = True
  236. return properties_dict
  237. CONTENT_TYPES = ContentTypeList(
  238. [
  239. thread_type,
  240. file_type,
  241. markdownpluspage_type,
  242. html_documents_type,
  243. ]
  244. )