donation.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.conf import settings
  2. from django.core.mail import EmailMessage
  3. from django.template.loader import get_template
  4. from rest_framework import generics
  5. from desecapi.serializers import DonationSerializer
  6. class DonationList(generics.CreateAPIView):
  7. serializer_class = DonationSerializer
  8. def perform_create(self, serializer):
  9. instance = serializer.save()
  10. context = {
  11. 'donation': instance,
  12. 'creditoridentifier': settings.SEPA['CREDITOR_ID'],
  13. 'creditorname': settings.SEPA['CREDITOR_NAME'],
  14. }
  15. # internal desec notification
  16. content_tmpl = get_template('emails/donation/desec-content.txt')
  17. subject_tmpl = get_template('emails/donation/desec-subject.txt')
  18. attachment_tmpl = get_template('emails/donation/desec-attachment-jameica.txt')
  19. from_tmpl = get_template('emails/from.txt')
  20. email = EmailMessage(subject_tmpl.render(context),
  21. content_tmpl.render(context),
  22. from_tmpl.render(context),
  23. [settings.DEFAULT_FROM_EMAIL],
  24. attachments=[('jameica-directdebit.xml', attachment_tmpl.render(context), 'text/xml')],
  25. reply_to=[instance.email] if instance.email else None
  26. )
  27. email.send()
  28. # donor notification
  29. if instance.email:
  30. content_tmpl = get_template('emails/donation/donor-content.txt')
  31. subject_tmpl = get_template('emails/donation/donor-subject.txt')
  32. email = EmailMessage(subject_tmpl.render(context),
  33. content_tmpl.render(context),
  34. from_tmpl.render(context),
  35. [instance.email])
  36. email.send()