emails.py 1.41 KB
Newer Older
1
2
import datetime

3
from django.conf import settings
4
5
6
7
8
9
10
from django.core.mail import EmailMessage
from django.utils import timezone

from partners.models import Vacancy


def send_vacancy_expiration_notifications(dry_run=False):
11
    """Send a notification about expiring vacancies."""
12
13
14
15
16
17
18
19
20
21
22
    # Select vacencies that expire in roughly a month, wherefor
    # a mail hasn't been sent yet to Mr/Mrs Extern
    expired_vacancies = Vacancy.objects.filter(
        expiration_mail_sent=False,
        expiration_date__lt=timezone.now().date() + datetime.timedelta(days=30)
    )
    for exp_vacancy in expired_vacancies:
        # Create Message
        subject = ('[WEBSITE] Vacancy \'{}\' by {} will soon expire'
                   .format(exp_vacancy.title, exp_vacancy.get_company_name()))
        text_message = ('Hello!\n\nA vacancy of {}, \'{}\' will '
Joren Vrancken's avatar
Joren Vrancken committed
23
24
                        'expire within the next 30 days.'
                        '\n\nKind regards,\nThe website'
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
                        .format(exp_vacancy.title,
                                exp_vacancy.get_company_name()))
        recipient = settings.PARTNER_EMAIL

        if not dry_run:
            # Send Mail
            EmailMessage(
                subject,
                text_message,
                to=[recipient]
            ).send()

            # Save that mail has been sent into database
            exp_vacancy.expiration_mail_sent = True
            exp_vacancy.save()