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.

131 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
  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. - TODO: travel: mail 3 weeks after end of project.
  13. - assumed end of project (/project) reached: mail to IF, link to project-editpage
  14. - TODO: 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. print(context)
  44. try:
  45. send_mail('Projektende erreicht',
  46. mail_template.render(context),
  47. IF_EMAIL,
  48. [IF_EMAIL],
  49. fail_silently=False)
  50. project.end_mail_send = True
  51. project.save()
  52. except BadHeaderError:
  53. self.stdout.write(self.style.ERROR('Invalid header found.'))
  54. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  55. def surveymails_to_lib(self):
  56. '''get all library objects which where granted two weeks ago'''
  57. supported = Library.objects.filter(granted=True)\
  58. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  59. .exclude(survey_mail_send=True)
  60. print(supported)
  61. for item in supported:
  62. self.survey_link(email=item.email,
  63. type=item.type,
  64. pid=9999, ## TODO
  65. name=item.library,
  66. realname=item.realname)
  67. item.survey_mail_send = True
  68. item.save()
  69. def surveymails_to_hon(self):
  70. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  71. supported = HonoraryCertificate.objects.filter(granted=True)\
  72. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  73. .exclude(survey_mail_send=True)
  74. print(supported)
  75. for item in supported:
  76. self.survey_link(email=item.email,
  77. type='HON',
  78. pid=9999, ## TODO
  79. name=item.request_url,
  80. realname=item.realname)
  81. item.survey_mail_send = True
  82. item.save()
  83. def surveymails_to_project(self):
  84. '''send survey link 4 weeks after end of project reached'''
  85. supported = Project.objects.filter(granted=True)\
  86. .filter(granted_date__lt = date.today() - timedelta(days=28))\
  87. .exclude(survey_mail_send=True)
  88. print(supported)
  89. for item in supported:
  90. self.survey_link(email=item.email,
  91. type='PRO',
  92. pid=9999, ## TODO
  93. name=item.name,
  94. realname=item.realname)
  95. item.survey_mail_send = True
  96. item.save()
  97. def surveymails_to_travel(self):
  98. '''send survey link 3 weeks after end of project reached'''
  99. pass
  100. def handle(self, *args, **options):
  101. '''the main function which is called by the custom command'''
  102. self.end_of_projects_reached()
  103. self.surveymails_to_lib()
  104. self.surveymails_to_hon()
  105. self.surveymails_to_project()
  106. self.surveymails_to_travel()
  107. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))