146 lines
5.5 KiB
Python
146 lines
5.5 KiB
Python
from datetime import date, timedelta
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.template.loader import get_template
|
|
from django.core.mail import send_mail, BadHeaderError
|
|
|
|
from input.models import Project, Library, HonoraryCertificate, Travel
|
|
from input.settings import URLPREFIX, 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}
|
|
context['URLPREFIX'] = URLPREFIX
|
|
print(context)
|
|
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_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)
|
|
print(supported)
|
|
for item in supported:
|
|
self.survey_link(email=item.email,
|
|
type=item.type,
|
|
pid=9999, ## TODO
|
|
name=item.library,
|
|
realname=item.realname)
|
|
item.survey_mail_send = True
|
|
item.save()
|
|
|
|
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)
|
|
print(supported)
|
|
for item in supported:
|
|
self.survey_link(email=item.email,
|
|
type='HON',
|
|
pid=9999, ## TODO
|
|
name=item.request_url,
|
|
realname=item.realname)
|
|
item.survey_mail_send = True
|
|
item.save()
|
|
|
|
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)
|
|
print(supported)
|
|
for item in supported:
|
|
self.survey_link(email=item.email,
|
|
type='PRO',
|
|
pid=9999, ## TODO
|
|
name=item.name,
|
|
realname=item.realname)
|
|
item.survey_mail_send = True
|
|
item.save()
|
|
|
|
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)
|
|
print(supported)
|
|
for item in supported:
|
|
self.survey_link(email=item.email,
|
|
type='TRAV',
|
|
pid=9999, ## TODO
|
|
name=item.request_url,
|
|
realname=item.realname)
|
|
item.survey_mail_send = True
|
|
item.save()
|
|
|
|
pass
|
|
|
|
|
|
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()
|
|
self.surveymails_to_project()
|
|
self.surveymails_to_travel()
|
|
|
|
self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))
|