Implement email notifications

This commit is contained in:
rubenwardy
2020-12-05 19:15:33 +00:00
parent 19308b645b
commit 9c10e190bc
6 changed files with 71 additions and 5 deletions

View File

@@ -71,6 +71,10 @@ CELERYBEAT_SCHEDULE = {
'package_score_update': {
'task': 'app.tasks.pkgtasks.updatePackageScores',
'schedule': crontab(minute=10, hour=1),
},
'send_pending_notifications': {
'task': 'app.tasks.emails.sendPendingNotifications',
'schedule': crontab(minute='*/5'),
}
}
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE

View File

@@ -18,8 +18,10 @@
from flask import render_template
from flask_mail import Message
from app import mail
from app.models import Notification, db
from app.tasks import celery
from app.utils import abs_url_for
from app.utils import abs_url_for, abs_url
@celery.task()
def sendVerifyEmail(newEmail, token):
@@ -48,3 +50,29 @@ def sendEmailRaw(to, subject, text, html=None):
html = html or text
msg.html = render_template("emails/base.html", subject=subject, content=html)
mail.send(msg)
def sendNotificationEmail(notification):
msg = Message(notification.title, recipients=[notification.user.email])
msg.body = """
New notification: {}
View: {}
Manage email settings: {}
""".format(notification.title, abs_url(notification.url),
abs_url_for("users.email_notifications", username=notification.user.username))
msg.html = render_template("emails/notification.html", notification=notification)
mail.send(msg)
@celery.task()
def sendPendingNotifications():
for notification in Notification.query.filter_by(emailed=False).all():
if notification.can_send_email():
sendNotificationEmail(notification)
notification.emailed = True
db.session.commit()