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.

88 lines
3.1 KiB

  1. from datetime import date, timedelta
  2. from django.core.management.base import BaseCommand, CommandError
  3. from django.template.loader import get_template
  4. from django.core.mail import send_mail, BadHeaderError
  5. from input.models import Project, Library
  6. from input.settings import URLPREFIX, IF_EMAIL, SURVEYPREFIX
  7. class Command(BaseCommand):
  8. ''' mails will be send here:
  9. - two weeks after confirmation of support for volunteer (/extern) send link
  10. with surveylink
  11. - same for HonoraryCertificate and accreditation (/intern)
  12. - travel: mail 3 weeks after end of project.
  13. - assumed end of project (/project) reached: mail to IF, link to project-editpage
  14. - 4 weeks after end of project reached: mail with surveylink
  15. '''
  16. help = '''This command sends mail with some links to the database or to the survey
  17. after some amount of time.'''
  18. def survey_link(self, email, type, pid, name, realname):
  19. context = {'realname': realname,
  20. 'type': type,
  21. 'name': name,
  22. 'pid': pid,
  23. 'SURVEYPREFIX': SURVEYPREFIX, }
  24. mail_template = get_template('input/survey_mail.txt')
  25. try:
  26. send_mail('Projektende erreicht',
  27. mail_template.render(context),
  28. IF_EMAIL,
  29. [email],
  30. fail_silently=False)
  31. except BadHeaderError:
  32. return HttpResponse('Invalid header found.')
  33. print(f'send surveylinkemail to {email}...')
  34. def end_of_projects_reached(self):
  35. ''' end of project reached '''
  36. # get all projects which ended
  37. # - timedelta(days=21))
  38. old = Project.objects.filter(end__lt = date.today())\
  39. .exclude(end_mail_send = True)
  40. mail_template = get_template('input/if_end_of_project.txt')
  41. for project in old:
  42. context = {'project': project}
  43. context['URLPREFIX'] = URLPREFIX
  44. print(context)
  45. try:
  46. send_mail('Projektende erreicht',
  47. mail_template.render(context),
  48. IF_EMAIL,
  49. [IF_EMAIL],
  50. fail_silently=False)
  51. project.end_mail_send = True
  52. project.save()
  53. except BadHeaderError:
  54. self.stdout.write(self.style.ERROR('Invalid header found.'))
  55. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  56. def handle(self, *args, **options):
  57. self.end_of_projects_reached()
  58. supported = Library.objects.filter(granted=True)\
  59. .exclude(survey_mail_send=True)
  60. print(supported)
  61. for item in supported:
  62. self.survey_link(email=item.email,
  63. type=item.type,
  64. pid=9999, ## TODO
  65. name=item.library,
  66. realname=item.realname)
  67. item.survey_mail_send = True
  68. item.save()
  69. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))