Mail send ok

This commit is contained in:
Colin Goutte 2021-10-07 15:03:02 +02:00
parent 05ad5828e4
commit 68f03eabbc
2 changed files with 41 additions and 4 deletions

24
papi/mail_sendermodel.py Normal file
View File

@ -0,0 +1,24 @@
from mailjet_rest import Client
import os
from papi.credentials import api_key, api_secret
def sendmail(htmlpart):
mailjet = Client(auth=(api_key, api_secret), version="v3.1")
data = {
"Messages": [
{
"From": {"Email": "colin.goutte@free.fr", "Name": "Colin"},
"To": [{"Email": "colin.goutte@free.fr", "Name": "Colin"}],
"Subject": "Monitoring API",
"TextPart": "My first Mailjet email",
"HTMLPart": "" + htmlpart,
"CustomID": "AppGettingStartedTest",
}
]
}
result = mailjet.send.create(data=data)
print(result.status_code)
print(result.json())

View File

@ -106,7 +106,7 @@ def list_notification(idsonde: str):
@app.get("/notifications/{idsonde}/text")
def list_notification_as_text(request: Request, idsonde: str):
def last_notif_text(request: Request, idsonde: str):
notifs = notifications[idsonde]
try:
content = notifs[-1]
@ -139,7 +139,10 @@ def list_notification_as_text(request: Request, idsonde: str):
@app.post("/sonde/{idsonde}/error")
def post_sonde_error(
idsonde: str, body: dict = Body(...), db: Session = Depends(get_db)
request: Request,
idsonde: str,
body: dict = Body(...),
db: Session = Depends(get_db),
):
if not (sonde := crud.get_sonde(db, idsonde)):
return # pragma: no cover
@ -157,13 +160,16 @@ def post_sonde_error(
api_error["date"] = date
post_sonde_data(idsonde, body=api_error, db=db)
post_sonde_data(request, idsonde, body=api_error, db=db)
return
@app.post("/sonde/{idsonde}/")
def post_sonde_data(
idsonde: str, body: dict = Body(...), db: Session = Depends(get_db)
request: Request,
idsonde: str,
body: dict = Body(...),
db: Session = Depends(get_db),
):
if not (sonde := crud.get_sonde(db, idsonde)):
return
@ -189,6 +195,13 @@ def post_sonde_data(
)
if content:
Notifier(idsonde, content)
res = last_notif_text(request, idsonde)
html = res.body.decode("utf-8")
from papi.mail_sendermodel import sendmail
cond = True
if cond:
sendmail(html)
return {
"count": len(mesures_)
if "date" in mesures_[0].content.keys()