mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Refs #44 -- Added optional celery background queue for mails
This commit is contained in:
@@ -1 +1,6 @@
|
||||
__version__ = "0.0.0"
|
||||
|
||||
try:
|
||||
from .celery import app as celery_app
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@@ -2,7 +2,6 @@ import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template.loader import get_template
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -66,10 +65,16 @@ def mail(user: User, subject: str, template: str, context: dict=None, event: Eve
|
||||
}
|
||||
)
|
||||
body += "\r\n"
|
||||
try:
|
||||
return mail_send([user.email], subject, body, sender)
|
||||
finally:
|
||||
translation.activate(_lng)
|
||||
|
||||
|
||||
def mail_send(to, subject, body, sender):
|
||||
email = EmailMessage(
|
||||
subject, body, sender,
|
||||
to=[user.email]
|
||||
to=to
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -78,5 +83,10 @@ def mail(user: User, subject: str, template: str, context: dict=None, event: Eve
|
||||
except Exception:
|
||||
logger.exception('Error sending e-mail')
|
||||
return False
|
||||
finally:
|
||||
translation.activate(_lng)
|
||||
|
||||
|
||||
if settings.HAS_CELERY:
|
||||
from pretix.celery import app
|
||||
|
||||
mail_send_task = app.task(mail_send)
|
||||
mail_send = lambda *args, **kwargs: mail_send_task.apply_async(args=args, kwargs=kwargs)
|
||||
|
||||
12
src/pretix/celery.py
Normal file
12
src/pretix/celery.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import os
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pretix.settings")
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
if settings.HAS_CELERY:
|
||||
from celery import Celery
|
||||
app = Celery('pretix')
|
||||
|
||||
app.config_from_object('django.conf:settings')
|
||||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
||||
@@ -114,6 +114,12 @@ if HAS_REDIS:
|
||||
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
||||
SESSION_CACHE_ALIAS = "redis"
|
||||
|
||||
HAS_CELERY = config.has_option('celery', 'broker')
|
||||
if HAS_CELERY:
|
||||
BROKER_URL = config.get('celery', 'broker')
|
||||
CELERY_RESULT_BACKEND = config.get('celery', 'backend')
|
||||
CELERY_SEND_TASK_ERROR_EMAILS = bool(ADMINS)
|
||||
|
||||
# Internal settings
|
||||
|
||||
STATIC_ROOT = '_static'
|
||||
@@ -330,3 +336,6 @@ LOGGING = {
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_RESULT_SERIALIZER = 'json'
|
||||
|
||||
Reference in New Issue
Block a user