schemas.py 19KB

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