contents.py 7.3KB

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