2020-10-22 08:27:14 +00:00
|
|
|
from datetime import date, timedelta
|
2020-10-27 10:55:16 +00:00
|
|
|
import sys
|
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
|
2021-04-14 12:37:49 +00:00
|
|
|
from django.core.mail import send_mail, BadHeaderError, EmailMessage
|
2023-02-27 17:09:29 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2020-11-16 11:19:12 +00:00
|
|
|
from django.conf import settings
|
2020-10-21 13:01:05 +00:00
|
|
|
|
2020-10-27 12:08:47 +00:00
|
|
|
from input.models import Project, Library, HonoraryCertificate, Travel, Email,\
|
2020-10-27 12:47:46 +00:00
|
|
|
BusinessCard, List, IFG, Literature
|
2021-04-13 11:43:29 +00:00
|
|
|
from input.settings import IF_EMAIL, SURVEYPREFIX, SURVEY_EMAIL
|
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
|
|
|
|
|
2020-10-26 11:56:29 +00:00
|
|
|
- same for HonoraryCertificate (/intern)
|
2020-10-21 13:01:05 +00:00
|
|
|
|
2020-10-26 12:23:05 +00:00
|
|
|
- travel: mail 3 weeks after end of project.
|
2020-10-21 13:01:05 +00:00
|
|
|
|
|
|
|
- assumed end of project (/project) reached: mail to IF, link to project-editpage
|
2020-10-22 11:08:58 +00:00
|
|
|
|
2020-10-26 12:23:05 +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, }
|
2023-02-27 17:09:29 +00:00
|
|
|
txt_mail_template = get_template('input/survey_mail.txt')
|
|
|
|
html_mail_template = get_template('input/survey_mail.html')
|
2020-10-22 12:17:34 +00:00
|
|
|
try:
|
2023-02-27 17:09:29 +00:00
|
|
|
subject, from_email, to = 'Dein Feedback zur Förderung durch Wikimedia Deutschland', IF_EMAIL, email
|
2023-02-27 17:09:29 +00:00
|
|
|
text_content = txt_mail_template.render(context)
|
|
|
|
html_content = html_mail_template.render(context)
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], bcc=[SURVEY_EMAIL])
|
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
msg.send()
|
|
|
|
#print('survey mail would have been send')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
#survey_mail = EmailMessage('Dein Feedback zur Förderung durch Wikimedia Deutschland',
|
|
|
|
# mail_template.render(context),
|
|
|
|
# IF_EMAIL,
|
|
|
|
# [email],
|
|
|
|
# bcc=[SURVEY_EMAIL])
|
|
|
|
#survey_mail.send(fail_silently=False)
|
2020-10-22 12:17:34 +00:00
|
|
|
except BadHeaderError:
|
|
|
|
return HttpResponse('Invalid header found.')
|
|
|
|
|
|
|
|
print(f'send surveylinkemail to {email}...')
|
2020-10-07 11:15:00 +00:00
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
''' the db entry mail_state was added. Useful when migrating databases. first delete the entry, then makemigrations
|
|
|
|
then migrate.. after that, recreate the entry with default END, after that makemigrations and migrate.
|
|
|
|
now all entries have the value END. after that rewrite the default to NONE in models.py, then makemigrations
|
|
|
|
and migrate again, to have NONE as default for all new queries. '''
|
|
|
|
|
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
def end_of_projects_reached(self):
|
|
|
|
''' end of project reached '''
|
|
|
|
# get all projects which ended
|
2023-02-27 17:09:28 +00:00
|
|
|
|
2020-10-22 11:08:58 +00:00
|
|
|
old = Project.objects.filter(end__lt = date.today())\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(end_mail_send = True)\
|
|
|
|
.filter(mail_state = 'NONE')
|
2023-02-27 17:09:28 +00:00
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
txt_mail_template = get_template('input/if_end_of_project.txt')
|
|
|
|
html_mail_template = get_template('input/if_end_of_project.html')
|
|
|
|
|
2020-10-22 08:27:14 +00:00
|
|
|
for project in old:
|
|
|
|
context = {'project': project}
|
2020-11-16 11:02:50 +00:00
|
|
|
context['URLPREFIX'] = settings.URLPREFIX
|
2023-02-27 17:09:29 +00:00
|
|
|
|
2020-10-22 08:27:14 +00:00
|
|
|
try:
|
2023-02-27 17:09:29 +00:00
|
|
|
subject, from_email, to = 'Projektende erreicht', IF_EMAIL, IF_EMAIL
|
|
|
|
text_content = txt_mail_template.render(context)
|
|
|
|
html_content = html_mail_template.render(context)
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
|
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
msg.send()
|
2023-02-27 17:09:29 +00:00
|
|
|
#print('end of project mail would have been sent')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
#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
|
2023-02-27 17:09:29 +00:00
|
|
|
project.mail_state = 'INF'
|
|
|
|
try:
|
|
|
|
project.save()
|
|
|
|
except:
|
|
|
|
print( 'For project', project, 'there is possibly no project.start (', project.start, ') class')
|
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.'))
|
|
|
|
|
2023-02-27 17:09:28 +00:00
|
|
|
def end_of_projects_approved(self):
|
|
|
|
''' end of project approved '''
|
|
|
|
# get all projects where end was reached already, and send mails for the ones already set to status "ended" by the admins
|
|
|
|
|
|
|
|
approved_end = Project.objects.filter(status = 'END')\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(end_mail_send = False)\
|
|
|
|
.filter(mail_state = 'INF')
|
2023-02-27 17:09:29 +00:00
|
|
|
txt_mail_template = get_template('input/if_end_of_project_approved.txt')
|
|
|
|
html_mail_template = get_template('input/if_end_of_project_approved.html')
|
|
|
|
|
|
|
|
txt_informMail_template = get_template('input/if_end_of_project_orginformed.txt')
|
|
|
|
html_informMail_template = get_template('input/if_end_of_project_orginformed.html')
|
2023-02-27 17:09:29 +00:00
|
|
|
# send the mail to project.email, which would be the mail of the volunteer filling out the form
|
|
|
|
|
2023-02-27 17:09:28 +00:00
|
|
|
for project in approved_end:
|
|
|
|
context = {'project': project}
|
|
|
|
context['URLPREFIX'] = settings.URLPREFIX
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
|
2023-02-27 17:09:28 +00:00
|
|
|
try:
|
2023-02-27 17:09:29 +00:00
|
|
|
subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
|
|
|
|
text_content = txt_mail_template.render(context)
|
|
|
|
html_content = html_mail_template.render(context)
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
|
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
msg.send()
|
2023-02-27 17:09:29 +00:00
|
|
|
#print('if and of project approved mail would have been sent')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
inform_subject, inform_from_email, inform_to = 'Projektorganisator*in wurde informiert', IF_EMAIL, IF_EMAIL
|
2023-02-27 17:09:29 +00:00
|
|
|
inform_text_content = txt_informMail_template.render(context)
|
|
|
|
inform_html_content = html_informMail_template.render(context)
|
|
|
|
inform_msg = EmailMultiAlternatives(inform_subject, inform_text_content, inform_from_email, [inform_to])
|
|
|
|
inform_msg.attach_alternative(html_content, "text/html")
|
|
|
|
inform_msg.send()
|
2023-02-27 17:09:29 +00:00
|
|
|
#print('if end of project orginformed mail would have been sent')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
#send_mail('Projektende erreicht',
|
|
|
|
# mail_template.render(context),
|
|
|
|
# IF_EMAIL,
|
|
|
|
# [project.email],
|
|
|
|
# fail_silently=False)
|
|
|
|
#send_mail('Projektorganisator*in wurde informiert',
|
|
|
|
# informMail_template.render(context),
|
|
|
|
# IF_EMAIL,
|
|
|
|
# [IF_EMAIL],
|
|
|
|
# fail_silently=False)
|
2023-02-27 17:09:29 +00:00
|
|
|
|
2023-02-27 17:09:28 +00:00
|
|
|
project.end_mail_send = True
|
2023-02-27 17:09:29 +00:00
|
|
|
project.mail_state = 'END'
|
|
|
|
try:
|
|
|
|
project.save()
|
|
|
|
except:
|
|
|
|
print( 'For project', project, 'there is possibly no project.start (', project.start, ') class')
|
2023-02-27 17:09:28 +00:00
|
|
|
except BadHeaderError:
|
|
|
|
self.stdout.write(self.style.ERROR('Invalid header found.'))
|
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS('end_of_projects_approved() executed.'))
|
|
|
|
|
|
|
|
def notHappened_of_projects_approved(self):
|
|
|
|
''' notHappened of project approved '''
|
|
|
|
# get all projects where end was reached already, and send mails for the ones where status was put to NOT by admins
|
|
|
|
|
|
|
|
approved_notHappened = Project.objects.filter(status = 'NOT')\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(end_mail_send = False)\
|
|
|
|
.filter(mail_state = 'INF')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
html_mail_template = get_template('input/if_not_of_project_approved.html')
|
|
|
|
txt_mail_template = get_template('input/if_not_of_project_approved.txt')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
txt_informMail_template = get_template('input/if_end_of_project_orginformed.txt')
|
|
|
|
html_informMail_template = get_template('input/if_end_of_project_orginformed.html')
|
2023-02-27 17:09:29 +00:00
|
|
|
# send the mail to project.email, which would be the mail of the volunteer that filled out the form
|
|
|
|
|
|
|
|
for project in approved_notHappened:
|
|
|
|
context = {'project': project}
|
|
|
|
context['URLPREFIX'] = settings.URLPREFIX
|
|
|
|
try:
|
2023-02-27 17:09:29 +00:00
|
|
|
subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
|
|
|
|
text_content = txt_mail_template.render(context)
|
|
|
|
html_content = html_mail_template.render(context)
|
|
|
|
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
|
|
|
|
msg.attach_alternative(html_content, "text/html")
|
|
|
|
msg.send()
|
2023-02-27 17:09:29 +00:00
|
|
|
#print('if not of project approved end mail would have been sent')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
#send_mail('Projektende erreicht',
|
|
|
|
# mail_template.render(context),
|
|
|
|
# IF_EMAIL,
|
|
|
|
# [project.email],
|
|
|
|
# fail_silently=False)
|
2023-02-27 17:09:29 +00:00
|
|
|
|
2023-02-27 17:09:29 +00:00
|
|
|
inform_subject, inform_from_email, inform_to = 'Projektorganisator*in wurde informiert', IF_EMAIL, IF_EMAIL
|
2023-02-27 17:09:29 +00:00
|
|
|
inform_text_content = txt_informMail_template.render(context)
|
|
|
|
inform_html_content = html_informMail_template.render(context)
|
|
|
|
inform_msg = EmailMultiAlternatives(inform_subject, inform_text_content, inform_from_email, [inform_to])
|
|
|
|
inform_msg.attach_alternative(html_content, "text/html")
|
|
|
|
inform_msg.send()
|
2023-02-27 17:09:29 +00:00
|
|
|
#print('if not of project approved end mail orginformed would have been sent')
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
#send_mail('Projektorganisator*in wurde informiert',
|
|
|
|
# informMail_template.render(context),
|
|
|
|
# IF_EMAIL,
|
|
|
|
# [IF_EMAIL],
|
|
|
|
# fail_silently=False)
|
2023-02-27 17:09:29 +00:00
|
|
|
|
|
|
|
project.end_mail_send = True
|
2023-02-27 17:09:29 +00:00
|
|
|
project.mail_state = 'END'
|
2023-02-27 17:09:29 +00:00
|
|
|
project.save()
|
|
|
|
except BadHeaderError:
|
|
|
|
self.stdout.write(self.style.ERROR('Invalid header found.'))
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS('notHappened_of_projects_approved() executed.'))
|
2023-02-27 17:09:28 +00:00
|
|
|
|
|
|
|
|
2020-10-27 10:55:16 +00:00
|
|
|
def surveymails_to_object(self, supported, name='', type='LIB'):
|
2020-10-26 12:55:36 +00:00
|
|
|
mytype=type
|
2020-10-27 10:55:16 +00:00
|
|
|
myname = name
|
2020-10-22 11:08:58 +00:00
|
|
|
for item in supported:
|
2020-10-26 12:55:36 +00:00
|
|
|
if type == 'LIB':
|
|
|
|
mytype = item.type
|
2020-10-27 10:55:16 +00:00
|
|
|
elif type not in ('MAIL','VIS','LIST'):
|
2021-03-16 12:08:50 +00:00
|
|
|
myname = getattr(item, name, 'ERROR: NONAME')
|
2021-02-08 08:57:21 +00:00
|
|
|
print(f'name gefunden: {myname}')
|
2020-10-22 11:08:58 +00:00
|
|
|
self.survey_link(email=item.email,
|
2020-10-26 12:55:36 +00:00
|
|
|
type=mytype,
|
2020-10-26 14:06:19 +00:00
|
|
|
pid=f'{mytype}{item.pk}',
|
2020-10-27 10:55:16 +00:00
|
|
|
name=myname,
|
2020-10-22 12:38:23 +00:00
|
|
|
realname=item.realname)
|
|
|
|
item.survey_mail_send = True
|
2021-04-12 11:58:50 +00:00
|
|
|
item.survey_mail_date = date.today()
|
2020-10-22 12:38:23 +00:00
|
|
|
item.save()
|
2020-10-26 13:46:45 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
|
2020-10-26 12:55:36 +00:00
|
|
|
|
|
|
|
|
2020-10-27 11:37:18 +00:00
|
|
|
''' TODO: there could be some more removing of duplicated code in the following functions '''
|
|
|
|
|
2020-10-26 12:55:36 +00: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))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2020-10-26 14:06:19 +00:00
|
|
|
self.surveymails_to_object(supported,name='library')
|
2020-10-22 11:08:58 +00:00
|
|
|
|
2020-10-26 11:56:29 +00:00
|
|
|
def surveymails_to_hon(self):
|
|
|
|
'''get all HonoraryCertificate objects which where granted two weeks ago'''
|
|
|
|
|
2020-10-26 11:26:41 +00:00
|
|
|
supported = HonoraryCertificate.objects.filter(granted=True)\
|
|
|
|
.filter(granted_date__lt = date.today() - timedelta(days=14))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2021-03-17 12:54:11 +00:00
|
|
|
self.surveymails_to_object(supported, type='HON', name='project')
|
2020-10-26 11:26:41 +00:00
|
|
|
|
2020-10-27 12:08:47 +00: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))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2020-10-27 12:08:47 +00:00
|
|
|
self.surveymails_to_object(supported, type='IFG', name='url')
|
|
|
|
|
2020-10-27 12:47:46 +00:00
|
|
|
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))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2020-10-27 12:47:46 +00:00
|
|
|
self.surveymails_to_object(supported, type='LIT', name='info')
|
|
|
|
|
2020-10-26 11:56:29 +00:00
|
|
|
def surveymails_to_project(self):
|
2020-10-26 12:01:11 +00:00
|
|
|
'''send survey link 4 weeks after end of project reached'''
|
|
|
|
supported = Project.objects.filter(granted=True)\
|
2021-02-08 08:12:56 +00:00
|
|
|
.filter(end__lt = date.today() - timedelta(days=28))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2021-03-17 12:54:11 +00:00
|
|
|
self.surveymails_to_object(supported, type='PRO', name='name')
|
2020-10-26 12:01:11 +00:00
|
|
|
|
2020-10-26 11:56:29 +00:00
|
|
|
def surveymails_to_travel(self):
|
2020-10-26 12:01:11 +00:00
|
|
|
'''send survey link 3 weeks after end of project reached'''
|
2020-10-26 12:23:05 +00:00
|
|
|
|
|
|
|
supported = Travel.objects.filter(project__granted=True)\
|
2021-02-08 08:12:56 +00:00
|
|
|
.filter(project__end__lt = date.today() - timedelta(days=21))\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2021-03-17 11:46:45 +00:00
|
|
|
self.surveymails_to_object(supported, type='TRAV', name='project')
|
2020-10-26 11:56:29 +00:00
|
|
|
|
2020-10-27 10:55:16 +00:00
|
|
|
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'):
|
2020-10-27 11:37:18 +00:00
|
|
|
# get class via string
|
2020-10-27 10:55:16 +00:00
|
|
|
supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
|
|
|
|
.filter(granted_date__lt = lastdate)\
|
2023-02-27 17:09:29 +00:00
|
|
|
.exclude(survey_mail_send=True)\
|
|
|
|
.exclude(mail_state = 'END')
|
2020-10-27 10:55:16 +00:00
|
|
|
self.surveymails_to_object(supported, type=typefield[count])
|
|
|
|
count += 1
|
|
|
|
|
2020-10-26 11:56:29 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
'''the main function which is called by the custom command'''
|
|
|
|
|
|
|
|
self.end_of_projects_reached()
|
2023-02-27 17:09:28 +00:00
|
|
|
self.end_of_projects_approved()
|
2023-02-27 17:09:29 +00:00
|
|
|
self.notHappened_of_projects_approved()
|
2020-10-26 11:56:29 +00:00
|
|
|
self.surveymails_to_lib()
|
|
|
|
self.surveymails_to_hon()
|
2020-10-27 12:08:47 +00:00
|
|
|
self.surveymails_to_ifg()
|
2020-10-27 12:47:46 +00:00
|
|
|
self.surveymails_to_lit()
|
2020-10-26 11:56:29 +00:00
|
|
|
self.surveymails_to_project()
|
|
|
|
self.surveymails_to_travel()
|
2020-10-27 10:55:16 +00:00
|
|
|
self.surveymails_to_mail_vis_lis()
|
2020-10-26 11:56:29 +00:00
|
|
|
|
2020-10-07 09:21:21 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))
|