https://dev.kronoz.win/r/marzneshin.git
1from typing import List, Optional
2
3from pydantic import ConfigDict, BaseModel, Field, computed_field
4
5
6class ServiceBase(BaseModel):
7 id: Optional[int] = None
8 name: Optional[str] = Field(None)
9 model_config = ConfigDict(from_attributes=True)
10
11
12from app.models.proxy import InboundBase
13from app.models.user import UserBase
14
15
16class Service(ServiceBase):
17 users: List[UserBase] = Field([])
18 inbounds: List[InboundBase] = Field([])
19
20
21class ServiceCreate(ServiceBase):
22 users: List[int] = Field([])
23 inbounds: List[int] = Field([])
24 model_config = ConfigDict(json_schema_extra={
25 "example": {
26 "name": "my service 1",
27 "inbounds": [1, 5, 9],
28 "users": [1, 2, 3]
29 }
30 })
31
32
33class ServiceModify(ServiceBase):
34 inbounds: Optional[List[int]] = Field(None)
35 model_config = ConfigDict(json_schema_extra={
36 "example": {
37 "id": 2,
38 "name": "my service 2",
39 "inbounds": [1, 2, 3]
40 }
41 })
42
43
44class ServiceResponse(Service):
45 id: int
46
47 @computed_field
48 @property
49 def user_ids(self) -> List[int]:
50 return [u.id for u in self.users]
51
52 @computed_field
53 @property
54 def inbound_ids(self) -> List[int]:
55 return [i.id for i in self.inbounds]
56
57 model_config = ConfigDict(from_attributes=True)