contents.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. return [item.slug for item in self._content_status]
  72. def allowed(self) -> typing.List[ContentStatus]:
  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 NewContentType(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. ):
  98. self.slug = slug
  99. self.fa_icon = fa_icon
  100. self.hexcolor = hexcolor
  101. self.label = label
  102. self.creation_label = creation_label
  103. self.available_statuses = available_statuses
  104. thread_type = NewContentType(
  105. slug='thread',
  106. fa_icon=thread.fa_icon,
  107. hexcolor=thread.hexcolor,
  108. label='Thread',
  109. creation_label='Discuss about a topic',
  110. available_statuses=CONTENT_STATUS.allowed(),
  111. )
  112. file_type = NewContentType(
  113. slug='file',
  114. fa_icon=_file.fa_icon,
  115. hexcolor=_file.hexcolor,
  116. label='File',
  117. creation_label='Upload a file',
  118. available_statuses=CONTENT_STATUS.allowed(),
  119. )
  120. markdownpluspage_type = NewContentType(
  121. slug='markdownpage',
  122. fa_icon=markdownpluspage.fa_icon,
  123. hexcolor=markdownpluspage.hexcolor,
  124. label='Rich Markdown File',
  125. creation_label='Create a Markdown document',
  126. available_statuses=CONTENT_STATUS.allowed(),
  127. )
  128. html_documents_type = NewContentType(
  129. slug='html-documents',
  130. fa_icon=html_documents.fa_icon,
  131. hexcolor=html_documents.hexcolor,
  132. label='Text Document',
  133. creation_label='Write a document',
  134. available_statuses=CONTENT_STATUS.allowed(),
  135. )
  136. # TODO - G.M - 31-05-2018 - Set Better folder params
  137. folder_type = NewContentType(
  138. slug='folder',
  139. fa_icon=thread.fa_icon,
  140. hexcolor=thread.hexcolor,
  141. label='Folder',
  142. creation_label='Create collection of any documents',
  143. available_statuses=CONTENT_STATUS.allowed(),
  144. )
  145. CONTENT_DEFAULT_TYPE = [
  146. thread_type,
  147. file_type,
  148. markdownpluspage_type,
  149. html_documents_type,
  150. folder_type,
  151. ]
  152. # TODO - G.M - 31-05-2018 - Set Better Event params
  153. event_type = NewContentType(
  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.allowed(),
  160. )
  161. # TODO - G.M - 31-05-2018 - Set Better Event params
  162. comment_type = NewContentType(
  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.allowed(),
  169. )
  170. CONTENT_DEFAULT_TYPE_SPECIAL = [
  171. event_type,
  172. comment_type,
  173. ]
  174. ALL_CONTENTS_DEFAULT_TYPES = CONTENT_DEFAULT_TYPE + CONTENT_DEFAULT_TYPE_SPECIAL
  175. class ContentTypeLegacy(NewContentType):
  176. """
  177. Temporary remplacement object for Legacy ContentType Object
  178. """
  179. # special type
  180. Any = 'any'
  181. Folder = 'folder'
  182. Event = 'event'
  183. Comment = 'comment'
  184. File = file_type.slug
  185. Thread = thread_type.slug
  186. Page = html_documents_type.slug
  187. PageLegacy = 'page'
  188. MarkdownPage = markdownpluspage_type.slug
  189. def __init__(self, slug: str):
  190. if slug == 'page':
  191. slug = ContentTypeLegacy.Page
  192. for content_type in ALL_CONTENTS_DEFAULT_TYPES:
  193. if slug == content_type.slug:
  194. super(ContentTypeLegacy, self).__init__(
  195. slug=content_type.slug,
  196. fa_icon=content_type.fa_icon,
  197. hexcolor=content_type.hexcolor,
  198. label=content_type.label,
  199. creation_label=content_type.creation_label,
  200. available_statuses=content_type.available_statuses
  201. )
  202. return
  203. raise ContentTypeNotExist()
  204. def get_slug_aliases(self) -> typing.List[str]:
  205. """
  206. Get all slug aliases of a content,
  207. useful for legacy code convertion
  208. """
  209. # TODO - G.M - 2018-07-05 - Remove this legacy compat code
  210. # when possible.
  211. page_alias = [self.Page, self.PageLegacy]
  212. if self.slug in page_alias:
  213. return page_alias
  214. else:
  215. return [self.slug]
  216. @classmethod
  217. def all(cls) -> typing.List[str]:
  218. return cls.allowed_types()
  219. @classmethod
  220. def allowed_types(cls) -> typing.List[str]:
  221. contents_types = [status.slug for status in ALL_CONTENTS_DEFAULT_TYPES]
  222. return contents_types
  223. @classmethod
  224. def allowed_type_values(cls) -> typing.List[str]:
  225. """
  226. All content type slug + special values like any
  227. """
  228. content_types = cls.allowed_types()
  229. content_types.append(ContentTypeLegacy.Any)
  230. return content_types
  231. @classmethod
  232. def allowed_types_for_folding(cls):
  233. # This method is used for showing only "main"
  234. # types in the left-side treeview
  235. contents_types = [status.slug for status in CONTENT_DEFAULT_TYPE]
  236. return contents_types
  237. # TODO - G.M - 30-05-2018 - This method don't do anything.
  238. @classmethod
  239. def sorted(cls, types: ['ContentType']) -> ['ContentType']:
  240. return types
  241. @property
  242. def id(self):
  243. return self.slug
  244. def toDict(self):
  245. raise NotImplementedError()