contents.py 6.5KB

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