2020-10-22 08:27:14 +00:00
|
|
|
from datetime import date, timedelta
|
2020-10-21 13:01:05 +00:00
|
|
|
|
2020-10-07 09:21:21 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2020-10-22 08:27:14 +00:00
|
|
|
from django.template.loader import get_template
|
|
|
|
from django.core.mail import send_mail, BadHeaderError
|
2020-10-21 13:01:05 +00:00
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
from input.models import Project, Library
|
2020-10-22 12:17:34 +00:00
|
|
|
from input.settings import URLPREFIX, IF_EMAIL, SURVEYPREFIX
|
2020-10-07 11:15:00 +00:00
|
|
|
|
2020-10-07 09:21:21 +00:00
|
|
|
class Command(BaseCommand):
|
2020-10-21 13:01:05 +00:00
|
|
|
''' mails will be send here:
|
|
|
|
|
|
|
|
- two weeks after confirmation of support for volunteer (/extern) send link
|
|
|
|
with surveylink
|
|
|
|
|
|
|
|
- same for HonoraryCertificate and accreditation (/intern)
|
|
|
|
|
|
|
|
- travel: mail 3 weeks after end of project.
|
|
|
|
|
|
|
|
- assumed end of project (/project) reached: mail to IF, link to project-editpage
|
2020-10-22 11:08:58 +00:00
|
|
|
|
|
|
|
- 4 weeks after end of project reached: mail with surveylink
|
2020-10-21 13:01:05 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
help = '''This command sends mail with some links to the database or to the survey
|
|
|
|
after some amount of time.'''
|
2020-10-07 09:21:21 +00:00
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
def survey_link(self, email, type, pid, name, realname):
|
2020-10-22 12:17:34 +00:00
|
|
|
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}...')
|
2020-10-07 11:15:00 +00:00
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
def end_of_projects_reached(self):
|
|
|
|
''' end of project reached '''
|
|
|
|
# get all projects which ended
|
|
|
|
# - timedelta(days=21))
|
|
|
|
old = Project.objects.filter(end__lt = date.today())\
|
2020-10-22 09:36:16 +00:00
|
|
|
.exclude(end_mail_send = True)
|
|
|
|
|
2020-10-22 08:27:14 +00:00
|
|
|
mail_template = get_template('input/if_end_of_project.txt')
|
|
|
|
for project in old:
|
|
|
|
context = {'project': project}
|
2020-10-22 08:53:40 +00:00
|
|
|
context['URLPREFIX'] = URLPREFIX
|
|
|
|
print(context)
|
2020-10-22 08:27:14 +00:00
|
|
|
try:
|
|
|
|
send_mail('Projektende erreicht',
|
|
|
|
mail_template.render(context),
|
|
|
|
IF_EMAIL,
|
|
|
|
[IF_EMAIL],
|
|
|
|
fail_silently=False)
|
2020-10-22 09:36:16 +00:00
|
|
|
project.end_mail_send = True
|
|
|
|
project.save()
|
2020-10-22 08:27:14 +00:00
|
|
|
except BadHeaderError:
|
2020-10-22 12:38:23 +00:00
|
|
|
self.stdout.write(self.style.ERROR('Invalid header found.'))
|
2020-10-22 08:27:14 +00:00
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
|
|
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
self.end_of_projects_reached()
|
|
|
|
|
2020-10-22 12:38:23 +00:00
|
|
|
supported = Library.objects.filter(granted=True)\
|
|
|
|
.exclude(survey_mail_send=True)
|
2020-10-22 11:08:58 +00:00
|
|
|
print(supported)
|
|
|
|
for item in supported:
|
|
|
|
self.survey_link(email=item.email,
|
2020-10-22 12:38:23 +00:00
|
|
|
type=item.type,
|
|
|
|
pid=9999, ## TODO
|
|
|
|
name=item.library,
|
|
|
|
realname=item.realname)
|
|
|
|
item.survey_mail_send = True
|
|
|
|
item.save()
|
2020-10-22 11:08:58 +00:00
|
|
|
|
2020-10-07 09:21:21 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))
|