Skip to content

Commit

Permalink
Merge pull request #58 from it-goats/feature/ITG-133-134
Browse files Browse the repository at this point in the history
[ITG-133] [ITG-134] Notification for tasks by sendgrid.
  • Loading branch information
gregori0o committed Jun 17, 2022
2 parents 4c6d84a + 1253fcc commit 0c07902
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bode/bode/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from flask_migrate import upgrade
from flask_smorest import Blueprint

from bode.mail_service.sending import start_notify

from .config import Config
from .extensions import api, db, migrate
from .resources import tags, task_relations, tasks
Expand All @@ -28,6 +30,8 @@ def create_app():
if CONFIG.AUTO_MIGRATE:
upgrade()

start_notify(app)

return app


Expand Down
3 changes: 3 additions & 0 deletions bode/bode/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ def get_db_uri():
class Config:
DATABASE_URI = get_db_uri()
AUTO_MIGRATE = os.getenv("AUTO_MIGRATE", False)


sendgrid_key = os.getenv("SENDGRID_API_KEY")
56 changes: 56 additions & 0 deletions bode/bode/mail_service/sending.py
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()
2 changes: 1 addition & 1 deletion bode/bode/models/tag/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy.dialects.postgresql import UUID

from bode.app import db
from bode.extensions import db

task_tag = db.Table(
"task_tag", db.Column("task_id", db.ForeignKey("tasks.id")), db.Column("tag_id", db.ForeignKey("tags.id"))
Expand Down
2 changes: 1 addition & 1 deletion bode/bode/models/task/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import Enum
from sqlalchemy.dialects.postgresql import UUID

from bode.app import db
from bode.extensions import db
from bode.models.enums import TaskStatus
from bode.models.tag.model import task_tag
from bode.models.utc_datetime import UTCDateTime
Expand Down
54 changes: 53 additions & 1 deletion bode/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bode/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ psycopg2 = "^2.9.3"
flake8-print = "^4.0.0"
python-dateutil = "^2.8.2"
gunicorn = "^20.1.0"
schedule = "^1.1.0"
sendgrid = "^6.9.7"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- "4000:4000"
env_file:
- .env.example
- .env
volumes:
- ./bode:/bode/

Expand Down

0 comments on commit 0c07902

Please sign in to comment.