From 86f77e7b6d2b53e004b62fa01258e1b1f36568ca Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 18 Sep 2023 12:57:02 -0400 Subject: [PATCH] Adding support for Pydantic2 --- flask_mailing/config.py | 2 +- flask_mailing/connection.py | 2 +- flask_mailing/schemas.py | 16 +++++++--------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/flask_mailing/config.py b/flask_mailing/config.py index 6c3c60e..626bd3d 100644 --- a/flask_mailing/config.py +++ b/flask_mailing/config.py @@ -4,7 +4,7 @@ from flask.globals import current_app from jinja2 import Environment, FileSystemLoader -from pydantic import BaseSettings as Settings +from pydantic_settings import BaseSettings as Settings from pydantic import DirectoryPath, EmailStr, conint, validator from .errors import TemplateFolderDoesNotExist diff --git a/flask_mailing/connection.py b/flask_mailing/connection.py index b4b7a0b..d1259d5 100644 --- a/flask_mailing/connection.py +++ b/flask_mailing/connection.py @@ -1,5 +1,5 @@ import aiosmtplib -from pydantic import BaseSettings as Settings +from pydantic_settings import BaseSettings as Settings from .config import ConnectionConfig from .errors import ConnectionErrors, PydanticClassRequired diff --git a/flask_mailing/schemas.py b/flask_mailing/schemas.py index 6698531..8e83d96 100644 --- a/flask_mailing/schemas.py +++ b/flask_mailing/schemas.py @@ -34,23 +34,22 @@ class MultipartSubtypeEnum(Enum): class Message(BaseModel): - recipients: List["EmailStr"] + recipients: List[EmailStr] attachments: List[Union[FileStorage, Dict, str]] = [] subject: str = "" body: Optional[Union[str, list]] = None template_body: Optional[Union[list, dict]] = None template_params: Optional[Union[list, dict]] = None html: Optional[Union[str, List, Dict]] = None - cc: List["EmailStr"] = [] - bcc: List["EmailStr"] = [] - reply_to: List["EmailStr"] = [] + cc: List[EmailStr] = [] + bcc: List[EmailStr] = [] + reply_to: List[EmailStr] = [] charset: str = "utf-8" subtype: Optional[str] = None multipart_subtype: MultipartSubtypeEnum = MultipartSubtypeEnum.mixed - @validator("template_params") + @validator("template_params", pre=True, always=True) def validate_template_params(cls, value, values): - if values.get("template_body", None) is None: values["template_body"] = value return value @@ -129,8 +128,8 @@ def attach( self.attachments.append(fsob) return True - @validator("subtype") - def validate_subtype(cls, value, values, config, field): + @validator("subtype", pre=True, always=True) + def validate_subtype(cls, value, values): """Validate subtype field.""" if values.get("template_body"): return "html" @@ -139,7 +138,6 @@ def validate_subtype(cls, value, values, config, field): class Config: arbitrary_types_allowed = True - def validate_path(path): cur_dir = os.path.abspath(os.curdir) requested_path = os.path.abspath(os.path.relpath(path, start=cur_dir))