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.

261 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. txt_informMail_template = get_template('input/if_end_of_project_orginformed.txt')
  96. html_informMail_template = get_template('input/if_end_of_project_orginformed.html')
  97. # send the mail to project.email, which would be the mail of the volunteer that filled out the form
  98. for project in approved_notHappened:
  99. context = {'project': project}
  100. context['URLPREFIX'] = settings.URLPREFIX
  101. try:
  102. subject, from_email, to = 'Projektende erreicht', IF_EMAIL, project.email
  103. text_content = txt_mail_template.render(context)
  104. html_content = html_mail_template.render(context)
  105. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  106. msg.attach_alternative(html_content, "text/html")
  107. msg.send()
  108. #send_mail('Projektende erreicht',
  109. # mail_template.render(context),
  110. # IF_EMAIL,
  111. # [project.email],
  112. # fail_silently=False)
  113. inform_subject, inform_from_email, inform_to = Projektorganisator*in wurde informiert', IF_EMAIL, IF_EMAIL
  114. inform_text_content = txt_informMail_template.render(context)
  115. inform_html_content = html_informMail_template.render(context)
  116. inform_msg = EmailMultiAlternatives(inform_subject, inform_text_content, inform_from_email, [inform_to])
  117. inform_msg.attach_alternative(html_content, "text/html")
  118. inform_msg.send()
  119. #send_mail('Projektorganisator*in wurde informiert',
  120. # informMail_template.render(context),
  121. # IF_EMAIL,
  122. # [IF_EMAIL],
  123. # fail_silently=False)
  124. project.end_mail_send = True
  125. project.save()
  126. except BadHeaderError:
  127. self.stdout.write(self.style.ERROR('Invalid header found.'))
  128. self.stdout.write(self.style.SUCCESS('notHappened_of_projects_approved() executed.'))
  129. def surveymails_to_object(self, supported, name='', type='LIB'):
  130. mytype=type
  131. myname = name
  132. for item in supported:
  133. if type == 'LIB':
  134. mytype = item.type
  135. elif type not in ('MAIL','VIS','LIST'):
  136. myname = getattr(item, name, 'ERROR: NONAME')
  137. print(f'name gefunden: {myname}')
  138. self.survey_link(email=item.email,
  139. type=mytype,
  140. pid=f'{mytype}{item.pk}',
  141. name=myname,
  142. realname=item.realname)
  143. item.survey_mail_send = True
  144. item.survey_mail_date = date.today()
  145. item.save()
  146. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  147. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  148. def surveymails_to_lib(self):
  149. '''get all library objects which where granted two weeks ago'''
  150. supported = Library.objects.filter(granted=True)\
  151. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  152. .exclude(survey_mail_send=True)
  153. self.surveymails_to_object(supported,name='library')
  154. def surveymails_to_hon(self):
  155. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  156. supported = HonoraryCertificate.objects.filter(granted=True)\
  157. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  158. .exclude(survey_mail_send=True)
  159. self.surveymails_to_object(supported, type='HON', name='project')
  160. def surveymails_to_ifg(self):
  161. '''get all IFG objects which where granted two weeks ago'''
  162. supported = IFG.objects.filter(granted=True)\
  163. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  164. .exclude(survey_mail_send=True)
  165. self.surveymails_to_object(supported, type='IFG', name='url')
  166. def surveymails_to_lit(self):
  167. '''get all Litearure objects which where granted two weeks ago'''
  168. supported = Literature.objects.filter(granted=True)\
  169. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  170. .exclude(survey_mail_send=True)
  171. self.surveymails_to_object(supported, type='LIT', name='info')
  172. def surveymails_to_project(self):
  173. '''send survey link 4 weeks after end of project reached'''
  174. supported = Project.objects.filter(granted=True)\
  175. .filter(end__lt = date.today() - timedelta(days=28))\
  176. .exclude(survey_mail_send=True)
  177. self.surveymails_to_object(supported, type='PRO', name='name')
  178. def surveymails_to_travel(self):
  179. '''send survey link 3 weeks after end of project reached'''
  180. supported = Travel.objects.filter(project__granted=True)\
  181. .filter(project__end__lt = date.today() - timedelta(days=21))\
  182. .exclude(survey_mail_send=True)
  183. self.surveymails_to_object(supported, type='TRAV', name='project')
  184. def surveymails_to_mail_vis_lis(self):
  185. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  186. lastdate = date.today() - timedelta(days=14)
  187. typefield = ('MAIL','VIS','LIST')
  188. count = 0
  189. for c in ('Email', 'BusinessCard', 'List'):
  190. # get class via string
  191. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  192. .filter(granted_date__lt = lastdate)\
  193. .exclude(survey_mail_send=True)
  194. self.surveymails_to_object(supported, type=typefield[count])
  195. count += 1
  196. def handle(self, *args, **options):
  197. '''the main function which is called by the custom command'''
  198. self.end_of_projects_reached()
  199. self.end_of_projects_approved()
  200. self.notHappened_of_projects_approved()
  201. self.surveymails_to_lib()
  202. self.surveymails_to_hon()
  203. self.surveymails_to_ifg()
  204. self.surveymails_to_lit()
  205. self.surveymails_to_project()
  206. self.surveymails_to_travel()
  207. self.surveymails_to_mail_vis_lis()
  208. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))