contents.py 8.2KB

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