-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from it-goats/feature/ITG-133-134
[ITG-133] [ITG-134] Notification for tasks by sendgrid.
- Loading branch information
Showing
8 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import time | ||
|
||
from datetime import datetime, timedelta | ||
from threading import Thread | ||
|
||
import schedule | ||
|
||
from dateutil import tz | ||
from sendgrid import SendGridAPIClient | ||
from sendgrid.helpers.mail import Mail | ||
|
||
from bode.config import sendgrid_key | ||
from bode.extensions import db | ||
from bode.models.enums import TaskStatus | ||
from bode.models.task.model import Task | ||
|
||
client = SendGridAPIClient(sendgrid_key) | ||
|
||
|
||
def send_notification(address, title, due_date, link): | ||
message = Mail( | ||
from_email="itgoatsteam@gmail.com", | ||
to_emails=address, | ||
subject="Notification", | ||
html_content=f"<h3>The deadline is approaching for task:</h3><h4>TITLE: </h4>{title}<br /><h4>" | ||
f"DUE DATE: </h4>{due_date}<br /><a href='{link}'><h4>CLICK</h4></a>", | ||
) | ||
client.send(message) | ||
|
||
|
||
def start_notify(app): | ||
def check_task_to_notify(): | ||
nowutc = datetime.utcnow().replace(second=0, microsecond=0) | ||
with app.app_context(): | ||
tasks = db.session.query(Task).filter(Task.status == TaskStatus.TODO.value).all() | ||
address = "itgoatsteam@gmail.com" # get email from database | ||
for task in tasks: | ||
notify_time = 60 # in minutes, get this information from task, if none => not notify | ||
if notify_time is None: | ||
continue | ||
dt = nowutc + timedelta(minutes=notify_time) | ||
due_date = datetime.fromisoformat(str(task.due_date)).replace(second=0, microsecond=0) | ||
delta = dt - due_date.replace(tzinfo=None) | ||
if delta.total_seconds() != 0: | ||
continue | ||
link = f"https://it-goats.netlify.app/task/{task.id}" | ||
due_date_string = due_date.astimezone(tz.gettz("Europe/Warsaw")).strftime("%d-%m-%Y %H:%M") | ||
send_notification(address, task.title, due_date_string, link) | ||
|
||
def start_scheduler(): | ||
schedule.every().minute.at(":00").do(check_task_to_notify) | ||
while True: | ||
schedule.run_pending() | ||
time.sleep(1) | ||
|
||
Thread(target=start_scheduler, daemon=True).start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ services: | |
- "4000:4000" | ||
env_file: | ||
- .env.example | ||
- .env | ||
volumes: | ||
- ./bode:/bode/ | ||
|
||
|