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.

252 lines
11 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.core.mail import EmailMultiAlternatives
  7. from django.conf import settings
  8. from input.models import Project, Library, HonoraryCertificate, Travel, Email,\
  9. BusinessCard, List, IFG, Literature
  10. from input.settings import IF_EMAIL, SURVEYPREFIX, SURVEY_EMAIL
  11. class Command(BaseCommand):
  12. ''' mails will be send here:
  13. - two weeks after confirmation of support for volunteer (/extern) send link
  14. with surveylink
  15. - same for HonoraryCertificate (/intern)
  16. - travel: mail 3 weeks after end of project.
  17. - assumed end of project (/project) reached: mail to IF, link to project-editpage
  18. - 4 weeks after end of project reached: mail with surveylink
  19. '''
  20. help = '''This command sends mail with some links to the database or to the survey
  21. after some amount of time.'''
  22. def survey_link(self, email, type, pid, name, realname):
  23. context = {'realname': realname,
  24. 'type': type,
  25. 'name': name,
  26. 'pid': pid,
  27. 'SURVEYPREFIX': SURVEYPREFIX, }
  28. mail_template = get_template('input/survey_mail.txt')
  29. try:
  30. survey_mail = EmailMessage('Dein Feedback zur Förderung durch Wikimedia Deutschland',
  31. mail_template.render(context),
  32. IF_EMAIL,
  33. [email],
  34. bcc=[SURVEY_EMAIL])
  35. survey_mail.send(fail_silently=False)
  36. except BadHeaderError:
  37. return HttpResponse('Invalid header found.')
  38. print(f'send surveylinkemail to {email}...')
  39. def end_of_projects_reached(self):
  40. ''' end of project reached '''
  41. # get all projects which ended
  42. print(Project.objects.filter(end__lt = date.today()))
  43. old = Project.objects.filter(end__lt = date.today())\
  44. .exclude(end_mail_send = True)
  45. mail_template = get_template('input/if_end_of_project.txt')
  46. for project in old:
  47. context = {'project': project}
  48. context['URLPREFIX'] = settings.URLPREFIX
  49. try:
  50. send_mail('Projektende erreicht',
  51. mail_template.render(context),
  52. IF_EMAIL,
  53. ['luca@cannabinieri.de'],
  54. fail_silently=False)
  55. project.end_mail_send = True
  56. project.save()
  57. except BadHeaderError:
  58. self.stdout.write(self.style.ERROR('Invalid header found.'))
  59. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  60. def end_of_projects_approved(self):
  61. ''' end of project approved '''
  62. # get all projects where end was reached already, and send mails for the ones already set to status "ended" by the admins
  63. approved_end = Project.objects.filter(status = 'END')\
  64. .exclude(end_mail_send = False)
  65. print(approved_end)
  66. mail_template = get_template('input/if_end_of_project_approved.txt')
  67. informMail_template = get_template('input/if_end_of_project_orginformed.txt')
  68. # send the mail to project.email, which would be the mail of the volunteer filling out the form
  69. for project in approved_end:
  70. context = {'project': project}
  71. context['URLPREFIX'] = settings.URLPREFIX
  72. try:
  73. send_mail('Projektende erreicht',
  74. mail_template.render(context),
  75. IF_EMAIL,
  76. [project.email],
  77. fail_silently=False)
  78. send_mail('Projektorganisator*in wurde informiert',
  79. informMail_template.render(context),
  80. IF_EMAIL,
  81. [IF_EMAIL],
  82. fail_silently=False)
  83. project.end_mail_send = True
  84. project.save()
  85. except BadHeaderError:
  86. self.stdout.write(self.style.ERROR('Invalid header found.'))
  87. self.stdout.write(self.style.SUCCESS('end_of_projects_approved() executed.'))
  88. def notHappened_of_projects_approved(self):
  89. ''' notHappened of project approved '''
  90. # get all projects where end was reached already, and send mails for the ones where status was put to NOT by admins
  91. approved_notHappened = Project.objects.filter(status = 'NOT')\
  92. .exclude(end_mail_send = False)
  93. html_mail_template = get_template('input/if_not_of_project_approved.html')
  94. txt_mail_template = get_template('input/if_not_of_project_approved.txt')
  95. informMail_template = get_template('input/if_end_of_project_orginformed.txt')
  96. # send the mail to project.email, which would be the mail of the volunteer that filled out the form
  97. for project in approved_notHappened:
  98. context = {'project': project}
  99. context['URLPREFIX'] = settings.URLPREFIX
  100. try:
  101. subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
  102. text_content = txt_mail_template.render(context)
  103. html_content = html_mail_template.render(context)
  104. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  105. msg.attach_alternative(html_content, "text/html")
  106. msg.send()
  107. #send_mail('Projektende erreicht',
  108. # mail_template.render(context),
  109. # IF_EMAIL,
  110. # [project.email],
  111. # fail_silently=False)
  112. send_mail('Projektorganisator*in wurde informiert',
  113. informMail_template.render(context),
  114. IF_EMAIL,
  115. [IF_EMAIL],
  116. fail_silently=False)
  117. project.end_mail_send = True
  118. project.save()
  119. except BadHeaderError:
  120. self.stdout.write(self.style.ERROR('Invalid header found.'))
  121. self.stdout.write(self.style.SUCCESS('notHappened_of_projects_approved() executed.'))
  122. def surveymails_to_object(self, supported, name='', type='LIB'):
  123. mytype=type
  124. myname = name
  125. for item in supported:
  126. if type == 'LIB':
  127. mytype = item.type
  128. elif type not in ('MAIL','VIS','LIST'):
  129. myname = getattr(item, name, 'ERROR: NONAME')
  130. print(f'name gefunden: {myname}')
  131. self.survey_link(email=item.email,
  132. type=mytype,
  133. pid=f'{mytype}{item.pk}',
  134. name=myname,
  135. realname=item.realname)
  136. item.survey_mail_send = True
  137. item.survey_mail_date = date.today()
  138. item.save()
  139. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  140. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  141. def surveymails_to_lib(self):
  142. '''get all library objects which where granted two weeks ago'''
  143. supported = Library.objects.filter(granted=True)\
  144. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  145. .exclude(survey_mail_send=True)
  146. self.surveymails_to_object(supported,name='library')
  147. def surveymails_to_hon(self):
  148. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  149. supported = HonoraryCertificate.objects.filter(granted=True)\
  150. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  151. .exclude(survey_mail_send=True)
  152. self.surveymails_to_object(supported, type='HON', name='project')
  153. def surveymails_to_ifg(self):
  154. '''get all IFG objects which where granted two weeks ago'''
  155. supported = IFG.objects.filter(granted=True)\
  156. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  157. .exclude(survey_mail_send=True)
  158. self.surveymails_to_object(supported, type='IFG', name='url')
  159. def surveymails_to_lit(self):
  160. '''get all Litearure objects which where granted two weeks ago'''
  161. supported = Literature.objects.filter(granted=True)\
  162. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  163. .exclude(survey_mail_send=True)
  164. self.surveymails_to_object(supported, type='LIT', name='info')
  165. def surveymails_to_project(self):
  166. '''send survey link 4 weeks after end of project reached'''
  167. supported = Project.objects.filter(granted=True)\
  168. .filter(end__lt = date.today() - timedelta(days=28))\
  169. .exclude(survey_mail_send=True)
  170. self.surveymails_to_object(supported, type='PRO', name='name')
  171. def surveymails_to_travel(self):
  172. '''send survey link 3 weeks after end of project reached'''
  173. supported = Travel.objects.filter(project__granted=True)\
  174. .filter(project__end__lt = date.today() - timedelta(days=21))\
  175. .exclude(survey_mail_send=True)
  176. self.surveymails_to_object(supported, type='TRAV', name='project')
  177. def surveymails_to_mail_vis_lis(self):
  178. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  179. lastdate = date.today() - timedelta(days=14)
  180. typefield = ('MAIL','VIS','LIST')
  181. count = 0
  182. for c in ('Email', 'BusinessCard', 'List'):
  183. # get class via string
  184. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  185. .filter(granted_date__lt = lastdate)\
  186. .exclude(survey_mail_send=True)
  187. self.surveymails_to_object(supported, type=typefield[count])
  188. count += 1
  189. def handle(self, *args, **options):
  190. '''the main function which is called by the custom command'''
  191. self.end_of_projects_reached()
  192. self.end_of_projects_approved()
  193. self.notHappened_of_projects_approved()
  194. self.surveymails_to_lib()
  195. self.surveymails_to_hon()
  196. self.surveymails_to_ifg()
  197. self.surveymails_to_lit()
  198. self.surveymails_to_project()
  199. self.surveymails_to_travel()
  200. self.surveymails_to_mail_vis_lis()
  201. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))