# coding=utf-8 import marshmallow from marshmallow import post_load from marshmallow.validate import OneOf from marshmallow.validate import Range from tracim.lib.utils.utils import DATETIME_FORMAT from tracim.models.auth import Profile from tracim.models.contents import GlobalStatus from tracim.models.contents import open_status from tracim.models.contents import ContentTypeLegacy as ContentType from tracim.models.contents import ContentStatusLegacy as ContentStatus from tracim.models.context_models import ContentCreation from tracim.models.context_models import WorkspaceMemberInvitation from tracim.models.context_models import WorkspaceUpdate from tracim.models.context_models import RoleUpdate from tracim.models.context_models import CommentCreation from tracim.models.context_models import TextBasedContentUpdate from tracim.models.context_models import SetContentStatus from tracim.models.context_models import CommentPath from tracim.models.context_models import MoveParams from tracim.models.context_models import WorkspaceAndContentPath from tracim.models.context_models import WorkspaceAndUserPath from tracim.models.context_models import ContentFilter from tracim.models.context_models import LoginCredentials from tracim.models.data import UserRoleInWorkspace from tracim.models.data import ActionDescription class UserDigestSchema(marshmallow.Schema): """ Simple user schema """ user_id = marshmallow.fields.Int(dump_only=True, example=3) avatar_url = marshmallow.fields.Url( allow_none=True, example="/api/v2/assets/avatars/suri-cate.jpg", description="avatar_url is the url to the image file. " "If no avatar, then set it to null " "(and frontend will interpret this with a default avatar)", ) public_name = marshmallow.fields.String( example='Suri Cate', ) class UserSchema(UserDigestSchema): """ Complete user schema """ email = marshmallow.fields.Email( required=True, example='suri.cate@algoo.fr' ) created = marshmallow.fields.DateTime( format=DATETIME_FORMAT, description='User account creation date', ) is_active = marshmallow.fields.Bool( example=True, # TODO - G.M - Explains this value. ) # TODO - G.M - 17-04-2018 - Restrict timezone values timezone = marshmallow.fields.String( example="Paris/Europe", ) # TODO - G.M - 17-04-2018 - check this, relative url allowed ? caldav_url = marshmallow.fields.Url( allow_none=True, relative=True, attribute='calendar_url', example="/api/v2/calendar/user/3.ics/", description="The url for calendar CalDAV direct access", ) profile = marshmallow.fields.String( attribute='profile', validate=OneOf(Profile._NAME), example='managers', ) class Meta: description = 'User account of Tracim' # Path Schemas class UserIdPathSchema(marshmallow.Schema): user_id = marshmallow.fields.Int( example=3, required=True, description='id of a valid user', validate=Range(min=1, error="Value must be greater than 0"), ) class WorkspaceIdPathSchema(marshmallow.Schema): workspace_id = marshmallow.fields.Int( example=4, required=True, description='id of a valid workspace', validate=Range(min=1, error="Value must be greater than 0"), ) class ContentIdPathSchema(marshmallow.Schema): content_id = marshmallow.fields.Int( example=6, required=True, description='id of a valid content', validate=Range(min=1, error="Value must be greater than 0"), ) class WorkspaceAndUserIdPathSchema( UserIdPathSchema, WorkspaceIdPathSchema ): @post_load def make_path_object(self, data): return WorkspaceAndUserPath(**data) class WorkspaceAndContentIdPathSchema( WorkspaceIdPathSchema, ContentIdPathSchema ): @post_load def make_path_object(self, data): return WorkspaceAndContentPath(**data) class CommentsPathSchema(WorkspaceAndContentIdPathSchema): comment_id = marshmallow.fields.Int( example=6, description='id of a valid comment related to content content_id', required=True, validate=Range(min=1, error="Value must be greater than 0"), ) @post_load def make_path_object(self, data): return CommentPath(**data) class FilterContentQuerySchema(marshmallow.Schema): parent_id = marshmallow.fields.Int( example=2, default=0, description='allow to filter items in a folder.' ' If not set, then return all contents.' ' If set to 0, then return root contents.' ' If set to another value, return all contents' ' directly included in the folder parent_id', validate=Range(min=0, error="Value must be positive or 0"), ) show_archived = marshmallow.fields.Int( example=0, default=0, description='if set to 1, then show archived contents.' ' Default is 0 - hide archived content', validate=Range(min=0, max=1, error="Value must be 0 or 1"), ) show_deleted = marshmallow.fields.Int( example=0, default=0, description='if set to 1, then show deleted contents.' ' Default is 0 - hide deleted content', validate=Range(min=0, max=1, error="Value must be 0 or 1"), ) show_active = marshmallow.fields.Int( example=1, default=1, description='f set to 1, then show active contents. ' 'Default is 1 - show active content.' ' Note: active content are content ' 'that is neither archived nor deleted. ' 'The reason for this parameter to exist is for example ' 'to allow to show only archived documents', validate=Range(min=0, max=1, error="Value must be 0 or 1"), ) @post_load def make_content_filter(self, data): return ContentFilter(**data) ### class RoleUpdateSchema(marshmallow.Schema): role = marshmallow.fields.String( example='contributor', validate=OneOf(UserRoleInWorkspace.get_all_role_slug()) ) @post_load def make_role(self, data): return RoleUpdate(**data) class WorkspaceMemberInviteSchema(RoleUpdateSchema): user_id = marshmallow.fields.Int( example=5, default=None, allow_none=True, ) user_email_or_public_name = marshmallow.fields.String( example='suri@cate.fr', default=None, allow_none=True, ) @post_load def make_role(self, data): return WorkspaceMemberInvitation(**data) class BasicAuthSchema(marshmallow.Schema): email = marshmallow.fields.Email( example='suri.cate@algoo.fr', required=True ) password = marshmallow.fields.String( example='8QLa$