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.

50 lines
1.7 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
  6. from input.settings import URLPREFIX, IF_EMAIL
  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. '''
  15. help = '''This command sends mail with some links to the database or to the survey
  16. after some amount of time.'''
  17. def handle(self, *args, **options):
  18. # get all projects which ended 3 weeks ago
  19. old = Project.objects.filter(end__lt = date.today() - timedelta(days=21))\
  20. .exclude(end_mail_send = True)
  21. print(old)
  22. mail_template = get_template('input/if_end_of_project.txt')
  23. for project in old:
  24. context = {'project': project}
  25. context['URLPREFIX'] = URLPREFIX
  26. print(context)
  27. try:
  28. send_mail('Projektende erreicht',
  29. mail_template.render(context),
  30. IF_EMAIL,
  31. [IF_EMAIL],
  32. fail_silently=False)
  33. project.end_mail_send = True
  34. project.save()
  35. except BadHeaderError:
  36. return HttpResponse('Invalid header found.')
  37. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))