Files
Marzban/app/models/user_template.py
Mohammad ea6a3d2eb6 Migrate to Pydantic V2 (#1495)
* chore: update pydantic to version 2.10.2 and refactor model validators

* refactor: simplify field validators by removing unnecessary pre and always flags

* remove typing_extensions==4.9.0 from requirements.txt

* refactor: remove allow_reuse flag from status field validator

* refactor: simplify field validators by removing pre and always flags

* refactor: update user model imports and enhance account class with abstract method

* refactor: update model_config to use dictionary format in Admin and SubscriptionUserResponse classes

* fix typo in UserDataResetByNext

* change pre=True to mode="before"

* refactor: update validation methods and model configuration in User and Proxy classes

* change pre=False with mode="after"

* Migrated to Pydantic V2

* fix: custom subscriptions not workong

* some small changes

* add missing properties to example schema

* replace from_orm with model_validate

---------

Co-authored-by: MahdiButcher <madibutchercoding@gmail.com>
Co-authored-by: Mahdi Butcher <MahdiButcherCoding@gmail.com>
2024-12-09 21:56:24 +03:30

66 lines
2.1 KiB
Python

from typing import Dict, List, Optional
from pydantic import field_validator, ConfigDict, BaseModel, Field
from app import xray
from app.models.proxy import ProxyTypes
class UserTemplate(BaseModel):
name: Optional[str] = Field(None, nullable=True)
data_limit: Optional[int] = Field(
ge=0, default=None, description="data_limit can be 0 or greater"
)
expire_duration: Optional[int] = Field(
ge=0, default=None, description="expire_duration can be 0 or greater in seconds"
)
username_prefix: Optional[str] = Field(max_length=20, min_length=1, default=None)
username_suffix: Optional[str] = Field(max_length=20, min_length=1, default=None)
inbounds: Dict[ProxyTypes, List[str]] = {}
class UserTemplateCreate(UserTemplate):
model_config = ConfigDict(json_schema_extra={
"example": {
"name": "my template 1",
"username_prefix": None,
"username_suffix": None,
"inbounds": {"vmess": ["VMESS_INBOUND"], "vless": ["VLESS_INBOUND"]},
"data_limit": 0,
"expire_duration": 0,
}
})
class UserTemplateModify(UserTemplate):
model_config = ConfigDict(json_schema_extra={
"example": {
"name": "my template 1",
"username_prefix": None,
"username_suffix": None,
"inbounds": {"vmess": ["VMESS_INBOUND"], "vless": ["VLESS_INBOUND"]},
"data_limit": 0,
"expire_duration": 0,
}
})
class UserTemplateResponse(UserTemplate):
id: int
@field_validator("inbounds", mode="before")
@classmethod
def validate_inbounds(cls, v):
final = {}
inbound_tags = [i.tag for i in v]
for protocol, inbounds in xray.config.inbounds_by_protocol.items():
for inbound in inbounds:
if inbound["tag"] in inbound_tags:
if protocol in final:
final[protocol].append(inbound["tag"])
else:
final[protocol] = [inbound["tag"]]
return final
model_config = ConfigDict(from_attributes=True)