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(
25 json_schema_extra={
26 "example": {
27 "name": "my service 1",
28 "inbounds": [1, 5, 9],
29 "users": [1, 2, 3],
30 }
31 }
32 )
33
34
35class ServiceModify(ServiceBase):
36 inbounds: Optional[List[int]] = Field(None)
37 model_config = ConfigDict(
38 json_schema_extra={
39 "example": {"id": 2, "name": "my service 2", "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)