schemas.py 17KB

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