utilities.py 1.3 KB

1234567891011121314151617181920212223242526272829
  1. from django.template.loader import render_to_string
  2. from django.core.signing import Signer
  3. from datetime import datetime
  4. from os.path import splitext
  5. from board.settings import ALLOWED_HOSTS
  6. signer = Signer()
  7. def send_activation_notification(user):
  8. if ALLOWED_HOSTS:
  9. host = 'https://' + ALLOWED_HOSTS[0]
  10. context = {'user': user, 'host': host, 'sign': signer.sign(user.username)}
  11. subject = render_to_string('email/activation_letter_subject.txt', context)
  12. body_text = render_to_string('email/activation_letter_body.txt', context)
  13. user.email_user(subject, body_text)
  14. # Графические файлы, сохраняемые в папке image, будут иметь в качестве имен текущие временные отметки
  15. def get_timestamp_path(instance, filename):
  16. return '%s%s' % (datetime.now().timestamp(), splitext(filename)[1])
  17. def send_new_comment_notification(comment):
  18. if ALLOWED_HOSTS:
  19. host = 'https://' + ALLOWED_HOSTS[0]
  20. author = comment.ad.author
  21. context = {'author': author, 'host': host, 'comment': comment}
  22. subject = render_to_string('email/new_comment_letter_subject.txt', context)
  23. body_text = render_to_string('email/new_comment_letter_body.txt', context)
  24. author.email_user(subject, body_text)