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.

241 lines
10 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. ['luca@cannabinieri.de'],
  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_approved.txt')
  66. informMail_template = get_template('input/if_end_of_project_orginformed.txt')
  67. # send the mail to project.email, which would be the mail of the volunteer filling out the form
  68. for project in approved_end:
  69. context = {'project': project}
  70. context['URLPREFIX'] = settings.URLPREFIX
  71. try:
  72. send_mail('Projektende erreicht',
  73. mail_template.render(context),
  74. IF_EMAIL,
  75. [project.email],
  76. fail_silently=False)
  77. send_mail('Projektorganisator*in wurde informiert',
  78. informMail_template.render(context),
  79. IF_EMAIL,
  80. [IF_EMAIL],
  81. fail_silently=False)
  82. project.end_mail_send = True
  83. project.save()
  84. except BadHeaderError:
  85. self.stdout.write(self.style.ERROR('Invalid header found.'))
  86. self.stdout.write(self.style.SUCCESS('end_of_projects_approved() executed.'))
  87. def notHappened_of_projects_approved(self):
  88. ''' notHappened of project approved '''
  89. # get all projects where end was reached already, and send mails for the ones where status was put to NOT by admins
  90. approved_notHappened = Project.objects.filter(status = 'NOT')\
  91. .exclude(end_mail_send = False)
  92. mail_template = get_template('input/if_not_of_project_approved.txt')
  93. informMail_template = get_template('input/if_end_of_project_orginformed.txt')
  94. # send the mail to project.email, which would be the mail of the volunteer that filled out the form
  95. for project in approved_notHappened:
  96. context = {'project': project}
  97. context['URLPREFIX'] = settings.URLPREFIX
  98. try:
  99. send_mail('Projektende erreicht',
  100. mail_template.render(context),
  101. IF_EMAIL,
  102. [project.email],
  103. fail_silently=False)
  104. send_mail('Projektorganisator*in wurde informiert',
  105. informMail_template.render(context),
  106. IF_EMAIL,
  107. [IF_EMAIL],
  108. fail_silently=False)
  109. project.end_mail_send = True
  110. project.save()
  111. except BadHeaderError:
  112. self.stdout.write(self.style.ERROR('Invalid header found.'))
  113. self.stdout.write(self.style.SUCCESS('notHappened_of_projects_approved() executed.'))
  114. def surveymails_to_object(self, supported, name='', type='LIB'):
  115. mytype=type
  116. myname = name
  117. for item in supported:
  118. if type == 'LIB':
  119. mytype = item.type
  120. elif type not in ('MAIL','VIS','LIST'):
  121. myname = getattr(item, name, 'ERROR: NONAME')
  122. print(f'name gefunden: {myname}')
  123. self.survey_link(email=item.email,
  124. type=mytype,
  125. pid=f'{mytype}{item.pk}',
  126. name=myname,
  127. realname=item.realname)
  128. item.survey_mail_send = True
  129. item.survey_mail_date = date.today()
  130. item.save()
  131. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  132. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  133. def surveymails_to_lib(self):
  134. '''get all library objects which where granted two weeks ago'''
  135. supported = Library.objects.filter(granted=True)\
  136. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  137. .exclude(survey_mail_send=True)
  138. self.surveymails_to_object(supported,name='library')
  139. def surveymails_to_hon(self):
  140. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  141. supported = HonoraryCertificate.objects.filter(granted=True)\
  142. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  143. .exclude(survey_mail_send=True)
  144. self.surveymails_to_object(supported, type='HON', name='project')
  145. def surveymails_to_ifg(self):
  146. '''get all IFG objects which where granted two weeks ago'''
  147. supported = IFG.objects.filter(granted=True)\
  148. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  149. .exclude(survey_mail_send=True)
  150. self.surveymails_to_object(supported, type='IFG', name='url')
  151. def surveymails_to_lit(self):
  152. '''get all Litearure objects which where granted two weeks ago'''
  153. supported = Literature.objects.filter(granted=True)\
  154. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  155. .exclude(survey_mail_send=True)
  156. self.surveymails_to_object(supported, type='LIT', name='info')
  157. def surveymails_to_project(self):
  158. '''send survey link 4 weeks after end of project reached'''
  159. supported = Project.objects.filter(granted=True)\
  160. .filter(end__lt = date.today() - timedelta(days=28))\
  161. .exclude(survey_mail_send=True)
  162. self.surveymails_to_object(supported, type='PRO', name='name')
  163. def surveymails_to_travel(self):
  164. '''send survey link 3 weeks after end of project reached'''
  165. supported = Travel.objects.filter(project__granted=True)\
  166. .filter(project__end__lt = date.today() - timedelta(days=21))\
  167. .exclude(survey_mail_send=True)
  168. self.surveymails_to_object(supported, type='TRAV', name='project')
  169. def surveymails_to_mail_vis_lis(self):
  170. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  171. lastdate = date.today() - timedelta(days=14)
  172. typefield = ('MAIL','VIS','LIST')
  173. count = 0
  174. for c in ('Email', 'BusinessCard', 'List'):
  175. # get class via string
  176. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  177. .filter(granted_date__lt = lastdate)\
  178. .exclude(survey_mail_send=True)
  179. self.surveymails_to_object(supported, type=typefield[count])
  180. count += 1
  181. def handle(self, *args, **options):
  182. '''the main function which is called by the custom command'''
  183. self.end_of_projects_reached()
  184. self.end_of_projects_approved()
  185. self.notHappened_of_projects_approved()
  186. self.surveymails_to_lib()
  187. self.surveymails_to_hon()
  188. self.surveymails_to_ifg()
  189. self.surveymails_to_lit()
  190. self.surveymails_to_project()
  191. self.surveymails_to_travel()
  192. self.surveymails_to_mail_vis_lis()
  193. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))