Coverage for src/kwai/api/schemas/application.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Schemas for an application resource."""
3from types import NoneType
5from pydantic import BaseModel
7from kwai.api.schemas.resources import ApplicationResourceIdentifier
8from kwai.core.json_api import Document, ResourceData, ResourceMeta
9from kwai.modules.portal.applications.application import ApplicationEntity
12class ApplicationBaseAttributes(BaseModel):
13 """Common attributes of an application JSON:API resource."""
15 name: str
16 title: str
19class ApplicationAttributes(ApplicationBaseAttributes):
20 """Attributes for an application JSON:API resource."""
22 short_description: str
23 description: str
24 remark: str
25 news: bool
26 events: bool
27 pages: bool
28 weight: int
31class ApplicationResource(
32 ApplicationResourceIdentifier, ResourceData[ApplicationAttributes, NoneType]
33):
34 """A JSON:API resource for an application."""
37class ApplicationDocument(Document[ApplicationResource, NoneType]):
38 """A JSON:API document for one or more applications."""
40 @classmethod
41 def create(cls, application: ApplicationEntity) -> "ApplicationDocument":
42 """Create a document from an application entity."""
43 return ApplicationDocument(
44 data=ApplicationResource(
45 id=str(application.id),
46 meta=ResourceMeta(
47 created_at=str(application.traceable_time.created_at),
48 updated_at=str(application.traceable_time.created_at),
49 ),
50 attributes=ApplicationAttributes(
51 name=application.name,
52 title=application.title,
53 short_description=application.short_description,
54 description=application.description,
55 remark=application.remark,
56 news=application.can_contain_news,
57 events=application.can_contain_events,
58 pages=application.can_contain_pages,
59 weight=application.weight,
60 ),
61 )
62 )