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.

146 lines
5.9 KiB

  1. from datetime import date, timedelta
  2. import sys
  3. from django.core.management.base import BaseCommand, CommandError
  4. from django.template.loader import get_template
  5. from django.core.mail import send_mail, BadHeaderError
  6. from input.models import Project, Library, HonoraryCertificate, Travel, Email, BusinessCard, List
  7. from input.settings import URLPREFIX, IF_EMAIL, SURVEYPREFIX
  8. class Command(BaseCommand):
  9. ''' mails will be send here:
  10. - two weeks after confirmation of support for volunteer (/extern) send link
  11. with surveylink
  12. - same for HonoraryCertificate (/intern)
  13. - travel: mail 3 weeks after end of project.
  14. - assumed end of project (/project) reached: mail to IF, link to project-editpage
  15. - 4 weeks after end of project reached: mail with surveylink
  16. '''
  17. help = '''This command sends mail with some links to the database or to the survey
  18. after some amount of time.'''
  19. def survey_link(self, email, type, pid, name, realname):
  20. context = {'realname': realname,
  21. 'type': type,
  22. 'name': name,
  23. 'pid': pid,
  24. 'SURVEYPREFIX': SURVEYPREFIX, }
  25. mail_template = get_template('input/survey_mail.txt')
  26. try:
  27. send_mail('Projektende erreicht',
  28. mail_template.render(context),
  29. IF_EMAIL,
  30. [email],
  31. fail_silently=False)
  32. except BadHeaderError:
  33. return HttpResponse('Invalid header found.')
  34. print(f'send surveylinkemail to {email}...')
  35. def end_of_projects_reached(self):
  36. ''' end of project reached '''
  37. # get all projects which ended
  38. old = Project.objects.filter(end__lt = date.today())\
  39. .exclude(end_mail_send = True)
  40. mail_template = get_template('input/if_end_of_project.txt')
  41. for project in old:
  42. context = {'project': project}
  43. context['URLPREFIX'] = URLPREFIX
  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_object(self, supported, name='', type='LIB'):
  56. mytype=type
  57. myname = name
  58. for item in supported:
  59. if type == 'LIB':
  60. mytype = item.type
  61. elif type not in ('MAIL','VIS','LIST'):
  62. myname = getattr(item,name)
  63. self.survey_link(email=item.email,
  64. type=mytype,
  65. pid=f'{mytype}{item.pk}',
  66. name=myname,
  67. realname=item.realname)
  68. item.survey_mail_send = True
  69. item.save()
  70. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  71. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  72. def surveymails_to_lib(self):
  73. '''get all library objects which where granted two weeks ago'''
  74. supported = Library.objects.filter(granted=True)\
  75. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  76. .exclude(survey_mail_send=True)
  77. self.surveymails_to_object(supported,name='library')
  78. def surveymails_to_hon(self):
  79. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  80. supported = HonoraryCertificate.objects.filter(granted=True)\
  81. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  82. .exclude(survey_mail_send=True)
  83. self.surveymails_to_object(supported, type='HON', name='request_url')
  84. def surveymails_to_project(self):
  85. '''send survey link 4 weeks after end of project reached'''
  86. supported = Project.objects.filter(granted=True)\
  87. .filter(granted_date__lt = date.today() - timedelta(days=28))\
  88. .exclude(survey_mail_send=True)
  89. self.surveymails_to_object(supported, type='PRO', name='realname')
  90. def surveymails_to_travel(self):
  91. '''send survey link 3 weeks after end of project reached'''
  92. supported = Travel.objects.filter(project__granted=True)\
  93. .filter(project__granted_date__lt = date.today() - timedelta(days=21))\
  94. .exclude(survey_mail_send=True)
  95. self.surveymails_to_object(supported, type='TRAV', name='request_url')
  96. def surveymails_to_mail_vis_lis(self):
  97. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  98. lastdate = date.today() - timedelta(days=14)
  99. typefield = ('MAIL','VIS','LIST')
  100. count = 0
  101. for c in ('Email', 'BusinessCard', 'List'):
  102. # get class via string
  103. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  104. .filter(granted_date__lt = lastdate)\
  105. .exclude(survey_mail_send=True)
  106. self.surveymails_to_object(supported, type=typefield[count])
  107. count += 1
  108. def handle(self, *args, **options):
  109. '''the main function which is called by the custom command'''
  110. self.end_of_projects_reached()
  111. self.surveymails_to_lib()
  112. self.surveymails_to_hon()
  113. self.surveymails_to_project()
  114. self.surveymails_to_travel()
  115. self.surveymails_to_mail_vis_lis()
  116. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))