schemas.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # coding=utf-8
  2. import marshmallow
  3. from marshmallow import post_load
  4. from marshmallow.validate import OneOf
  5. from tracim.models.auth import Profile
  6. from tracim.models.contents import CONTENT_DEFAULT_TYPE, GlobalStatus, CONTENT_DEFAULT_STATUS
  7. from tracim.models.context_models import LoginCredentials, ContentFilter
  8. from tracim.models.data import UserRoleInWorkspace
  9. class ProfileSchema(marshmallow.Schema):
  10. slug = marshmallow.fields.String(
  11. attribute='name',
  12. validate=OneOf(Profile._NAME),
  13. example='managers',
  14. )
  15. class Meta:
  16. description = 'User Profile, give user right on whole Tracim instance.'
  17. class UserSchema(marshmallow.Schema):
  18. user_id = marshmallow.fields.Int(dump_only=True, example=3)
  19. email = marshmallow.fields.Email(
  20. required=True,
  21. example='suri.cate@algoo.fr'
  22. )
  23. display_name = marshmallow.fields.String(
  24. example='Suri Cate',
  25. )
  26. created = marshmallow.fields.DateTime(
  27. format='iso8601',
  28. description='User account creation date (iso8601 format).',
  29. )
  30. is_active = marshmallow.fields.Bool(
  31. example=True,
  32. # TODO - G.M - Explains this value.
  33. )
  34. # TODO - G.M - 17-04-2018 - Restrict timezone values
  35. timezone = marshmallow.fields.String(
  36. example="Paris/Europe",
  37. )
  38. # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
  39. caldav_url = marshmallow.fields.Url(
  40. allow_none=True,
  41. relative=True,
  42. attribute='calendar_url',
  43. example="/api/v2/calendar/user/3.ics/",
  44. description="The url for calendar CalDAV direct access",
  45. )
  46. avatar_url = marshmallow.fields.Url(
  47. allow_none=True,
  48. example="/api/v2/assets/avatars/suri-cate.jpg",
  49. description="avatar_url is the url to the image file. "
  50. "If no avatar, then set it to null "
  51. "(and frontend will interpret this with a default avatar)",
  52. )
  53. profile = marshmallow.fields.Nested(
  54. ProfileSchema,
  55. many=False,
  56. )
  57. class Meta:
  58. description = 'User account of Tracim'
  59. # Path Schemas
  60. class UserIdPathSchema(marshmallow.Schema):
  61. user_id = marshmallow.fields.Int(example=3, required=True)
  62. class WorkspaceIdPathSchema(marshmallow.Schema):
  63. workspace_id = marshmallow.fields.Int(example=4, required=True)
  64. class ContentIdPathSchema(marshmallow.Schema):
  65. content_id = marshmallow.fields.Int(example=6, required=True)
  66. class WorkspaceAndContentIdPathSchema(WorkspaceIdPathSchema, ContentIdPathSchema):
  67. pass
  68. class FilterContentQuerySchema(marshmallow.Schema):
  69. parent_id = workspace_id = marshmallow.fields.Int(
  70. example=2,
  71. default=None,
  72. description='allow to filter items in a folder.'
  73. ' If not set, then return all contents.'
  74. ' If set to 0, then return root contents.'
  75. ' If set to another value, return all contents'
  76. ' directly included in the folder parent_id'
  77. )
  78. show_archived = marshmallow.fields.Int(
  79. example=0,
  80. default=0,
  81. description='if set to 1, then show archived contents.'
  82. ' Default is 0 - hide archived content'
  83. )
  84. show_deleted = marshmallow.fields.Int(
  85. example=0,
  86. default=0,
  87. description='if set to 1, then show deleted contents.'
  88. ' Default is 0 - hide deleted content'
  89. )
  90. show_active = marshmallow.fields.Int(
  91. example=1,
  92. default=1,
  93. description='f set to 1, then show active contents. '
  94. 'Default is 1 - show active content.'
  95. ' Note: active content are content '
  96. 'that is neither archived nor deleted. '
  97. 'The reason for this parameter to exist is for example '
  98. 'to allow to show only archived documents'
  99. )
  100. @post_load
  101. def make_content_filter(self, data):
  102. return ContentFilter(**data)
  103. ###
  104. class BasicAuthSchema(marshmallow.Schema):
  105. email = marshmallow.fields.Email(
  106. example='suri.cate@algoo.fr',
  107. required=True
  108. )
  109. password = marshmallow.fields.String(
  110. example='8QLa$<w',
  111. required=True,
  112. load_only=True,
  113. )
  114. class Meta:
  115. description = 'Entry for HTTP Basic Auth'
  116. @post_load
  117. def make_login(self, data):
  118. return LoginCredentials(**data)
  119. class LoginOutputHeaders(marshmallow.Schema):
  120. expire_after = marshmallow.fields.String()
  121. class NoContentSchema(marshmallow.Schema):
  122. class Meta:
  123. description = 'Empty Schema'
  124. pass
  125. class WorkspaceMenuEntrySchema(marshmallow.Schema):
  126. slug = marshmallow.fields.String(example='markdown-pages')
  127. label = marshmallow.fields.String(example='Markdown Documents')
  128. route = marshmallow.fields.String(
  129. example='/#/workspace/{workspace_id}/contents/?type=mardown-page',
  130. description='the route is the frontend route. '
  131. 'It may include workspace_id '
  132. 'which must be replaced on backend size '
  133. '(the route must be ready-to-use)'
  134. )
  135. icon = marshmallow.fields.String(
  136. example='file-text-o',
  137. description='CSS class of the icon. Example: file-o for using Fontawesome file-text-o icon', # nopep8
  138. )
  139. hexcolor = marshmallow.fields.String(
  140. example='#F0F9DC',
  141. description='Hexadecimal color of the entry.'
  142. )
  143. class Meta:
  144. description = 'Entry element of a workspace menu'
  145. class WorkspaceDigestSchema(marshmallow.Schema):
  146. id = marshmallow.fields.Int(example=4)
  147. label = marshmallow.fields.String(example='Intranet')
  148. sidebar_entries = marshmallow.fields.Nested(
  149. WorkspaceMenuEntrySchema,
  150. many=True,
  151. )
  152. class Meta:
  153. description = 'Digest of workspace informations'
  154. class WorkspaceSchema(WorkspaceDigestSchema):
  155. slug = marshmallow.fields.String(example='intranet')
  156. description = marshmallow.fields.String(example='All intranet data.')
  157. class Meta:
  158. description = 'Full workspace informations'
  159. class WorkspaceMemberSchema(marshmallow.Schema):
  160. role_slug = marshmallow.fields.String(
  161. example='contributor',
  162. validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
  163. )
  164. user_id = marshmallow.fields.Int(example=3)
  165. workspace_id = marshmallow.fields.Int(example=4)
  166. user = marshmallow.fields.Nested(
  167. UserSchema(only=('display_name', 'avatar_url'))
  168. )
  169. class Meta:
  170. description = 'Workspace Member information'
  171. class ApplicationConfigSchema(marshmallow.Schema):
  172. pass
  173. # TODO - G.M - 24-05-2018 - Set this
  174. class ApplicationSchema(marshmallow.Schema):
  175. label = marshmallow.fields.String(example='Calendar')
  176. slug = marshmallow.fields.String(example='calendar')
  177. icon = marshmallow.fields.String(
  178. example='file-o',
  179. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  180. )
  181. hexcolor = marshmallow.fields.String(
  182. example='#FF0000',
  183. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  184. )
  185. is_active = marshmallow.fields.Boolean(
  186. example=True,
  187. description='if true, the application is in use in the context',
  188. )
  189. config = marshmallow.fields.Nested(
  190. ApplicationConfigSchema,
  191. )
  192. class Meta:
  193. description = 'Tracim Application informations'
  194. class StatusSchema(marshmallow.Schema):
  195. slug = marshmallow.fields.String(
  196. example='open',
  197. description='the slug represents the type of status. '
  198. 'Statuses are open, closed-validated, closed-invalidated, closed-deprecated' # nopep8
  199. )
  200. global_status = marshmallow.fields.String(
  201. example='Open',
  202. description='global_status: open, closed',
  203. validate=OneOf([status.value for status in GlobalStatus]),
  204. )
  205. label = marshmallow.fields.String(example='Open')
  206. icon = marshmallow.fields.String(example='fa-check')
  207. hexcolor = marshmallow.fields.String(example='#0000FF')
  208. class ContentTypeSchema(marshmallow.Schema):
  209. slug = marshmallow.fields.String(
  210. example='pagehtml',
  211. validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
  212. )
  213. icon = marshmallow.fields.String(
  214. example='fa-file-text-o',
  215. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  216. )
  217. hexcolor = marshmallow.fields.String(
  218. example="#FF0000",
  219. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  220. )
  221. label = marshmallow.fields.String(
  222. example='Text Documents'
  223. )
  224. creation_label = marshmallow.fields.String(
  225. example='Write a document'
  226. )
  227. available_statuses = marshmallow.fields.Nested(
  228. StatusSchema,
  229. many=True
  230. )
  231. class ContentMoveSchema(marshmallow.Schema):
  232. # TODO - G.M - 30-05-2018 - Read and apply this note
  233. # Note:
  234. # if the new workspace is different, then the backend
  235. # must check if the user is allowed to move to this workspace
  236. # (the user must be content manager of both workspaces)
  237. new_parent_id = marshmallow.fields.Int(
  238. example=42,
  239. description='id of the new parent content id.'
  240. )
  241. class ContentCreationSchema(marshmallow.Schema):
  242. label = marshmallow.fields.String(
  243. example='contract for client XXX',
  244. description='Title of the content to create'
  245. )
  246. content_type_slug = marshmallow.fields.String(
  247. example='htmlpage',
  248. validate=OneOf(CONTENT_DEFAULT_TYPE),
  249. )
  250. class ContentDigestSchema(marshmallow.Schema):
  251. id = marshmallow.fields.Int(example=6)
  252. slug = marshmallow.fields.Str(example='intervention-report-12')
  253. parent_id = marshmallow.fields.Int(
  254. example=34,
  255. allow_none=True,
  256. default=None
  257. )
  258. workspace_id = marshmallow.fields.Int(
  259. example=19,
  260. )
  261. label = marshmallow.fields.Str(example='Intervention Report 12')
  262. content_type_slug = marshmallow.fields.Str(
  263. example='htmlpage',
  264. validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
  265. )
  266. sub_content_type_slug = marshmallow.fields.List(
  267. marshmallow.fields.Str,
  268. description='list of content types allowed as sub contents. '
  269. 'This field is required for folder contents, '
  270. 'set it to empty list in other cases'
  271. )
  272. status_slug = marshmallow.fields.Str(
  273. example='closed-deprecated',
  274. validate=OneOf([status.slug for status in CONTENT_DEFAULT_STATUS]),
  275. description='this slug is found in content_type available statuses',
  276. )
  277. is_archived = marshmallow.fields.Bool(example=False)
  278. is_deleted = marshmallow.fields.Bool(example=False)
  279. show_in_ui = marshmallow.fields.Bool(
  280. example=True,
  281. description='if false, then do not show content in the treeview. '
  282. 'This may his maybe used for specific contents or '
  283. 'for sub-contents. Default is True. '
  284. 'In first version of the API, this field is always True',
  285. )