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

1"""Module for defining presenters for JSON:API author documents.""" 

2 

3from typing import Self 

4 

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 

8 

9from kwai_api.v1.auth.authors.schemas import ( 

10 AuthorAttributes, 

11 AuthorResource, 

12 AuthorsDocument, 

13) 

14 

15 

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.""" 

21 

22 async def present(self, use_case_result: IterableResult[AuthorEntity]) -> Self: 

23 self._document = AuthorsDocument( 

24 meta=Meta( 

25 count=use_case_result.count, 

26 offset=use_case_result.offset, 

27 limit=use_case_result.limit, 

28 ) 

29 ) 

30 async for author in use_case_result: 

31 self._document.data.append( 

32 AuthorResource( 

33 id=str(author.uuid), 

34 attributes=AuthorAttributes( 

35 name=author.name, 

36 remark=author.remark, 

37 active=author.active, 

38 editor=author.editor, 

39 ), 

40 meta=ResourceMeta( 

41 created_at=str(author.traceable_time.created_at), 

42 updated_at=None 

43 if author.traceable_time.updated_at 

44 else str(author.traceable_time.updated_at), 

45 ), 

46 ) 

47 ) 

48 

49 return self