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.

169 lines
7.1 KiB

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