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.

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