class BookSchema(BaseSchema): title = attributes.String() date_published = attributes.Date() created_at = attributes.DateTime() updated_at = attributes.DateTime(allow_none=True) author = relationships.ToOne(foreign_types=('author', )) series = relationships.ToOne(foreign_types=('series', ), allow_none=True) chapters = relationships.ToMany(foreign_types=('chapters', )) photos = relationships.ToMany(foreign_types=('photos', ), allow_none=True) class Options: resource_cls = Book resource_type = 'books'
class CommentSchema(BaseSchema): body = attributes.String(required=Event.CREATE, max_length=1024) author = relationships.ToOne(required=Event.CREATE, foreign_types=('people', )) class Options: resource_cls = Comment resource_type = 'comments' @sets('author') async def set_author(self, field, resource, data, sp, context=None, **kwargs): rid = self.ctx.registry.ensure_identifier(data['data']) storage = self.ctx.app['storage'] author = storage[rid.type].get(rid.id) if author is None: raise ResourceNotFound(rid.type, rid.id) logger.debug('Set author of %r to %r.', resource, author) resource.author = author return resource
class ArticleSchema(BaseSchema): title = attributes.String(required=Event.CREATE, max_length=256) author = relationships.ToOne(required=Event.CREATE, foreign_types=('people', )) comments = relationships.ToMany(foreign_types=('comments', )) class Options: resource_cls = Article resource_type = 'articles'
class ChapterSchema(BaseSchema): title = attributes.String() ordering = attributes.Integer() created_at = attributes.DateTime() updated_at = attributes.DateTime(allow_none=True) book = relationships.ToOne(foreign_types=('books', )) class Options: resource_cls = Chapter resource_type = 'chapters'
class PhotoSchema(BaseSchema): title = attributes.String() uri = attributes.String() created_at = attributes.DateTime() updated_at = attributes.DateTime(allow_none=True) imageable = relationships.ToOne(foreign_types=('authors', 'books', 'series') # polymorphic ) class Options: resource_cls = Photo resource_type = 'photos'