foerderbarometer/input/management/commands/sendmails.py

167 lines
6.9 KiB
Python
Raw Normal View History

from datetime import date, timedelta
import sys
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import get_template
from django.core.mail import send_mail, BadHeaderError
2020-11-16 12:19:12 +01:00
from django.conf import settings
2020-10-27 13:08:47 +01:00
from input.models import Project, Library, HonoraryCertificate, Travel, Email,\
BusinessCard, List, IFG, Literature
2020-11-16 12:02:50 +01:00
from input.settings import IF_EMAIL, SURVEYPREFIX
class Command(BaseCommand):
''' mails will be send here:
- two weeks after confirmation of support for volunteer (/extern) send link
with surveylink
- same for HonoraryCertificate (/intern)
- travel: mail 3 weeks after end of project.
- assumed end of project (/project) reached: mail to IF, link to project-editpage
- 4 weeks after end of project reached: mail with surveylink
'''
help = '''This command sends mail with some links to the database or to the survey
after some amount of time.'''
def survey_link(self, email, type, pid, name, realname):
context = {'realname': realname,
'type': type,
'name': name,
'pid': pid,
'SURVEYPREFIX': SURVEYPREFIX, }
mail_template = get_template('input/survey_mail.txt')
try:
send_mail('Projektende erreicht',
mail_template.render(context),
IF_EMAIL,
[email],
fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found.')
print(f'send surveylinkemail to {email}...')
def end_of_projects_reached(self):
''' end of project reached '''
# get all projects which ended
old = Project.objects.filter(end__lt = date.today())\
.exclude(end_mail_send = True)
mail_template = get_template('input/if_end_of_project.txt')
for project in old:
context = {'project': project}
2020-11-16 12:02:50 +01:00
context['URLPREFIX'] = settings.URLPREFIX
try:
send_mail('Projektende erreicht',
mail_template.render(context),
IF_EMAIL,
[IF_EMAIL],
fail_silently=False)
project.end_mail_send = True
project.save()
except BadHeaderError:
self.stdout.write(self.style.ERROR('Invalid header found.'))
self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
def surveymails_to_object(self, supported, name='', type='LIB'):
2020-10-26 13:55:36 +01:00
mytype=type
myname = name
for item in supported:
2020-10-26 13:55:36 +01:00
if type == 'LIB':
mytype = item.type
elif type not in ('MAIL','VIS','LIST'):
myname = getattr(item,name)
self.survey_link(email=item.email,
2020-10-26 13:55:36 +01:00
type=mytype,
2020-10-26 15:06:19 +01:00
pid=f'{mytype}{item.pk}',
name=myname,
realname=item.realname)
item.survey_mail_send = True
item.save()
self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
2020-10-26 13:55:36 +01:00
''' TODO: there could be some more removing of duplicated code in the following functions '''
2020-10-26 13:55:36 +01:00
def surveymails_to_lib(self):
'''get all library objects which where granted two weeks ago'''
supported = Library.objects.filter(granted=True)\
.filter(granted_date__lt = date.today() - timedelta(days=14))\
.exclude(survey_mail_send=True)
2020-10-26 15:06:19 +01:00
self.surveymails_to_object(supported,name='library')
def surveymails_to_hon(self):
'''get all HonoraryCertificate objects which where granted two weeks ago'''
supported = HonoraryCertificate.objects.filter(granted=True)\
.filter(granted_date__lt = date.today() - timedelta(days=14))\
.exclude(survey_mail_send=True)
2020-10-26 13:55:36 +01:00
self.surveymails_to_object(supported, type='HON', name='request_url')
2020-10-27 13:08:47 +01:00
def surveymails_to_ifg(self):
'''get all IFG objects which where granted two weeks ago'''
supported = IFG.objects.filter(granted=True)\
.filter(granted_date__lt = date.today() - timedelta(days=14))\
.exclude(survey_mail_send=True)
self.surveymails_to_object(supported, type='IFG', name='url')
def surveymails_to_lit(self):
'''get all Litearure objects which where granted two weeks ago'''
supported = Literature.objects.filter(granted=True)\
.filter(granted_date__lt = date.today() - timedelta(days=14))\
.exclude(survey_mail_send=True)
self.surveymails_to_object(supported, type='LIT', name='info')
def surveymails_to_project(self):
'''send survey link 4 weeks after end of project reached'''
supported = Project.objects.filter(granted=True)\
.filter(granted_date__lt = date.today() - timedelta(days=28))\
.exclude(survey_mail_send=True)
2020-10-26 13:55:36 +01:00
self.surveymails_to_object(supported, type='PRO', name='realname')
def surveymails_to_travel(self):
'''send survey link 3 weeks after end of project reached'''
supported = Travel.objects.filter(project__granted=True)\
.filter(project__granted_date__lt = date.today() - timedelta(days=21))\
.exclude(survey_mail_send=True)
2020-10-26 13:55:36 +01:00
self.surveymails_to_object(supported, type='TRAV', name='request_url')
def surveymails_to_mail_vis_lis(self):
'''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
lastdate = date.today() - timedelta(days=14)
typefield = ('MAIL','VIS','LIST')
count = 0
for c in ('Email', 'BusinessCard', 'List'):
# get class via string
supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
.filter(granted_date__lt = lastdate)\
.exclude(survey_mail_send=True)
self.surveymails_to_object(supported, type=typefield[count])
count += 1
def handle(self, *args, **options):
'''the main function which is called by the custom command'''
self.end_of_projects_reached()
self.surveymails_to_lib()
self.surveymails_to_hon()
2020-10-27 13:08:47 +01:00
self.surveymails_to_ifg()
self.surveymails_to_lit()
self.surveymails_to_project()
self.surveymails_to_travel()
self.surveymails_to_mail_vis_lis()
self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))