Skip to content

Commit

Permalink
Merge pull request #55 from CharlesTWood/development
Browse files Browse the repository at this point in the history
Adding support for Pydantic2
  • Loading branch information
marktennyson authored Sep 19, 2023
2 parents a618473 + c8db6aa commit 627c1ac
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion flask_mailing/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flask_mailing/connection.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 7 additions & 9 deletions flask_mailing/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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))
Expand Down
4 changes: 0 additions & 4 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from pydantic import EmailError

from flask_mailing.utils.errors import DBProvaiderError

Expand Down Expand Up @@ -40,8 +39,5 @@ async def test_default_checker(default_checker):

assert default_checker.validate_email(email) is True

with pytest.raises(EmailError):
default_checker.validate_email("test#mail.com")

with pytest.raises(DBProvaiderError):
await default_checker.close_connections()

0 comments on commit 627c1ac

Please sign in to comment.