schemas.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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
  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 FilterContentPathSchema(marshmallow.Schema):
  69. workspace_id = marshmallow.fields.Int(example=4, required=True)
  70. parent_id = workspace_id = marshmallow.fields.Int(
  71. example=2,
  72. default=None,
  73. description='allow to filter items in a folder.'
  74. ' If not set, then return all contents.'
  75. ' If set to 0, then return root contents.'
  76. ' If set to another value, return all contents'
  77. ' directly included in the folder parent_id'
  78. )
  79. show_archived = marshmallow.fields.Int(
  80. example=0,
  81. default=0,
  82. description='if set to 1, then show archived contents.'
  83. ' Default is 0 - hide archived content'
  84. )
  85. show_deleted = marshmallow.fields.Int(
  86. example=0,
  87. default=0,
  88. description='if set to 1, then show deleted contents.'
  89. ' Default is 0 - hide deleted content'
  90. )
  91. show_active = marshmallow.fields.Int(
  92. example=1,
  93. default=1,
  94. description='f set to 1, then show active contents. '
  95. 'Default is 1 - show active content.'
  96. ' Note: active content are content '
  97. 'that is neither archived nor deleted. '
  98. 'The reason for this parameter to exist is for example '
  99. 'to allow to show only archived documents'
  100. )
  101. ###
  102. class BasicAuthSchema(marshmallow.Schema):
  103. email = marshmallow.fields.Email(
  104. example='suri.cate@algoo.fr',
  105. required=True
  106. )
  107. password = marshmallow.fields.String(
  108. example='8QLa$<w',
  109. required=True,
  110. load_only=True,
  111. )
  112. class Meta:
  113. description = 'Entry for HTTP Basic Auth'
  114. @post_load
  115. def make_login(self, data):
  116. return LoginCredentials(**data)
  117. class LoginOutputHeaders(marshmallow.Schema):
  118. expire_after = marshmallow.fields.String()
  119. class NoContentSchema(marshmallow.Schema):
  120. class Meta:
  121. description = 'Empty Schema'
  122. pass
  123. class WorkspaceMenuEntrySchema(marshmallow.Schema):
  124. slug = marshmallow.fields.String(example='markdown-pages')
  125. label = marshmallow.fields.String(example='Markdown Documents')
  126. route = marshmallow.fields.String(
  127. example='/#/workspace/{workspace_id}/contents/?type=mardown-page',
  128. description='the route is the frontend route. '
  129. 'It may include workspace_id '
  130. 'which must be replaced on backend size '
  131. '(the route must be ready-to-use)'
  132. )
  133. icon = marshmallow.fields.String(
  134. example='file-text-o',
  135. description='CSS class of the icon. Example: file-o for using Fontawesome file-text-o icon', # nopep8
  136. )
  137. hexcolor = marshmallow.fields.String(
  138. example='#F0F9DC',
  139. description='Hexadecimal color of the entry.'
  140. )
  141. class Meta:
  142. description = 'Entry element of a workspace menu'
  143. class WorkspaceSchema(marshmallow.Schema):
  144. id = marshmallow.fields.Int(example=4)
  145. slug = marshmallow.fields.String(example='intranet')
  146. label = marshmallow.fields.String(example='Intranet')
  147. description = marshmallow.fields.String(example='All intranet data.')
  148. sidebar_entries = marshmallow.fields.Nested(
  149. WorkspaceMenuEntrySchema,
  150. many=True,
  151. )
  152. class Meta:
  153. description = 'Full workspace informations'
  154. class WorkspaceDigestSchema(marshmallow.Schema):
  155. id = marshmallow.fields.Int(example=4)
  156. label = marshmallow.fields.String(example='Intranet')
  157. sidebar_entries = marshmallow.fields.Nested(
  158. WorkspaceMenuEntrySchema,
  159. many=True,
  160. )
  161. class Meta:
  162. description = 'Digest of workspace informations'
  163. class WorkspaceMemberSchema(marshmallow.Schema):
  164. role_slug = marshmallow.fields.String(
  165. example='contributor',
  166. validate=OneOf(UserRoleInWorkspace.get_all_role_slug())
  167. )
  168. user_id = marshmallow.fields.Int(example=3)
  169. workspace_id = marshmallow.fields.Int(example=4)
  170. user = marshmallow.fields.Nested(
  171. UserSchema(only=('display_name', 'avatar_url'))
  172. )
  173. class Meta:
  174. description = 'Workspace Member information'
  175. class ApplicationConfigSchema(marshmallow.Schema):
  176. pass
  177. # TODO - G.M - 24-05-2018 - Set this
  178. class ApplicationSchema(marshmallow.Schema):
  179. label = marshmallow.fields.String(example='Calendar')
  180. slug = marshmallow.fields.String(example='calendar')
  181. icon = marshmallow.fields.String(
  182. example='file-o',
  183. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  184. )
  185. hexcolor = marshmallow.fields.String(
  186. example='#FF0000',
  187. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  188. )
  189. is_active = marshmallow.fields.Boolean(
  190. example=True,
  191. description='if true, the application is in use in the context',
  192. )
  193. config = marshmallow.fields.Nested(
  194. ApplicationConfigSchema,
  195. )
  196. class Meta:
  197. description = 'Tracim Application informations'
  198. class StatusSchema(marshmallow.Schema):
  199. slug = marshmallow.fields.String(
  200. example='open',
  201. description='the slug represents the type of status. '
  202. 'Statuses are open, closed-validated, closed-invalidated, closed-deprecated' # nopep8
  203. )
  204. global_status = marshmallow.fields.String(
  205. example='Open',
  206. description='global_status: open, closed',
  207. validate=OneOf([status.value for status in GlobalStatus]),
  208. )
  209. label = marshmallow.fields.String(example='Open')
  210. icon = marshmallow.fields.String(example='fa-check')
  211. hexcolor = marshmallow.fields.String(example='#0000FF')
  212. class ContentTypeSchema(marshmallow.Schema):
  213. slug = marshmallow.fields.String(
  214. example='pagehtml',
  215. validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
  216. )
  217. icon = marshmallow.fields.String(
  218. example='fa-file-text-o',
  219. description='CSS class of the icon. Example: file-o for using Fontawesome file-o icon', # nopep8
  220. )
  221. hexcolor = marshmallow.fields.String(
  222. example="#FF0000",
  223. description='HTML encoded color associated to the application. Example:#FF0000 for red' # nopep8
  224. )
  225. label = marshmallow.fields.String(
  226. example='Text Documents'
  227. )
  228. creation_label = marshmallow.fields.String(
  229. example='Write a document'
  230. )
  231. available_statuses = marshmallow.fields.Nested(
  232. StatusSchema,
  233. many=True
  234. )
  235. class ContentMoveSchema(marshmallow.Schema):
  236. # TODO - G.M - 30-05-2018 - Read and apply this note
  237. # Note:
  238. # if the new workspace is different, then the backend
  239. # must check if the user is allowed to move to this workspace
  240. # (the user must be content manager of both workspaces)
  241. new_parent_id = marshmallow.fields.Int(
  242. example=42,
  243. description='id of the new parent content id.'
  244. )
  245. class ContentCreationSchema(marshmallow.Schema):
  246. label = marshmallow.fields.String(
  247. example='contract for client XXX',
  248. description='Title of the content to create'
  249. )
  250. content_type_slug = marshmallow.fields.String(
  251. example='htmlpage',
  252. validate=OneOf(CONTENT_DEFAULT_TYPE),
  253. )
  254. class ContentDigestSchema(marshmallow.Schema):
  255. id = marshmallow.fields.Int(example=6)
  256. slug = marshmallow.fields.Str(example='intervention-report-12')
  257. parent_id = marshmallow.fields.Int(
  258. example=34,
  259. allow_none=True,
  260. default=None
  261. )
  262. workspace_id = marshmallow.fields.Int(
  263. example=19,
  264. )
  265. label = marshmallow.fields.Str(example='Intervention Report 12')
  266. content_type_slug = marshmallow.fields.Str(
  267. example='htmlpage',
  268. validate=OneOf([content.slug for content in CONTENT_DEFAULT_TYPE]),
  269. )
  270. sub_content_type_slug = marshmallow.fields.Nested(
  271. ContentTypeSchema(only=('slug',)),
  272. many=True,
  273. description='list of content types allowed as sub contents. '
  274. 'This field is required for folder contents, '
  275. 'set it to empty list in other cases'
  276. )
  277. status_slug = marshmallow.fields.Str(
  278. example='closed-deprecated',
  279. validate=OneOf([status.slug for status in CONTENT_DEFAULT_STATUS]),
  280. description='this slug is found in content_type available statuses',
  281. )
  282. is_archived = marshmallow.fields.Bool(example=False)
  283. is_deleted = marshmallow.fields.Bool(example=False)
  284. show_in_ui = marshmallow.fields.Bool(
  285. example=True,
  286. description='if false, then do not show content in the treeview. '
  287. 'This may his maybe used for specific contents or '
  288. 'for sub-contents. Default is True. '
  289. 'In first version of the API, this field is always True',
  290. )