-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.py
More file actions
51 lines (41 loc) · 1.73 KB
/
notification.py
File metadata and controls
51 lines (41 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask_mail import Mail, Message
from models import User, Task
from datetime import datetime, timedelta
mail = Mail()
def setup_mail(app):
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'zorguimohamedyassine@gmail.com' #update the mail to your sender mail and implement it here and at line 31
app.config['MAIL_PASSWORD'] = 'your-email-app-password' #genrate app password in google account for this app and implement it here
mail.init_app(app)
def send_task_notifications():
"""Send notifications for tasks due in the next 7 days"""
from app import app
with app.app_context():
upcoming_tasks = Task.query.filter(
Task.echeance_prochaine.between(
datetime.now(),
datetime.now() + timedelta(days=7)
)
).all()
if upcoming_tasks:
users = User.query.all()
for user in users:
msg = Message(
'Upcoming Tasks Notification',
sender='zorguimohamedyassine@gmail.com',
recipients=[user.email]
)
task_list = "\n".join([
f"- {task.action_programmee} (Due: {task.echeance_prochaine.strftime('%Y-%m-%d')})"
for task in upcoming_tasks
])
msg.body = f"""
Hello {user.username},
The following tasks are due in the next 7 days:
{task_list}
Best regards,
Task Management System
"""
mail.send(msg)