schemas.py 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. # coding=utf-8
  2. import marshmallow
  3. from marshmallow import post_load
  4. from marshmallow.validate import OneOf
  5. from marshmallow.validate import Range
  6. from tracim.lib.utils.utils import DATETIME_FORMAT
  7. from tracim.models.auth import Profile
  8. from tracim.models.contents import GlobalStatus
  9. from tracim.models.contents import open_status
  10. from tracim.models.contents import ContentTypeLegacy as ContentType
  11. from tracim.models.contents import ContentStatusLegacy as ContentStatus
  12. from tracim.models.context_models import ContentCreation
  13. from tracim.models.context_models import WorkspaceMemberInvitation
  14. from tracim.models.context_models import WorkspaceUpdate
  15. from tracim.models.context_models import RoleUpdate
  16. from tracim.models.context_models import CommentCreation
  17. from tracim.models.context_models import TextBasedContentUpdate
  18. from tracim.models.context_models import SetContentStatus
  19. from tracim.models.context_models import CommentPath
  20. from tracim.models.context_models import MoveParams
  21. from tracim.models.context_models import WorkspaceAndContentPath
  22. from tracim.models.context_models import WorkspaceAndUserPath
  23. from tracim.models.context_models import ContentFilter
  24. from tracim.models.context_models import LoginCredentials
  25. from tracim.models.data import UserRoleInWorkspace
  26. from tracim.models.data import ActionDescription
  27. class UserDigestSchema(marshmallow.Schema):
  28. """
  29. Simple user schema
  30. """
  31. user_id = marshmallow.fields.Int(dump_only=True, example=3)
  32. avatar_url = marshmallow.fields.Url(
  33. allow_none=True,
  34. example="/api/v2/assets/avatars/suri-cate.jpg",
  35. description="avatar_url is the url to the image file. "
  36. "If no avatar, then set it to null "
  37. "(and frontend will interpret this with a default avatar)",
  38. )
  39. public_name = marshmallow.fields.String(
  40. example='Suri Cate',
  41. )
  42. class UserSchema(UserDigestSchema):
  43. """
  44. Complete user schema
  45. """
  46. email = marshmallow.fields.Email(
  47. required=True,
  48. example='suri.cate@algoo.fr'
  49. )
  50. created = marshmallow.fields.DateTime(
  51. format=DATETIME_FORMAT,
  52. description='User account creation date',
  53. )
  54. is_active = marshmallow.fields.Bool(
  55. example=True,
  56. # TODO - G.M - Explains this value.
  57. )
  58. # TODO - G.M - 17-04-2018 - Restrict timezone values
  59. timezone = marshmallow.fields.String(
  60. example="Paris/Europe",
  61. )
  62. # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
  63. caldav_url = marshmallow.fields.Url(
  64. allow_none=True,
  65. relative=True,
  66. attribute='calendar_url',
  67. example="/api/v2/calendar/user/3.ics/",
  68. description="The url for calendar CalDAV direct access",
  69. )
  70. profile = marshmallow.fields.String(
  71. attribute='profile',
  72. validate=OneOf(Profile._NAME),
  73. example='managers',
  74. )
  75. class Meta:
  76. description = 'User account of Tracim'
  77. # Path Schemas
  78. class UserIdPathSchema(marshmallow.Schema):
  79. user_id = marshmallow.fields.Int(
  80. example=3,
  81. required=True,
  82. description='id of a valid user',
  83. validate=Range(min=1, error="Value must be greater than 0"),
  84. )
  85. class WorkspaceIdPathSchema(marshmallow.Schema):
  86. workspace_id = marshmallow.fields.Int(
  87. example=4,
  88. required=True,
  89. description='id of a valid workspace',
  90. validate=Range(min=1, error="Value must be greater than 0"),
  91. )
  92. class ContentIdPathSchema(marshmallow.Schema):
  93. content_id = marshmallow.fields.Int(
  94. example=6,
  95. required=True,
  96. description='id of a valid content',
  97. validate=Range(min=1, error="Value must be greater than 0"),
  98. )
  99. class WorkspaceAndUserIdPathSchema(
  100. UserIdPathSchema,
  101. WorkspaceIdPathSchema
  102. ):
  103. @post_load
  104. def make_path_object(self, data):
  105. return WorkspaceAndUserPath(**data)
  106. class WorkspaceAndContentIdPathSchema(
  107. WorkspaceIdPathSchema,
  108. ContentIdPathSchema
  109. ):
  110. @post_load
  111. def make_path_object(self, data):
  112. return WorkspaceAndContentPath(**data)
  113. class CommentsPathSchema(WorkspaceAndContentIdPathSchema):
  114. comment_id = marshmallow.fields.Int(
  115. example=6,
  116. description='id of a valid comment related to content content_id',
  117. required=True,
  118. validate=Range(min=1, error="Value must be greater than 0"),
  119. )
  120. @post_load
  121. def make_path_object(self, data):
  122. return CommentPath(**data)
  123. class FilterContentQuerySchema(marshmallow.Schema):
  124. parent_id = marshmallow.fields.Int(
  125. example=2,
  126. default=0,
  127. description='allow to filter items in a folder.'
  128. ' If not set, then return all contents.'
  129. ' If set to 0, then return root contents.'
  130. ' If set to another value, return all contents'
  131. ' directly included in the folder parent_id',
  132. validate=Range(min=0, error="Value must be positive or 0"),
  133. )
  134. show_archived = marshmallow.fields.Int(
  135. example=0,
  136. default=0,
  137. description='if set to 1, then show archived contents.'
  138. ' Default is 0 - hide archived content',
  139. validate=Range(min=0, max=1, error="Value must be 0 or 1"),
  140. )
  141. show_deleted = marshmallow.fields.Int(
  142. example=0,
  143. default=0,
  144. description='if set to 1, then show deleted contents.'
  145. ' Default is 0 - hide deleted content',
  146. validate=Range(min=0, max=1, error="Value must be 0 or 1"),
  147. )
  148. show_active = marshmallow.fields.Int(
  149. example=1,
  150. default=1,
  151. description='f set to 1, then show active contents. '
  152. 'Default is 1 - show active content.'
  153. ' Note: active content are content '
  154. 'that is neither archived nor deleted. '
  155. 'The reason for this parameter to exist is for example '
  156. 'to allow to show only archived documents',
  157. validate=Range(min=0, max=1, error="Value must be 0 or 1"),
  158. )
  159. content_type = marshmallow.fields.String(
  160. example=ContentType.Any,
  161. default=ContentType.Any,
  162. validate=OneOf(ContentType.allowed_type_values())
  163. )
  164. @post_load
  165. def make_content_filter(self, data):
  166. return ContentFilter(**data)
  167. ###
  168. class RoleUpdateSchema(marshmallow.Schema):
  169. role = marshmallow.fields.String(
  170. example='contributor',
  171. validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
  172. )
  173. @post_load
  174. def make_role(self, data):
  175. return RoleUpdate(**data)
  176. class WorkspaceMemberInviteSchema(RoleUpdateSchema):
  177. user_id = marshmallow.fields.Int(
  178. example=5,
  179. default=None,
  180. allow_none=True,
  181. )
  182. user_email_or_public_name = marshmallow.fields.String(
  183. example='suri@cate.fr',
  184. default=None,
  185. allow_none=True,
  186. )
  187. @post_load
  188. def make_role(self, data):
  189. return WorkspaceMemberInvitation(**data)
  190. class BasicAuthSchema(marshmallow.Schema):
  191. email = marshmallow.fields.Email(
  192. example='suri.cate@algoo.fr',
  193. required=True
  194. )
  195. password = marshmallow.fields.String(
  196. example='8QLa$<w',
  197. required=True,
  198. load_only=True,
  199. )
  200. class Meta:
  201. description = 'Entry for HTTP Basic Auth'
  202. @post_load
  203. def make_login(self, data):
  204. return LoginCredentials(**data)
  205. class LoginOutputHeaders(marshmallow.Schema):
  206. expire_after = marshmallow.fields.String()
  207. class WorkspaceModifySchema(marshmallow.Schema):
  208. label = marshmallow.fields.String(
  209. example='My Workspace',
  210. )
  211. description = marshmallow.fields.String(
  212. example='A super description of my workspace.',
  213. )
  214. @post_load
  215. def make_workspace_modifications(self, data):
  216. return WorkspaceUpdate(**data)
  217. class WorkspaceCreationSchema(WorkspaceModifySchema):
  218. pass
  219. class NoContentSchema(marshmallow.Schema):
  220. class Meta:
  221. description = 'Empty Schema'
  222. pass
  223. class WorkspaceMenuEntrySchema(marshmallow.Schema):
  224. slug = marshmallow.fields.String(example='markdown-pages')
  225. label = marshmallow.fields.String(example='Markdown Documents')
  226. route = marshmallow.fields.String(
  227. example='/#/workspace/{workspace_id}/contents/?type=mardown-page',
  228. description='the route is the frontend route. '
  229. 'It may include workspace_id '
  230. 'which must be replaced on backend size '
  231. '(the route must be ready-to-use)'
  232. )
  233. fa_icon = marshmallow.fields.String(
  234. example='file-text-o',
  235. description='CSS class of the icon. Example: file-o for using Fontawesome file-text-o icon', # nopep8
  236. )
  237. hexcolor = marshmallow.fields.String(
  238. example='#F0F9DC',
  239. description='Hexadecimal color of the entry.'
  240. )
  241. class Meta:
  242. description = 'Entry element of a workspace menu'
  243. class WorkspaceDigestSchema(marshmallow.Schema):
  244. workspace_id = marshmallow.fields.Int(
  245. example=4,
  246. validate=Range(min=1, error="Value must be greater than 0"),
  247. )
  248. slug = marshmallow.fields.String(example='intranet')
  249. label = marshmallow.fields.String(example='Intranet')
  250. sidebar_entries = marshmallow.fields.Nested(
  251. WorkspaceMenuEntrySchema,
  252. many=True,
  253. )
  254. class Meta:
  255. description = 'Digest of workspace informations'
  256. class WorkspaceSchema(WorkspaceDigestSchema):
  257. description = marshmallow.fields.String(example='All intranet data.')
  258. class Meta:
  259. description = 'Full workspace informations'
  260. class WorkspaceMemberSchema(marshmallow.Schema):
  261. role = marshmallow.fields.String(
  262. example='contributor',
  263. validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
  264. )
  265. user_id = marshmallow.fields.Int(
  266. example=3,
  267. validate=Range(min=1, error="Value must be greater than 0"),
  268. )
  269. workspace_id = marshmallow.fields.Int(
  270. example=4,
  271. validate=Range(min=1, error="Value must be greater than 0"),
  272. )
  273. user = marshmallow.fields.Nested(
  274. UserDigestSchema()
  275. )
  276. workspace = marshmallow.fields.Nested(
  277. WorkspaceDigestSchema(exclude=('sidebar_entries',))
  278. )
  279. is_active = marshmallow.fields.Bool()
  280. class Meta:
  281. description = 'Workspace Member information'
  282. class WorkspaceMemberCreationSchema(WorkspaceMemberSchema):
  283. newly_created = marshmallow.fields.Bool(
  284. exemple=False,
  285. description='Is the user completely new '
  286. '(and account was just created) or not ?',
  287. )
  288. email_sent = marshmallow.fields.Bool(
  289. exemple=False,
  290. description='Has an email been sent to user to inform him about '
  291. 'this new workspace registration and eventually his account'
  292. 'creation'
  293. )
  294. class ApplicationConfigSchema(marshmallow.Schema):
  295. pass
  296. # TODO - G.M - 24-05-2018 - Set this
  297. class ApplicationSchema(marshmallow.Schema):
  298. label = marshmallow.fields.String(example='Calendar')
  299. slug = marshmallow.fields.String(example='calendar')
  300. fa_icon = marshmallow.fields.String(
  301. example='file-o',
  302. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  303. )
  304. hexcolor = marshmallow.fields.String(
  305. example='#FF0000',
  306. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  307. )
  308. is_active = marshmallow.fields.Boolean(
  309. example=True,
  310. description='if true, the application is in use in the context',
  311. )
  312. config = marshmallow.fields.Nested(
  313. ApplicationConfigSchema,
  314. )
  315. class Meta:
  316. description = 'Tracim Application informations'
  317. class StatusSchema(marshmallow.Schema):
  318. slug = marshmallow.fields.String(
  319. example='open',
  320. description='the slug represents the type of status. '
  321. 'Statuses are open, closed-validated, closed-invalidated, closed-deprecated' # nopep8
  322. )
  323. global_status = marshmallow.fields.String(
  324. example='open',
  325. description='global_status: open, closed',
  326. validate=OneOf([status.value for status in GlobalStatus]),
  327. )
  328. label = marshmallow.fields.String(example='Open')
  329. fa_icon = marshmallow.fields.String(example='fa-check')
  330. hexcolor = marshmallow.fields.String(example='#0000FF')
  331. class ContentTypeSchema(marshmallow.Schema):
  332. slug = marshmallow.fields.String(
  333. example='pagehtml',
  334. validate=OneOf(ContentType.allowed_types()),
  335. )
  336. fa_icon = marshmallow.fields.String(
  337. example='fa-file-text-o',
  338. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  339. )
  340. hexcolor = marshmallow.fields.String(
  341. example="#FF0000",
  342. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  343. )
  344. label = marshmallow.fields.String(
  345. example='Text Documents'
  346. )
  347. creation_label = marshmallow.fields.String(
  348. example='Write a document'
  349. )
  350. available_statuses = marshmallow.fields.Nested(
  351. StatusSchema,
  352. many=True
  353. )
  354. class ContentMoveSchema(marshmallow.Schema):
  355. # TODO - G.M - 30-05-2018 - Read and apply this note
  356. # Note:
  357. # if the new workspace is different, then the backend
  358. # must check if the user is allowed to move to this workspace
  359. # (the user must be content manager of both workspaces)
  360. new_parent_id = marshmallow.fields.Int(
  361. example=42,
  362. description='id of the new parent content id.',
  363. allow_none=True,
  364. required=True,
  365. validate=Range(min=0, error="Value must be positive or 0"),
  366. )
  367. new_workspace_id = marshmallow.fields.Int(
  368. example=2,
  369. description='id of the new workspace id.',
  370. required=True,
  371. validate=Range(min=1, error="Value must be greater than 0"),
  372. )
  373. @post_load
  374. def make_move_params(self, data):
  375. return MoveParams(**data)
  376. class ContentCreationSchema(marshmallow.Schema):
  377. label = marshmallow.fields.String(
  378. example='contract for client XXX',
  379. description='Title of the content to create'
  380. )
  381. content_type = marshmallow.fields.String(
  382. example='html-documents',
  383. validate=OneOf(ContentType.allowed_types_for_folding()), # nopep8
  384. )
  385. parent_id = marshmallow.fields.Integer(
  386. example=35,
  387. description='content_id of parent content, if content should be placed in a folder, this should be folder content_id.'
  388. )
  389. @post_load
  390. def make_content_filter(self, data):
  391. return ContentCreation(**data)
  392. class ContentDigestSchema(marshmallow.Schema):
  393. content_id = marshmallow.fields.Int(
  394. example=6,
  395. validate=Range(min=1, error="Value must be greater than 0"),
  396. )
  397. slug = marshmallow.fields.Str(example='intervention-report-12')
  398. parent_id = marshmallow.fields.Int(
  399. example=34,
  400. allow_none=True,
  401. default=None,
  402. validate=Range(min=0, error="Value must be positive or 0"),
  403. )
  404. workspace_id = marshmallow.fields.Int(
  405. example=19,
  406. validate=Range(min=1, error="Value must be greater than 0"),
  407. )
  408. label = marshmallow.fields.Str(example='Intervention Report 12')
  409. content_type = marshmallow.fields.Str(
  410. example='html-documents',
  411. validate=OneOf(ContentType.allowed_types()),
  412. )
  413. sub_content_types = marshmallow.fields.List(
  414. marshmallow.fields.String(
  415. example='html-content',
  416. validate=OneOf(ContentType.allowed_types())
  417. ),
  418. description='list of content types allowed as sub contents. '
  419. 'This field is required for folder contents, '
  420. 'set it to empty list in other cases'
  421. )
  422. status = marshmallow.fields.Str(
  423. example='closed-deprecated',
  424. validate=OneOf(ContentStatus.allowed_values()),
  425. description='this slug is found in content_type available statuses',
  426. default=open_status
  427. )
  428. is_archived = marshmallow.fields.Bool(example=False, default=False)
  429. is_deleted = marshmallow.fields.Bool(example=False, default=False)
  430. show_in_ui = marshmallow.fields.Bool(
  431. example=True,
  432. description='if false, then do not show content in the treeview. '
  433. 'This may his maybe used for specific contents or '
  434. 'for sub-contents. Default is True. '
  435. 'In first version of the API, this field is always True',
  436. )
  437. #####
  438. # Content
  439. #####
  440. class ContentSchema(ContentDigestSchema):
  441. current_revision_id = marshmallow.fields.Int(example=12)
  442. created = marshmallow.fields.DateTime(
  443. format=DATETIME_FORMAT,
  444. description='Content creation date',
  445. )
  446. author = marshmallow.fields.Nested(UserDigestSchema)
  447. modified = marshmallow.fields.DateTime(
  448. format=DATETIME_FORMAT,
  449. description='date of last modification of content',
  450. )
  451. last_modifier = marshmallow.fields.Nested(UserDigestSchema)
  452. class TextBasedDataAbstractSchema(marshmallow.Schema):
  453. raw_content = marshmallow.fields.String(
  454. description='Content of the object, may be raw text or <b>html</b> for example' # nopep8
  455. )
  456. class TextBasedContentSchema(ContentSchema, TextBasedDataAbstractSchema):
  457. pass
  458. #####
  459. # Revision
  460. #####
  461. class RevisionSchema(ContentDigestSchema):
  462. comment_ids = marshmallow.fields.List(
  463. marshmallow.fields.Int(
  464. example=4,
  465. validate=Range(min=1, error="Value must be greater than 0"),
  466. )
  467. )
  468. revision_id = marshmallow.fields.Int(
  469. example=12,
  470. validate=Range(min=1, error="Value must be greater than 0"),
  471. )
  472. revision_type = marshmallow.fields.String(
  473. example=ActionDescription.CREATION,
  474. validate=OneOf(ActionDescription.allowed_values()),
  475. )
  476. created = marshmallow.fields.DateTime(
  477. format=DATETIME_FORMAT,
  478. description='Content creation date',
  479. )
  480. author = marshmallow.fields.Nested(UserDigestSchema)
  481. class TextBasedRevisionSchema(RevisionSchema, TextBasedDataAbstractSchema):
  482. pass
  483. class CommentSchema(marshmallow.Schema):
  484. content_id = marshmallow.fields.Int(
  485. example=6,
  486. validate=Range(min=1, error="Value must be greater than 0"),
  487. )
  488. parent_id = marshmallow.fields.Int(
  489. example=34,
  490. validate=Range(min=0, error="Value must be positive or 0"),
  491. )
  492. raw_content = marshmallow.fields.String(
  493. example='<p>This is just an html comment !</p>'
  494. )
  495. author = marshmallow.fields.Nested(UserDigestSchema)
  496. created = marshmallow.fields.DateTime(
  497. format=DATETIME_FORMAT,
  498. description='comment creation date',
  499. )
  500. class SetCommentSchema(marshmallow.Schema):
  501. raw_content = marshmallow.fields.String(
  502. example='<p>This is just an html comment !</p>'
  503. )
  504. @post_load()
  505. def create_comment(self, data):
  506. return CommentCreation(**data)
  507. class ContentModifyAbstractSchema(marshmallow.Schema):
  508. label = marshmallow.fields.String(
  509. required=True,
  510. example='contract for client XXX',
  511. description='New title of the content'
  512. )
  513. class TextBasedContentModifySchema(ContentModifyAbstractSchema, TextBasedDataAbstractSchema): # nopep8
  514. @post_load
  515. def text_based_content_update(self, data):
  516. return TextBasedContentUpdate(**data)
  517. class SetContentStatusSchema(marshmallow.Schema):
  518. status = marshmallow.fields.Str(
  519. example='closed-deprecated',
  520. validate=OneOf(ContentStatus.allowed_values()),
  521. description='this slug is found in content_type available statuses',
  522. default=open_status,
  523. required=True,
  524. )
  525. @post_load
  526. def set_status(self, data):
  527. return SetContentStatus(**data)