contents.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 allowed_slugs_values(self) -> typing.List[str]:
  71. """ Get alls slugs"""
  72. return [item.slug for item in self._content_status]
  73. def allowed(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.allowed(),
  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.allowed(),
  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.allowed(),
  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.allowed(),
  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.allowed(),
  151. allow_sub_content=True,
  152. )
  153. # TODO - G.M - 31-05-2018 - Set Better Event params
  154. event_type = ContentType(
  155. slug='event',
  156. fa_icon=thread.fa_icon,
  157. hexcolor=thread.hexcolor,
  158. label='Event',
  159. creation_label='Event',
  160. available_statuses=CONTENT_STATUS.allowed(),
  161. )
  162. # TODO - G.M - 31-05-2018 - Set Better Event params
  163. comment_type = ContentType(
  164. slug='comment',
  165. fa_icon=thread.fa_icon,
  166. hexcolor=thread.hexcolor,
  167. label='Comment',
  168. creation_label='Comment',
  169. available_statuses=CONTENT_STATUS.allowed(),
  170. )
  171. class ContentTypeList(object):
  172. """
  173. ContentType List
  174. """
  175. Any_SLUG = 'any'
  176. Folder = folder_type
  177. Comment = comment_type
  178. Event = event_type
  179. File = file_type
  180. Page = html_documents_type
  181. Thread = thread_type
  182. def __init__(self, extend_content_status: typing.List[ContentType]):
  183. self._content_types = [self.Folder]
  184. self._content_types.extend(extend_content_status)
  185. self._special_contents_types = [self.Comment]
  186. self._extra_slugs = [self.Any_SLUG]
  187. def get_one_by_slug(self, slug: str) -> ContentType:
  188. """
  189. Get ContentType object according to slug
  190. match for both slug and slug_alias
  191. """
  192. content_types = self._content_types.copy()
  193. content_types.extend(self._special_contents_types)
  194. content_types.append(self.Event)
  195. for item in content_types:
  196. if item.slug == slug or (item.slug_alias and slug in item.slug_alias): # nopep8
  197. return item
  198. raise ContentTypeNotExist()
  199. def endpoint_allowed_types_slug(self) -> typing.List[str]:
  200. """
  201. Return restricted list of content_type:
  202. dont return special content_type like comment, don't return
  203. "any" slug, dont return content type slug alias , don't return event.
  204. Useful to restrict slug param in schema.
  205. """
  206. allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
  207. return allowed_type_slug
  208. def extended_endpoint_allowed_types_slug(self) -> typing.List[str]:
  209. allowed_types_slug = self.endpoint_allowed_types_slug().copy()
  210. for content_type in self._special_contents_types:
  211. allowed_types_slug.append(content_type.slug)
  212. return allowed_types_slug
  213. def query_allowed_types_slugs(self) -> typing.List[str]:
  214. """
  215. Return alls allowed types slug : content_type slug + all alias, any
  216. and special content_type like comment. Do not return event.
  217. Usefull allowed value to perform query to database.
  218. """
  219. allowed_types_slug = []
  220. for content_type in self._content_types:
  221. allowed_types_slug.append(content_type.slug)
  222. if content_type.slug_alias:
  223. allowed_types_slug.extend(content_type.slug_alias)
  224. for content_type in self._special_contents_types:
  225. allowed_types_slug.append(content_type.slug)
  226. allowed_types_slug.extend(self._extra_slugs)
  227. return allowed_types_slug
  228. def default_allowed_content_properties(self, slug) -> dict:
  229. content_type = self.get_one_by_slug(slug)
  230. if content_type.allow_sub_content:
  231. sub_content_allowed = self.extended_endpoint_allowed_types_slug()
  232. else:
  233. sub_content_allowed = [self.Comment.slug]
  234. properties_dict = {}
  235. for elem in sub_content_allowed:
  236. properties_dict[elem] = True
  237. return properties_dict
  238. CONTENT_TYPES = ContentTypeList(
  239. [
  240. thread_type,
  241. file_type,
  242. markdownpluspage_type,
  243. html_documents_type,
  244. ]
  245. )