2019-02-26 13:07:47 +00:00
|
|
|
"""passbook core tasks"""
|
2019-04-04 20:49:10 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from logging import getLogger
|
|
|
|
|
2019-02-26 13:07:47 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
|
|
from passbook.core.celery import CELERY_APP
|
2019-04-04 20:49:10 +01:00
|
|
|
from passbook.core.models import Nonce
|
2019-02-26 13:07:47 +00:00
|
|
|
from passbook.lib.config import CONFIG
|
|
|
|
|
2019-04-04 20:49:10 +01:00
|
|
|
LOGGER = getLogger(__name__)
|
2019-02-26 13:07:47 +00:00
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def send_email(to_address, subject, template, context):
|
|
|
|
"""Send Email to user(s)"""
|
|
|
|
html_content = render_to_string(template, context=context)
|
|
|
|
text_content = strip_tags(html_content)
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, CONFIG.y('email.from'), [to_address])
|
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
msg.send()
|
2019-04-04 20:49:10 +01:00
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def clean_nonces():
|
|
|
|
"""Remove expired nonces"""
|
|
|
|
amount = Nonce.objects.filter(expires__lt=datetime.now(), expiring=True).delete()
|
|
|
|
LOGGER.debug("Deleted expired %d nonces", amount)
|