contents.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 NewContentStatus(object):
  16. """
  17. Future 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 = NewContentStatus(
  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 = NewContentStatus(
  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 = NewContentStatus(
  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 = NewContentStatus(
  54. slug='closed-deprecated',
  55. global_status=GlobalStatus.CLOSED.value,
  56. label='Deprecated',
  57. fa_icon='warning',
  58. hexcolor='#ababab',
  59. )
  60. CONTENT_DEFAULT_STATUS = [
  61. open_status,
  62. closed_validated_status,
  63. closed_unvalidated_status,
  64. closed_deprecated_status,
  65. ]
  66. class ContentStatusLegacy(NewContentStatus):
  67. """
  68. Temporary remplacement object for Legacy ContentStatus Object
  69. """
  70. OPEN = open_status.slug
  71. CLOSED_VALIDATED = closed_validated_status.slug
  72. CLOSED_UNVALIDATED = closed_unvalidated_status.slug
  73. CLOSED_DEPRECATED = closed_deprecated_status.slug
  74. def __init__(self, slug: str):
  75. for status in CONTENT_DEFAULT_STATUS:
  76. if slug == status.slug:
  77. super(ContentStatusLegacy, self).__init__(
  78. slug=status.slug,
  79. global_status=status.global_status,
  80. label=status.label,
  81. fa_icon=status.fa_icon,
  82. hexcolor=status.hexcolor,
  83. )
  84. return
  85. raise ContentStatusNotExist()
  86. @classmethod
  87. def all(cls, type='') -> ['NewContentStatus']:
  88. return CONTENT_DEFAULT_STATUS
  89. @classmethod
  90. def allowed_values(cls):
  91. return [status.slug for status in CONTENT_DEFAULT_STATUS]
  92. ####
  93. # ContentType
  94. class NewContentType(object):
  95. """
  96. Future ContentType object class
  97. """
  98. def __init__(
  99. self,
  100. slug: str,
  101. fa_icon: str,
  102. hexcolor: str,
  103. label: str,
  104. creation_label: str,
  105. available_statuses: typing.List[NewContentStatus],
  106. ):
  107. self.slug = slug
  108. self.fa_icon = fa_icon
  109. self.hexcolor = hexcolor
  110. self.label = label
  111. self.creation_label = creation_label
  112. self.available_statuses = available_statuses
  113. thread_type = NewContentType(
  114. slug='thread',
  115. fa_icon=thread.fa_icon,
  116. hexcolor=thread.hexcolor,
  117. label='Thread',
  118. creation_label='Discuss about a topic',
  119. available_statuses=CONTENT_DEFAULT_STATUS,
  120. )
  121. file_type = NewContentType(
  122. slug='file',
  123. fa_icon=_file.fa_icon,
  124. hexcolor=_file.hexcolor,
  125. label='File',
  126. creation_label='Upload a file',
  127. available_statuses=CONTENT_DEFAULT_STATUS,
  128. )
  129. markdownpluspage_type = NewContentType(
  130. slug='markdownpage',
  131. fa_icon=markdownpluspage.fa_icon,
  132. hexcolor=markdownpluspage.hexcolor,
  133. label='Rich Markdown File',
  134. creation_label='Create a Markdown document',
  135. available_statuses=CONTENT_DEFAULT_STATUS,
  136. )
  137. html_documents_type = NewContentType(
  138. slug='html-documents',
  139. fa_icon=html_documents.fa_icon,
  140. hexcolor=html_documents.hexcolor,
  141. label='Text Document',
  142. creation_label='Write a document',
  143. available_statuses=CONTENT_DEFAULT_STATUS,
  144. )
  145. # TODO - G.M - 31-05-2018 - Set Better folder params
  146. folder_type = NewContentType(
  147. slug='folder',
  148. fa_icon=thread.fa_icon,
  149. hexcolor=thread.hexcolor,
  150. label='Folder',
  151. creation_label='Create collection of any documents',
  152. available_statuses=CONTENT_DEFAULT_STATUS,
  153. )
  154. CONTENT_DEFAULT_TYPE = [
  155. thread_type,
  156. file_type,
  157. markdownpluspage_type,
  158. html_documents_type,
  159. folder_type,
  160. ]
  161. # TODO - G.M - 31-05-2018 - Set Better Event params
  162. event_type = NewContentType(
  163. slug='event',
  164. fa_icon=thread.fa_icon,
  165. hexcolor=thread.hexcolor,
  166. label='Event',
  167. creation_label='Event',
  168. available_statuses=CONTENT_DEFAULT_STATUS,
  169. )
  170. # TODO - G.M - 31-05-2018 - Set Better Event params
  171. comment_type = NewContentType(
  172. slug='comment',
  173. fa_icon=thread.fa_icon,
  174. hexcolor=thread.hexcolor,
  175. label='Comment',
  176. creation_label='Comment',
  177. available_statuses=CONTENT_DEFAULT_STATUS,
  178. )
  179. CONTENT_DEFAULT_TYPE_SPECIAL = [
  180. event_type,
  181. comment_type,
  182. ]
  183. ALL_CONTENTS_DEFAULT_TYPES = CONTENT_DEFAULT_TYPE + CONTENT_DEFAULT_TYPE_SPECIAL
  184. class ContentTypeLegacy(NewContentType):
  185. """
  186. Temporary remplacement object for Legacy ContentType Object
  187. """
  188. # special type
  189. Any = 'any'
  190. Folder = 'folder'
  191. Event = 'event'
  192. Comment = 'comment'
  193. File = file_type.slug
  194. Thread = thread_type.slug
  195. Page = html_documents_type.slug
  196. PageLegacy = 'page'
  197. MarkdownPage = markdownpluspage_type.slug
  198. def __init__(self, slug: str):
  199. if slug == 'page':
  200. slug = ContentTypeLegacy.Page
  201. for content_type in ALL_CONTENTS_DEFAULT_TYPES:
  202. if slug == content_type.slug:
  203. super(ContentTypeLegacy, self).__init__(
  204. slug=content_type.slug,
  205. fa_icon=content_type.fa_icon,
  206. hexcolor=content_type.hexcolor,
  207. label=content_type.label,
  208. creation_label=content_type.creation_label,
  209. available_statuses=content_type.available_statuses
  210. )
  211. return
  212. raise ContentTypeNotExist()
  213. def get_slug_aliases(self) -> typing.List[str]:
  214. """
  215. Get all slug aliases of a content,
  216. useful for legacy code convertion
  217. """
  218. # TODO - G.M - 2018-07-05 - Remove this legacy compat code
  219. # when possible.
  220. page_alias = [self.Page, self.PageLegacy]
  221. if self.slug in page_alias:
  222. return page_alias
  223. else:
  224. return [self.slug]
  225. @classmethod
  226. def all(cls) -> typing.List[str]:
  227. return cls.allowed_types()
  228. @classmethod
  229. def allowed_types(cls) -> typing.List[str]:
  230. contents_types = [status.slug for status in ALL_CONTENTS_DEFAULT_TYPES]
  231. return contents_types
  232. @classmethod
  233. def allowed_type_values(cls) -> typing.List[str]:
  234. """
  235. All content type slug + special values like any
  236. """
  237. content_types = cls.allowed_types()
  238. content_types.append(ContentTypeLegacy.Any)
  239. return content_types
  240. @classmethod
  241. def allowed_types_for_folding(cls):
  242. # This method is used for showing only "main"
  243. # types in the left-side treeview
  244. contents_types = [status.slug for status in CONTENT_DEFAULT_TYPE]
  245. return contents_types
  246. # TODO - G.M - 30-05-2018 - This method don't do anything.
  247. @classmethod
  248. def sorted(cls, types: ['ContentType']) -> ['ContentType']:
  249. return types
  250. @property
  251. def id(self):
  252. return self.slug
  253. def toDict(self):
  254. raise NotImplementedError()