tuomas2/serviceform

View on GitHub
serviceform/serviceform/management/commands/send_emails.py

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
# -*- coding: utf-8 -*-
# (c) 2017 Tuomas Airaksinen
#
# This file is part of Serviceform.
#
# Serviceform is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Serviceform is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Serviceform.  If not, see <http://www.gnu.org/licenses/>.


import time
import logging

from django.core.management import BaseCommand
from django.utils.translation import activate
from django.conf import settings
from serviceform.serviceform.models import EmailMessage
from serviceform.serviceform.utils import DelayedKeyboardInterrupt


logger = logging.getLogger(__name__)


class Command(BaseCommand):
    args = '-'
    help = 'Send emails'

    def handle(self, *args, **kwargs):
        activate(settings.LANGUAGE_CODE)
        while True:
            msgs = EmailMessage.objects.filter(sent_at__isnull=True)
            for m in msgs:
                with DelayedKeyboardInterrupt():
                    m.send()
            time.sleep(5)