contents.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. ):
  101. self.slug = slug
  102. self.fa_icon = fa_icon
  103. self.hexcolor = hexcolor
  104. self.label = label
  105. self.creation_label = creation_label
  106. self.available_statuses = available_statuses
  107. self.slug_alias = slug_alias
  108. thread_type = ContentType(
  109. slug='thread',
  110. fa_icon=thread.fa_icon,
  111. hexcolor=thread.hexcolor,
  112. label='Thread',
  113. creation_label='Discuss about a topic',
  114. available_statuses=CONTENT_STATUS.get_all(),
  115. )
  116. file_type = ContentType(
  117. slug='file',
  118. fa_icon=_file.fa_icon,
  119. hexcolor=_file.hexcolor,
  120. label='File',
  121. creation_label='Upload a file',
  122. available_statuses=CONTENT_STATUS.get_all(),
  123. )
  124. markdownpluspage_type = ContentType(
  125. slug='markdownpage',
  126. fa_icon=markdownpluspage.fa_icon,
  127. hexcolor=markdownpluspage.hexcolor,
  128. label='Rich Markdown File',
  129. creation_label='Create a Markdown document',
  130. available_statuses=CONTENT_STATUS.get_all(),
  131. )
  132. html_documents_type = ContentType(
  133. slug='html-document',
  134. fa_icon=html_documents.fa_icon,
  135. hexcolor=html_documents.hexcolor,
  136. label='Text Document',
  137. creation_label='Write a document',
  138. available_statuses=CONTENT_STATUS.get_all(),
  139. slug_alias=['page']
  140. )
  141. # TODO - G.M - 31-05-2018 - Set Better folder params
  142. folder_type = ContentType(
  143. slug='folder',
  144. fa_icon=thread.fa_icon,
  145. hexcolor=thread.hexcolor,
  146. label='Folder',
  147. creation_label='Create collection of any documents',
  148. available_statuses=CONTENT_STATUS.get_all(),
  149. )
  150. # TODO - G.M - 31-05-2018 - Set Better Event params
  151. event_type = ContentType(
  152. slug='event',
  153. fa_icon=thread.fa_icon,
  154. hexcolor=thread.hexcolor,
  155. label='Event',
  156. creation_label='Event',
  157. available_statuses=CONTENT_STATUS.get_all(),
  158. )
  159. # TODO - G.M - 31-05-2018 - Set Better Event params
  160. comment_type = ContentType(
  161. slug='comment',
  162. fa_icon=thread.fa_icon,
  163. hexcolor=thread.hexcolor,
  164. label='Comment',
  165. creation_label='Comment',
  166. available_statuses=CONTENT_STATUS.get_all(),
  167. )
  168. class ContentTypeList(object):
  169. """
  170. ContentType List
  171. """
  172. Any_SLUG = 'any'
  173. Folder = folder_type
  174. Comment = comment_type
  175. Event = event_type
  176. File = file_type
  177. Page = html_documents_type
  178. Thread = thread_type
  179. def __init__(self, extend_content_status: typing.List[ContentType]):
  180. self._content_types = [self.Folder]
  181. self._content_types.extend(extend_content_status)
  182. self._special_contents_types = [self.Comment]
  183. self._extra_slugs = [self.Any_SLUG]
  184. def get_one_by_slug(self, slug: str) -> ContentType:
  185. """
  186. Get ContentType object according to slug
  187. match for both slug and slug_alias
  188. """
  189. content_types = self._content_types.copy()
  190. content_types.extend(self._special_contents_types)
  191. for item in content_types:
  192. if item.slug == slug or (item.slug_alias and slug in item.slug_alias): # nopep8
  193. return item
  194. raise ContentTypeNotExist()
  195. def endpoint_allowed_types_slug(self) -> typing.List[str]:
  196. """
  197. Return restricted list of content_type:
  198. dont return special content_type like comment, don't return
  199. "any" slug, dont return content type slug alias , don't return event.
  200. Useful to restrict slug param in schema.
  201. """
  202. allowed_type_slug = [contents_type.slug for contents_type in self._content_types] # nopep8
  203. return allowed_type_slug
  204. def query_allowed_types_slugs(self) -> typing.List[str]:
  205. """
  206. Return alls allowed types slug : content_type slug + all alias, any
  207. and special content_type like comment. Do not return event.
  208. Usefull allowed value to perform query to database.
  209. """
  210. allowed_types_slug = []
  211. for content_type in self._content_types:
  212. allowed_types_slug.append(content_type.slug)
  213. if content_type.slug_alias:
  214. allowed_types_slug.extend(content_type.slug_alias)
  215. for content_type in self._special_contents_types:
  216. allowed_types_slug.append(content_type.slug)
  217. allowed_types_slug.extend(self._extra_slugs)
  218. return allowed_types_slug
  219. CONTENT_TYPES = ContentTypeList(
  220. [
  221. thread_type,
  222. file_type,
  223. markdownpluspage_type,
  224. html_documents_type,
  225. ]
  226. )