<commit_before>from aiohttp_json_api.errors import ResourceNotFound
from aiohttp_json_api.schema import BaseSchema, fields, relationships, sets
from aiohttp_json_api.common import Event

from examples.fantasy.models import Author, Store, Book, Series, Photo, Chapter


class CommonQueryMixin:
    async def fetch_resource(self, resource_id, context, **kwargs):
        async with self.app['db'].acquire() as connection:
            result = await self.resource_class.fetch_one(connection,
                                                         resource_id)

        if result is None:
            raise ResourceNotFound(type=self.type, id=resource_id)

        return result

    async def query_collection(self, context, **kwargs):
        async with self.app['db'].acquire() as connection:
            results = await self.resource_class.fetch_many(connection)

        return results.values()

    async def query_resource(self, resource_id, context, **kwargs):
        return await self.fetch_resource(resource_id, context, **kwargs)


class AuthorSchema(CommonQueryMixin, BaseSchema):
    resource_class = Author
    type = 'authors'

    name = fields.String()
    date_of_birth = fields.Date()
    date_of_death = fields.Date(allow_none=True)
    created_at = fields.DateTime()
    updated_at = fields.DateTime(allow_none=True)

    books = relationships.ToMany(foreign_types=('books',))
    photos = relationships.ToMany(foreign_types=('photos',), allow_none=True)

    async def delete_resource(self, resource_id, context, **kwargs):
        pass


class BookSchema(CommonQueryMixin, BaseSchema):
    resource_class = Book
    type = 'books'

    title = fields.String()
    date_published = fields.Date()
    created_at = fields.DateTime()
    updated_at = fields.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)

    async def delete_resource(self, resource_id, context, **kwargs):
        pass


class ChapterSchema(CommonQueryMixin, BaseSchema):
    resource_class = Chapter
    type = 'chapter'

    title = fields.String()
    ordering = fields.Integer()
    created_at = fields.DateTime()
    updated_at = fields.DateTime(allow_none=True)<commit_msg>Rename type of Chapter resources<commit_after>from aiohttp_json_api.errors import ResourceNotFound
from aiohttp_json_api.schema import BaseSchema, fields, relationships, sets
from aiohttp_json_api.common import Event

from examples.fantasy.models import Author, Store, Book, Series, Photo, Chapter


class CommonQueryMixin:
    async def fetch_resource(self, resource_id, context, **kwargs):
        async with self.app['db'].acquire() as connection:
            result = await self.resource_class.fetch_one(connection,
                                                         resource_id)

        if result is None:
            raise ResourceNotFound(type=self.type, id=resource_id)

        return result

    async def query_collection(self, context, **kwargs):
        async with self.app['db'].acquire() as connection:
            results = await self.resource_class.fetch_many(connection)

        return results.values()

    async def query_resource(self, resource_id, context, **kwargs):
        return await self.fetch_resource(resource_id, context, **kwargs)


class AuthorSchema(CommonQueryMixin, BaseSchema):
    resource_class = Author
    type = 'authors'

    name = fields.String()
    date_of_birth = fields.Date()
    date_of_death = fields.Date(allow_none=True)
    created_at = fields.DateTime()
    updated_at = fields.DateTime(allow_none=True)

    books = relationships.ToMany(foreign_types=('books',))
    photos = relationships.ToMany(foreign_types=('photos',), allow_none=True)

    async def delete_resource(self, resource_id, context, **kwargs):
        pass


class BookSchema(CommonQueryMixin, BaseSchema):
    resource_class = Book
    type = 'books'

    title = fields.String()
    date_published = fields.Date()
    created_at = fields.DateTime()
    updated_at = fields.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)

    async def delete_resource(self, resource_id, context, **kwargs):
        pass


class ChapterSchema(CommonQueryMixin, BaseSchema):
    resource_class = Chapter
    type = 'chapters'

    title = fields.String()
    ordering = fields.Integer()
    created_at = fields.DateTime()
    updated_at = fields.DateTime(allow_none=True)
{'fraction_non_alphanumeric': 0.10180623973727422, 'fraction_numerical': 0.0, 'mean_word_length': 3.744888023369036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2199295', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1329, 'n_words': 307}