You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.0 KiB

  1. from datetime import date, timedelta
  2. from django.core.management.base import BaseCommand, CommandError
  3. from django.template.loader import get_template
  4. from django.core.mail import send_mail, BadHeaderError
  5. from input.models import Project, Library, HonoraryCertificate, Travel
  6. from input.settings import URLPREFIX, IF_EMAIL, SURVEYPREFIX
  7. class Command(BaseCommand):
  8. ''' mails will be send here:
  9. - two weeks after confirmation of support for volunteer (/extern) send link
  10. with surveylink
  11. - same for HonoraryCertificate (/intern)
  12. - travel: mail 3 weeks after end of project.
  13. - assumed end of project (/project) reached: mail to IF, link to project-editpage
  14. - 4 weeks after end of project reached: mail with surveylink
  15. '''
  16. help = '''This command sends mail with some links to the database or to the survey
  17. after some amount of time.'''
  18. def survey_link(self, email, type, pid, name, realname):
  19. context = {'realname': realname,
  20. 'type': type,
  21. 'name': name,
  22. 'pid': pid,
  23. 'SURVEYPREFIX': SURVEYPREFIX, }
  24. mail_template = get_template('input/survey_mail.txt')
  25. try:
  26. send_mail('Projektende erreicht',
  27. mail_template.render(context),
  28. IF_EMAIL,
  29. [email],
  30. fail_silently=False)
  31. except BadHeaderError:
  32. return HttpResponse('Invalid header found.')
  33. print(f'send surveylinkemail to {email}...')
  34. def end_of_projects_reached(self):
  35. ''' end of project reached '''
  36. # get all projects which ended
  37. old = Project.objects.filter(end__lt = date.today())\
  38. .exclude(end_mail_send = True)
  39. mail_template = get_template('input/if_end_of_project.txt')
  40. for project in old:
  41. context = {'project': project}
  42. context['URLPREFIX'] = URLPREFIX
  43. try:
  44. send_mail('Projektende erreicht',
  45. mail_template.render(context),
  46. IF_EMAIL,
  47. [IF_EMAIL],
  48. fail_silently=False)
  49. project.end_mail_send = True
  50. project.save()
  51. except BadHeaderError:
  52. self.stdout.write(self.style.ERROR('Invalid header found.'))
  53. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  54. def surveymails_to_object(self, supported, name, type='LIB'):
  55. mytype=type
  56. for item in supported:
  57. if type == 'LIB':
  58. mytype = item.type
  59. self.survey_link(email=item.email,
  60. type=mytype,
  61. pid=9999, ## TODO
  62. name=getattr(item,name),
  63. realname=item.realname)
  64. item.survey_mail_send = True
  65. item.save()
  66. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  67. def surveymails_to_lib(self):
  68. '''get all library objects which where granted two weeks ago'''
  69. supported = Library.objects.filter(granted=True)\
  70. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  71. .exclude(survey_mail_send=True)
  72. self.surveymails_to_object(supported,name='name')
  73. def surveymails_to_hon(self):
  74. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  75. supported = HonoraryCertificate.objects.filter(granted=True)\
  76. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  77. .exclude(survey_mail_send=True)
  78. self.surveymails_to_object(supported, type='HON', name='request_url')
  79. def surveymails_to_project(self):
  80. '''send survey link 4 weeks after end of project reached'''
  81. supported = Project.objects.filter(granted=True)\
  82. .filter(granted_date__lt = date.today() - timedelta(days=28))\
  83. .exclude(survey_mail_send=True)
  84. self.surveymails_to_object(supported, type='PRO', name='realname')
  85. def surveymails_to_travel(self):
  86. '''send survey link 3 weeks after end of project reached'''
  87. supported = Travel.objects.filter(project__granted=True)\
  88. .filter(project__granted_date__lt = date.today() - timedelta(days=21))\
  89. .exclude(survey_mail_send=True)
  90. self.surveymails_to_object(supported, type='TRAV', name='request_url')
  91. def handle(self, *args, **options):
  92. '''the main function which is called by the custom command'''
  93. self.end_of_projects_reached()
  94. self.surveymails_to_lib()
  95. self.surveymails_to_hon()
  96. self.surveymails_to_project()
  97. self.surveymails_to_travel()
  98. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))