Skip to content

Commit

Permalink
fixed format
Browse files Browse the repository at this point in the history
  • Loading branch information
marktennyson committed Sep 19, 2023
1 parent f6db28f commit 2679e3e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
1 change: 0 additions & 1 deletion flask_mailing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ async def simple_send():
from .schemas import Message as Message
from .schemas import MultipartSubtypeEnum as MultipartSubtypeEnum


__author__ = "aniketsarkar@yahoo.com"


Expand Down
2 changes: 1 addition & 1 deletion flask_mailing/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from flask.globals import current_app
from jinja2 import Environment, FileSystemLoader
from pydantic_settings import BaseSettings as Settings
from pydantic import DirectoryPath, EmailStr, conint, field_validator
from pydantic_settings import BaseSettings as Settings

from .errors import TemplateFolderDoesNotExist

Expand Down
1 change: 0 additions & 1 deletion flask_mailing/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Connection:
"""

def __init__(self, settings: ConnectionConfig):

if not issubclass(settings.__class__, Settings):
raise PydanticClassRequired(
"""Email configuruation should be provided from ConnectionConfig class, check example below:
Expand Down
34 changes: 15 additions & 19 deletions flask_mailing/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


class _MailMixin:

name = "Flask Mailing"
version = ".".join([str(v) for v in version_info])

Expand Down Expand Up @@ -58,12 +57,10 @@ class Mail(_MailMixin):
"""

def __init__(self, app: t.Optional["Flask"] = None) -> None:

if app is not None:
self.init_app(app)

def init_app(self, app: "Flask") -> None:

self.config = ConnectionConfig(
MAIL_USERNAME=app.config.get("MAIL_USERNAME"),
MAIL_PASSWORD=app.config.get("MAIL_PASSWORD"),
Expand Down Expand Up @@ -125,10 +122,10 @@ async def send_message(self, message: Message, template_name=None):
to send the message object.
:param `message`: The object of the Message pydantic class.
:param `template_name`: if you are about to render any template
:param `template_name`: if you are about to render any template
with the mail please provide the name of the template by using this param.
### For example =>
### For example =>
```python
@app.get("/email")
async def simple_send():
Expand Down Expand Up @@ -164,23 +161,23 @@ async def simple_send():

async def send_mail(
self,
subject:str,
message:str,
recipients:t.List[EmailStr],
html_message:t.Optional[str]=None,
**msgkwargs
) -> None:
subject: str,
message: str,
recipients: t.List[EmailStr],
html_message: t.Optional[str] = None,
**msgkwargs,
) -> None:
"""
send a simple email by using this simple `send_mail` method.
:param `subject`: A String containing the subject of the message.
:param `message`: A string containing the message body.
:param `recipients`: A list of strings, each an email address.
Each member of recipients will see the other recipients
:param `recipients`: A list of strings, each an email address.
Each member of recipients will see the other recipients
in the “To:” field of the email message
:param `msgkwargs` : the kwargs based parameters for `Message` class.
### For example =>
### For example =>
```python
@app.get('/send-mail')
async def send_mail():
Expand All @@ -193,14 +190,13 @@ async def send_mail():
recipients=recipients,
body=message,
html=html_message,
**msgkwargs
**msgkwargs,
)
await self.send_message(message)

async def send_mass_mail(
self,
datatuple:t.Tuple[t.Tuple[str, str, t.List[EmailStr]]]
):
self, datatuple: t.Tuple[t.Tuple[str, str, t.List[EmailStr]]]
):
"""
To handle mass mailing.
Expand All @@ -221,4 +217,4 @@ async def send_mass_mail(
Signal sent when an email is dispatched. This signal will also be sent
in testing mode, even though the email will not actually be sent.
""",
)
)
3 changes: 2 additions & 1 deletion flask_mailing/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def validate_template_params(cls, value, info):
if info.data.get("template_body", None) is None:
info.data["template_body"] = value
return value

@field_validator("subtype")
def validate_subtype(cls, value, info):
"""Validate subtype field."""
Expand Down Expand Up @@ -138,6 +138,7 @@ def attach(
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
8 changes: 4 additions & 4 deletions flask_mailing/utils/email_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
from abc import ABC, abstractmethod
from typing import Any, List, Set


import dns.exception
import dns.resolver

try:
import aioredis

redis_lib = True
except:
redis_lib = False

try:
import httpx

request_lib = True
except:
request_lib = False
Expand Down Expand Up @@ -97,12 +98,12 @@ def __init__(
):
if not redis_lib:
raise ImportError(
'You must install aioredis from https://pypi.org/project/aioredis in order to run functionality'
"You must install aioredis from https://pypi.org/project/aioredis in order to run functionality"
)

if not request_lib:
raise ImportError(
'You must install httpx from https://pypi.org/project/httpx in order to run functionality'
"You must install httpx from https://pypi.org/project/httpx in order to run functionality"
)

self.source = (
Expand Down Expand Up @@ -274,7 +275,6 @@ async def check_mx_record(self, domain: str, full_result: bool = False):
dns.resolver.NoNameservers,
dns.exception.Timeout,
):

return False

async def blocked_email_count(self):
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest

# import fakeredis.aioredis
from flask import Flask

Expand Down
10 changes: 8 additions & 2 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ async def test_connection(app: "Flask"):
assert not message.template_body
assert not message.html


@pt.mark.asyncio
async def test_send_mail_method(app: "Flask"):
fm = Mail(app)

with fm.record_messages() as outbox:
await fm.send_mail(subject="test subject", message="test", recipients=["sabuhi.shukurov@gmail.com"])
await fm.send_mail(
subject="test subject",
message="test",
recipients=["sabuhi.shukurov@gmail.com"],
)

assert len(outbox) == 1
mail = outbox[0]
assert mail["To"] == "sabuhi.shukurov@gmail.com"
assert mail["Subject"] == "test subject"


@pt.mark.asyncio
async def test_send_mass_mail_method(app: "Flask"):
fm = Mail(app)
Expand All @@ -49,7 +55,7 @@ async def test_send_mass_mail_method(app: "Flask"):
datatuple = (
("test-subject-1", "message-body-1", ["sabuhi.shukurov@gmail.com"]),
("test-subject-2", "message-body-2", ["sabuhi.shukurov@gmail.com"]),
("test-subject-3", "message-body-3", ["sabuhi.shukurov@gmail.com"])
("test-subject-3", "message-body-3", ["sabuhi.shukurov@gmail.com"]),
)
await fm.send_mass_mail(datatuple)

Expand Down

0 comments on commit 2679e3e

Please sign in to comment.