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.

70 lines
2.4 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
  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. print(f'send surveylinkemail to {email}...')
  20. def end_of_projects_reached(self):
  21. ''' end of project reached '''
  22. # get all projects which ended
  23. # - timedelta(days=21))
  24. old = Project.objects.filter(end__lt = date.today())\
  25. .exclude(end_mail_send = True)
  26. mail_template = get_template('input/if_end_of_project.txt')
  27. for project in old:
  28. context = {'project': project}
  29. context['URLPREFIX'] = URLPREFIX
  30. print(context)
  31. try:
  32. send_mail('Projektende erreicht',
  33. mail_template.render(context),
  34. IF_EMAIL,
  35. [IF_EMAIL],
  36. fail_silently=False)
  37. project.end_mail_send = True
  38. project.save()
  39. except BadHeaderError:
  40. return HttpResponse('Invalid header found.')
  41. self.stdout.write(self.style.SUCCESS('end_of_projects_reached() executed.'))
  42. def handle(self, *args, **options):
  43. self.end_of_projects_reached()
  44. supported = Library.objects.filter(granted=True)
  45. print(supported)
  46. for item in supported:
  47. self.survey_link(email=item.email,
  48. type=item.type,
  49. pid=9999, ## TODO
  50. name=item.library,
  51. realname=item.realname)
  52. self.stdout.write(self.style.SUCCESS('sendmails custom command executed'))