mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-24 06:51:29 +01:00
Implement email notifications
This commit is contained in:
parent
19308b645b
commit
9c10e190bc
@ -158,7 +158,7 @@ def email_notifications(username):
|
|||||||
return redirect(url_for("tasks.check", id=task.id, r=url_for("users.profile", username=username)))
|
return redirect(url_for("tasks.check", id=task.id, r=url_for("users.profile", username=username)))
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return redirect(url_for("notifications.settings"))
|
return redirect(url_for("users.email_notifications", username=username))
|
||||||
|
|
||||||
return render_template("users/settings_email.html",
|
return render_template("users/settings_email.html",
|
||||||
form=form, user=user, types=types, is_new=is_new,
|
form=form, user=user, types=types, is_new=is_new,
|
||||||
|
@ -380,7 +380,7 @@ class Notification(db.Model):
|
|||||||
|
|
||||||
def can_send_email(self):
|
def can_send_email(self):
|
||||||
prefs = self.user.notification_preferences
|
prefs = self.user.notification_preferences
|
||||||
return prefs and getattr(prefs, "pref_" + self.type.toName()) == 2
|
return prefs and self.user.email and prefs.get_can_email(self.type)
|
||||||
|
|
||||||
|
|
||||||
class UserNotificationPreferences(db.Model):
|
class UserNotificationPreferences(db.Model):
|
||||||
@ -407,7 +407,7 @@ class UserNotificationPreferences(db.Model):
|
|||||||
self.pref_package_edit = 1
|
self.pref_package_edit = 1
|
||||||
self.pref_package_approval = 2
|
self.pref_package_approval = 2
|
||||||
self.pref_new_thread = 2
|
self.pref_new_thread = 2
|
||||||
self.pref_new_review = 1
|
self.pref_new_review = 2
|
||||||
self.pref_thread_reply = 2
|
self.pref_thread_reply = 2
|
||||||
self.pref_maintainer = 2
|
self.pref_maintainer = 2
|
||||||
self.pref_editor_alert = 2
|
self.pref_editor_alert = 2
|
||||||
|
@ -71,6 +71,10 @@ CELERYBEAT_SCHEDULE = {
|
|||||||
'package_score_update': {
|
'package_score_update': {
|
||||||
'task': 'app.tasks.pkgtasks.updatePackageScores',
|
'task': 'app.tasks.pkgtasks.updatePackageScores',
|
||||||
'schedule': crontab(minute=10, hour=1),
|
'schedule': crontab(minute=10, hour=1),
|
||||||
|
},
|
||||||
|
'send_pending_notifications': {
|
||||||
|
'task': 'app.tasks.emails.sendPendingNotifications',
|
||||||
|
'schedule': crontab(minute='*/5'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
|
celery.conf.beat_schedule = CELERYBEAT_SCHEDULE
|
||||||
|
@ -18,8 +18,10 @@
|
|||||||
from flask import render_template
|
from flask import render_template
|
||||||
from flask_mail import Message
|
from flask_mail import Message
|
||||||
from app import mail
|
from app import mail
|
||||||
|
from app.models import Notification, db
|
||||||
from app.tasks import celery
|
from app.tasks import celery
|
||||||
from app.utils import abs_url_for
|
from app.utils import abs_url_for, abs_url
|
||||||
|
|
||||||
|
|
||||||
@celery.task()
|
@celery.task()
|
||||||
def sendVerifyEmail(newEmail, token):
|
def sendVerifyEmail(newEmail, token):
|
||||||
@ -48,3 +50,29 @@ def sendEmailRaw(to, subject, text, html=None):
|
|||||||
html = html or text
|
html = html or text
|
||||||
msg.html = render_template("emails/base.html", subject=subject, content=html)
|
msg.html = render_template("emails/base.html", subject=subject, content=html)
|
||||||
mail.send(msg)
|
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()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from . import app
|
from . import app, utils
|
||||||
from .models import Permission, Package, PackageState, PackageRelease
|
from .models import Permission, Package, PackageState, PackageRelease
|
||||||
from .utils import abs_url_for, url_set_query
|
from .utils import abs_url_for, url_set_query
|
||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
@ -42,3 +42,7 @@ def datetime(value):
|
|||||||
@app.template_filter()
|
@app.template_filter()
|
||||||
def timedelta(value):
|
def timedelta(value):
|
||||||
return format_timedelta(value)
|
return format_timedelta(value)
|
||||||
|
|
||||||
|
@app.template_filter()
|
||||||
|
def abs_url(url):
|
||||||
|
return utils.abs_url(url)
|
||||||
|
30
app/templates/emails/notification.html
Normal file
30
app/templates/emails/notification.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
{% extends "emails/base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h2 style="margin-top: 0;">
|
||||||
|
{% if notification.package %}
|
||||||
|
{{ _("New notification on package %(package)s", package=notification.package.title) }}
|
||||||
|
{% else %}
|
||||||
|
{{ _("New notification") }}
|
||||||
|
{% endif %}
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<p><small>{{ _("Triggered by %(username)s", username=notification.causer.display_name) }}</small></p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
{{ notification.title }}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<a class="btn" href="{{ notification.url | abs_url }}">
|
||||||
|
View Notification
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p><small>
|
||||||
|
<a href="{{ abs_url_for('users.email_notifications', username=notification.user.username) }}">
|
||||||
|
{{ _("Edit notification settings") }}
|
||||||
|
</a>
|
||||||
|
</small></p>
|
||||||
|
|
||||||
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user