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.

197 lines
8.2 KiB

4 years ago
3 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])
  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. print(Project.objects.filter(end__lt = date.today()))
  42. old = Project.objects.filter(end__lt = date.today())\
  43. .exclude(end_mail_send = True)
  44. mail_template = get_template('input/if_end_of_project.txt')
  45. for project in old:
  46. context = {'project': project}
  47. context['URLPREFIX'] = settings.URLPREFIX
  48. try:
  49. send_mail('Projektende erreicht',
  50. mail_template.render(context),
  51. IF_EMAIL,
  52. [IF_EMAIL],
  53. fail_silently=False)
  54. project.end_mail_send = True
  55. project.save()
  56. except BadHeaderError:
  57. self.stdout.write(self.style.ERROR('Invalid header found.'))
  58. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  59. def end_of_projects_approved(self):
  60. ''' end of project approved '''
  61. # get all projects where end was reached already, and send mails for the ones already set to status "ended" by the admins
  62. approved_end = Project.objects.filter(status = 'END')\
  63. .exclude(end_mail_send = False)
  64. print(approved_end)
  65. mail_template = get_template('input/if_end_of_project.txt')
  66. for project in approved_end:
  67. context = {'project': project}
  68. context['URLPREFIX'] = settings.URLPREFIX
  69. try:
  70. send_mail('Projektende erreicht',
  71. mail_template.render(context),
  72. IF_EMAIL,
  73. [IF_EMAIL],
  74. fail_silently=False)
  75. project.end_mail_send = True
  76. project.save()
  77. except BadHeaderError:
  78. self.stdout.write(self.style.ERROR('Invalid header found.'))
  79. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  80. def surveymails_to_object(self, supported, name='', type='LIB'):
  81. mytype=type
  82. myname = name
  83. for item in supported:
  84. if type == 'LIB':
  85. mytype = item.type
  86. elif type not in ('MAIL','VIS','LIST'):
  87. myname = getattr(item, name, 'ERROR: NONAME')
  88. print(f'name gefunden: {myname}')
  89. self.survey_link(email=item.email,
  90. type=mytype,
  91. pid=f'{mytype}{item.pk}',
  92. name=myname,
  93. realname=item.realname)
  94. item.survey_mail_send = True
  95. item.survey_mail_date = date.today()
  96. item.save()
  97. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  98. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  99. def surveymails_to_lib(self):
  100. '''get all library objects which where granted two weeks ago'''
  101. supported = Library.objects.filter(granted=True)\
  102. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  103. .exclude(survey_mail_send=True)
  104. self.surveymails_to_object(supported,name='library')
  105. def surveymails_to_hon(self):
  106. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  107. supported = HonoraryCertificate.objects.filter(granted=True)\
  108. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  109. .exclude(survey_mail_send=True)
  110. self.surveymails_to_object(supported, type='HON', name='project')
  111. def surveymails_to_ifg(self):
  112. '''get all IFG objects which where granted two weeks ago'''
  113. supported = IFG.objects.filter(granted=True)\
  114. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  115. .exclude(survey_mail_send=True)
  116. self.surveymails_to_object(supported, type='IFG', name='url')
  117. def surveymails_to_lit(self):
  118. '''get all Litearure objects which where granted two weeks ago'''
  119. supported = Literature.objects.filter(granted=True)\
  120. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  121. .exclude(survey_mail_send=True)
  122. self.surveymails_to_object(supported, type='LIT', name='info')
  123. def surveymails_to_project(self):
  124. '''send survey link 4 weeks after end of project reached'''
  125. supported = Project.objects.filter(granted=True)\
  126. .filter(end__lt = date.today() - timedelta(days=28))\
  127. .exclude(survey_mail_send=True)
  128. self.surveymails_to_object(supported, type='PRO', name='name')
  129. def surveymails_to_travel(self):
  130. '''send survey link 3 weeks after end of project reached'''
  131. supported = Travel.objects.filter(project__granted=True)\
  132. .filter(project__end__lt = date.today() - timedelta(days=21))\
  133. .exclude(survey_mail_send=True)
  134. self.surveymails_to_object(supported, type='TRAV', name='project')
  135. def surveymails_to_mail_vis_lis(self):
  136. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  137. lastdate = date.today() - timedelta(days=14)
  138. typefield = ('MAIL','VIS','LIST')
  139. count = 0
  140. for c in ('Email', 'BusinessCard', 'List'):
  141. # get class via string
  142. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  143. .filter(granted_date__lt = lastdate)\
  144. .exclude(survey_mail_send=True)
  145. self.surveymails_to_object(supported, type=typefield[count])
  146. count += 1
  147. def handle(self, *args, **options):
  148. '''the main function which is called by the custom command'''
  149. self.end_of_projects_reached()
  150. self.end_of_projects_approved()
  151. self.surveymails_to_lib()
  152. self.surveymails_to_hon()
  153. self.surveymails_to_ifg()
  154. self.surveymails_to_lit()
  155. self.surveymails_to_project()
  156. self.surveymails_to_travel()
  157. self.surveymails_to_mail_vis_lis()
  158. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))