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.

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