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.

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