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.

167 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, 'ERROR: NONAME')
  65. print(f'name gefunden: {myname}')
  66. self.survey_link(email=item.email,
  67. type=mytype,
  68. pid=f'{mytype}{item.pk}',
  69. name=myname,
  70. realname=item.realname)
  71. item.survey_mail_send = True
  72. item.save()
  73. self.stdout.write(self.style.SUCCESS(f'surveymails for object type {type} sent'))
  74. ''' TODO: there could be some more removing of duplicated code in the following functions '''
  75. def surveymails_to_lib(self):
  76. '''get all library objects which where granted two weeks ago'''
  77. supported = Library.objects.filter(granted=True)\
  78. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  79. .exclude(survey_mail_send=True)
  80. self.surveymails_to_object(supported,name='library')
  81. def surveymails_to_hon(self):
  82. '''get all HonoraryCertificate objects which where granted two weeks ago'''
  83. supported = HonoraryCertificate.objects.filter(granted=True)\
  84. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  85. .exclude(survey_mail_send=True)
  86. self.surveymails_to_object(supported, type='HON', name='project')
  87. def surveymails_to_ifg(self):
  88. '''get all IFG objects which where granted two weeks ago'''
  89. supported = IFG.objects.filter(granted=True)\
  90. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  91. .exclude(survey_mail_send=True)
  92. self.surveymails_to_object(supported, type='IFG', name='url')
  93. def surveymails_to_lit(self):
  94. '''get all Litearure objects which where granted two weeks ago'''
  95. supported = Literature.objects.filter(granted=True)\
  96. .filter(granted_date__lt = date.today() - timedelta(days=14))\
  97. .exclude(survey_mail_send=True)
  98. self.surveymails_to_object(supported, type='LIT', name='info')
  99. def surveymails_to_project(self):
  100. '''send survey link 4 weeks after end of project reached'''
  101. supported = Project.objects.filter(granted=True)\
  102. .filter(end__lt = date.today() - timedelta(days=28))\
  103. .exclude(survey_mail_send=True)
  104. self.surveymails_to_object(supported, type='PRO', name='name')
  105. def surveymails_to_travel(self):
  106. '''send survey link 3 weeks after end of project reached'''
  107. supported = Travel.objects.filter(project__granted=True)\
  108. .filter(project__end__lt = date.today() - timedelta(days=21))\
  109. .exclude(survey_mail_send=True)
  110. self.surveymails_to_object(supported, type='TRAV', name='project')
  111. def surveymails_to_mail_vis_lis(self):
  112. '''send survey link 2 weeks after mailadresss, mailinglist or businesscards are granted'''
  113. lastdate = date.today() - timedelta(days=14)
  114. typefield = ('MAIL','VIS','LIST')
  115. count = 0
  116. for c in ('Email', 'BusinessCard', 'List'):
  117. # get class via string
  118. supported = getattr(sys.modules[__name__], c).objects.filter(granted=True)\
  119. .filter(granted_date__lt = lastdate)\
  120. .exclude(survey_mail_send=True)
  121. self.surveymails_to_object(supported, type=typefield[count])
  122. count += 1
  123. def handle(self, *args, **options):
  124. '''the main function which is called by the custom command'''
  125. self.end_of_projects_reached()
  126. self.surveymails_to_lib()
  127. self.surveymails_to_hon()
  128. self.surveymails_to_ifg()
  129. self.surveymails_to_lit()
  130. self.surveymails_to_project()
  131. self.surveymails_to_travel()
  132. self.surveymails_to_mail_vis_lis()
  133. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))