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.

166 lines
6.9 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
  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
  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. send_mail('Dein Feedback zur Förderung durch Wikimedia Deutschland',
  30. mail_template.render(context),
  31. IF_EMAIL,
  32. [email],
  33. fail_silently=False)
  34. except BadHeaderError:
  35. return HttpResponse('Invalid header found.')
  36. print(f'send surveylinkemail to {email}...')
  37. def end_of_projects_reached(self):
  38. ''' end of project reached '''
  39. # get all projects which ended
  40. old = Project.objects.filter(end__lt = date.today())\
  41. .exclude(end_mail_send = True)
  42. mail_template = get_template('input/if_end_of_project.txt')
  43. for project in old:
  44. context = {'project': project}
  45. context['URLPREFIX'] = settings.URLPREFIX
  46. try:
  47. send_mail('Projektende erreicht',
  48. mail_template.render(context),
  49. IF_EMAIL,
  50. [IF_EMAIL],
  51. fail_silently=False)
  52. project.end_mail_send = True
  53. project.save()
  54. except BadHeaderError:
  55. self.stdout.write(self.style.ERROR('Invalid header found.'))
  56. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  57. def surveymails_to_object(self, supported, name='', type='LIB'):
  58. mytype=type
  59. myname = name
  60. for item in supported:
  61. if type == 'LIB':
  62. mytype = item.type
  63. elif type not in ('MAIL','VIS','LIST'):
  64. myname = getattr(item,name)
  65. self.survey_link(email=item.email,
  66. type=mytype,
  67. pid=f'{mytype}{item.pk}',
  68. name=myname,
  69. realname=item.realname)
  70. item.survey_mail_send = True
  71. item.save()
  72. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  73. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  74. def surveymails_to_lib(self):
  75. '''get all library objects which where granted two weeks ago'''
  76. supported = Library.objects.filter(granted=True)\
  77. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  78. .exclude(survey_mail_send=True)
  79. self.surveymails_to_object(supported,name='library')
  80. def surveymails_to_hon(self):
  81. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  82. supported = HonoraryCertificate.objects.filter(granted=True)\
  83. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  84. .exclude(survey_mail_send=True)
  85. self.surveymails_to_object(supported, type='HON', name='request_url')
  86. def surveymails_to_ifg(self):
  87. '''get all IFG objects which where granted two weeks ago'''
  88. supported = IFG.objects.filter(granted=True)\
  89. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  90. .exclude(survey_mail_send=True)
  91. self.surveymails_to_object(supported, type='IFG', name='url')
  92. def surveymails_to_lit(self):
  93. '''get all Litearure objects which where granted two weeks ago'''
  94. supported = Literature.objects.filter(granted=True)\
  95. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  96. .exclude(survey_mail_send=True)
  97. self.surveymails_to_object(supported, type='LIT', name='info')
  98. def surveymails_to_project(self):
  99. '''send survey link 4 weeks after end of project reached'''
  100. supported = Project.objects.filter(granted=True)\
  101. .filter(end__lt = date.today() - timedelta(days=28))\
  102. .exclude(survey_mail_send=True)
  103. self.surveymails_to_object(supported, type='PRO', name='realname')
  104. def surveymails_to_travel(self):
  105. '''send survey link 3 weeks after end of project reached'''
  106. supported = Travel.objects.filter(project__granted=True)\
  107. .filter(project__end__lt = date.today() - timedelta(days=21))\
  108. .exclude(survey_mail_send=True)
  109. self.surveymails_to_object(supported, type='TRAV', name='request_url')
  110. def surveymails_to_mail_vis_lis(self):
  111. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  112. lastdate = date.today() - timedelta(days=14)
  113. typefield = ('MAIL','VIS','LIST')
  114. count = 0
  115. for c in ('Email', 'BusinessCard', 'List'):
  116. # get class via string
  117. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  118. .filter(granted_date__lt = lastdate)\
  119. .exclude(survey_mail_send=True)
  120. self.surveymails_to_object(supported, type=typefield[count])
  121. count += 1
  122. def handle(self, *args, **options):
  123. '''the main function which is called by the custom command'''
  124. self.end_of_projects_reached()
  125. self.surveymails_to_lib()
  126. self.surveymails_to_hon()
  127. self.surveymails_to_ifg()
  128. self.surveymails_to_lit()
  129. self.surveymails_to_project()
  130. self.surveymails_to_travel()
  131. self.surveymails_to_mail_vis_lis()
  132. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))