Coverage for packages/kwai-core/src/kwai_core/db/rows.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module that defines common table classes.""" 

2 

3from dataclasses import dataclass 

4from datetime import datetime 

5 

6from kwai_core.db.table_row import TableRow 

7from kwai_core.domain.value_objects.identifier import IntIdentifier 

8from kwai_core.domain.value_objects.name import Name 

9from kwai_core.domain.value_objects.owner import Owner 

10from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText 

11from kwai_core.domain.value_objects.timestamp import Timestamp 

12from kwai_core.domain.value_objects.traceable_time import TraceableTime 

13from kwai_core.domain.value_objects.unique_id import UniqueId 

14 

15 

16@dataclass(kw_only=True, frozen=True, slots=True) 

17class OwnerRow(TableRow): 

18 """Represent the owner data.""" 

19 

20 __table_name__ = "users" 

21 

22 id: int 

23 uuid: str 

24 first_name: str 

25 last_name: str 

26 

27 def create_owner(self) -> Owner: 

28 """Create an Author value object from row data.""" 

29 return Owner( 

30 id=IntIdentifier(self.id), 

31 uuid=UniqueId.create_from_string(self.uuid), 

32 name=Name(first_name=self.first_name, last_name=self.last_name), 

33 ) 

34 

35 

36@dataclass(kw_only=True, frozen=True, slots=True) 

37class TextRow: 

38 """Represent a row for a content table. 

39 

40 Attributes: 

41 locale: The code of the locale of the text 

42 format: The format of the text (md = markdown, html, ...) 

43 title: The title of the text 

44 content: The long content of the text 

45 summary: A summary of the text 

46 user_id: The id of the author 

47 created_at: the timestamp of creation 

48 updated_at: the timestamp of the last modification 

49 """ 

50 

51 locale: str 

52 format: str 

53 title: str 

54 content: str 

55 summary: str 

56 user_id: int 

57 created_at: datetime 

58 updated_at: datetime | None 

59 

60 def create_text(self, author: Owner) -> LocaleText: 

61 """Create a LocaleText value object from a row. 

62 

63 Args: 

64 author: The author of the text. 

65 """ 

66 return LocaleText( 

67 locale=Locale(self.locale), 

68 format=DocumentFormat(self.format), 

69 title=self.title, 

70 content=self.content, 

71 summary=self.summary, 

72 author=author, 

73 traceable_time=TraceableTime( 

74 created_at=Timestamp.create_utc(self.created_at), 

75 updated_at=Timestamp.create_utc(self.updated_at), 

76 ), 

77 )