Coverage for apps/kwai-api/src/kwai_api/v1/auth/authors/presenters.py: 100%
11 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for defining presenters for JSON:API author documents."""
3from typing import Self
5from kwai_bc_portal.domain.author import AuthorEntity
6from kwai_core.domain.presenter import AsyncPresenter, IterableResult
7from kwai_core.json_api import JsonApiPresenter, Meta, ResourceMeta
9from kwai_api.v1.auth.authors.schemas import (
10 AuthorAttributes,
11 AuthorResource,
12 AuthorsDocument,
13)
16class JsonApiAuthorsPresenter(
17 JsonApiPresenter[AuthorsDocument],
18 AsyncPresenter[IterableResult[AuthorEntity]],
19):
20 """A presenter that transform an iterable list of author entities into a JSON:API document."""
22 async def present(self, result: IterableResult[AuthorEntity]) -> Self:
23 self._document = AuthorsDocument(
24 meta=Meta(count=result.count, offset=result.offset, limit=result.limit)
25 )
26 async for author in result.iterator:
27 self._document.data.append(
28 AuthorResource(
29 id=str(author.uuid),
30 attributes=AuthorAttributes(
31 name=author.name,
32 remark=author.remark,
33 active=author.active,
34 editor=author.editor,
35 ),
36 meta=ResourceMeta(
37 created_at=str(author.traceable_time.created_at),
38 updated_at=None
39 if author.traceable_time.updated_at
40 else str(author.traceable_time.updated_at),
41 ),
42 )
43 )
45 return self