schemas.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.context_models import LoginCredentials
  7. from tracim.models.data import UserRoleInWorkspace
  8. class ProfileSchema(marshmallow.Schema):
  9. id = marshmallow.fields.Int(dump_only=True, validate=OneOf(Profile._IDS))
  10. slug = marshmallow.fields.String(attribute='name', validate=OneOf(Profile._NAME))
  11. class UserSchema(marshmallow.Schema):
  12. user_id = marshmallow.fields.Int(dump_only=True)
  13. email = marshmallow.fields.Email(required=True)
  14. display_name = marshmallow.fields.String()
  15. created = marshmallow.fields.DateTime(format='iso8601')
  16. is_active = marshmallow.fields.Bool()
  17. # TODO - G.M - 17-04-2018 - Restrict timezone values
  18. timezone = marshmallow.fields.String()
  19. # TODO - G.M - 17-04-2018 - check this, relative url allowed ?
  20. caldav_url = marshmallow.fields.Url(
  21. allow_none=True,
  22. relative=True,
  23. attribute='calendar_url'
  24. )
  25. avatar_url = marshmallow.fields.Url(allow_none=True)
  26. profile = marshmallow.fields.Nested(
  27. ProfileSchema,
  28. many=False,
  29. )
  30. class UserIdPathSchema(marshmallow.Schema):
  31. user_id = marshmallow.fields.Int()
  32. class WorkspaceIdPathSchema(marshmallow.Schema):
  33. workspace_id = marshmallow.fields.Int()
  34. class BasicAuthSchema(marshmallow.Schema):
  35. email = marshmallow.fields.Email(required=True)
  36. password = marshmallow.fields.String(required=True, load_only=True)
  37. @post_load
  38. def make_login(self, data):
  39. return LoginCredentials(**data)
  40. class LoginOutputHeaders(marshmallow.Schema):
  41. expire_after = marshmallow.fields.String()
  42. class NoContentSchema(marshmallow.Schema):
  43. pass
  44. class WorkspaceMenuEntrySchema(marshmallow.Schema):
  45. slug = marshmallow.fields.String()
  46. label = marshmallow.fields.String()
  47. route = marshmallow.fields.String()
  48. hexcolor = marshmallow.fields.String()
  49. icon = marshmallow.fields.String()
  50. class WorkspaceSchema(marshmallow.Schema):
  51. id = marshmallow.fields.Int()
  52. slug = marshmallow.fields.String()
  53. label = marshmallow.fields.String()
  54. description = marshmallow.fields.String()
  55. sidebar_entries = marshmallow.fields.Nested(
  56. WorkspaceMenuEntrySchema,
  57. many=True,
  58. )
  59. class WorkspaceDigestSchema(marshmallow.Schema):
  60. id = marshmallow.fields.Int()
  61. label = marshmallow.fields.String()
  62. sidebar_entries = marshmallow.fields.Nested(
  63. WorkspaceMenuEntrySchema,
  64. many=True,
  65. )
  66. class WorkspaceMemberSchema(marshmallow.Schema):
  67. role_id = marshmallow.fields.Int(validate=OneOf(UserRoleInWorkspace.get_all_role_values())) # nopep8
  68. role_slug = marshmallow.fields.String(validate=OneOf(UserRoleInWorkspace.get_all_role_slug())) # nopep8
  69. user_id = marshmallow.fields.Int()
  70. workspace_id = marshmallow.fields.Int()
  71. user = marshmallow.fields.Nested(
  72. UserSchema(only=('display_name', 'avatar_url'))
  73. )
  74. class ApplicationConfigSchema(marshmallow.Schema):
  75. pass
  76. # TODO - G.M - 24-05-2018 - Set this
  77. class ApplicationSchema(marshmallow.Schema):
  78. label = marshmallow.fields.String()
  79. slug = marshmallow.fields.String()
  80. icon = marshmallow.fields.String()
  81. hexcolor = marshmallow.fields.String()
  82. is_active = marshmallow.fields.Boolean()
  83. config = marshmallow.fields.Nested(
  84. ApplicationConfigSchema,
  85. )