utils.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import hashlib
  4. import binascii
  5. import base64
  6. import html2text
  7. from django.core.mail import EmailMultiAlternatives
  8. from django.template import TemplateDoesNotExist
  9. from django.core.files.storage import FileSystemStorage
  10. from django.conf import settings
  11. # Stockage des fichiers privés (comme les factures par exemple)
  12. def private_files_storage():
  13. return FileSystemStorage(location=settings.PRIVATE_FILES_ROOT)
  14. def str_or_none(obj):
  15. return str(obj) if obj else None
  16. def ldap_hash(password):
  17. """Hash a password for use with LDAP. If the password is already hashed,
  18. do nothing."""
  19. if password and not password.startswith('{SSHA}'):
  20. salt = binascii.hexlify(os.urandom(8))
  21. digest = hashlib.sha1(password.encode() + salt).digest()
  22. return '{SSHA}' + base64.b64encode(digest + salt).decode()
  23. else:
  24. return password
  25. if __name__ == '__main__':
  26. print(ldap_hash('coin'))
  27. def send_templated_email(subject, to, template_to_use, context, attachements, from_email=None):
  28. """
  29. Send a multialternative email based on html and optional txt template.
  30. """
  31. # Get default sender if not specified
  32. from_email = from_email if from_email else 'coin@illyse.org'
  33. # Ensure arrays when needed
  34. if not isinstance(to, list):
  35. to = [to]
  36. if not isinstance(attachements, list):
  37. attachements = [attachements]
  38. # If .html is specified in template name remove it
  39. if template_to_use.endswith('.html'):
  40. template_to_use = template_to_use[:-5]
  41. # Get html template, fail if not exists
  42. template_html = get_template('%s.html' % (template_to_use,))
  43. html_content = template_html.render(Context(context))
  44. # Try to get a txt version, convert from html to markdown style
  45. # (using html2text) if fail
  46. try:
  47. template_txt = get_template('%s.txt' % (template_to_use,))
  48. text_content = template_txt.render_to_string(Context(context))
  49. except TemplateDoesNotExist:
  50. text_content = html2text.html2text(html_content)
  51. # make multipart email default : text, alternative : html
  52. msg = EmailMultiAlternatives(subject, text_content, from_email, to)
  53. msg.attach_alternative(html_content, "text/html")
  54. # Set attachements
  55. for attachement in attachements:
  56. msg.attach_file(attachement)
  57. #Send email
  58. msg.send()
  59. def delete_selected(modeladmin, request, queryset):
  60. """Overrides QuerySet's delete() function to remove objects one by one
  61. so, that they are deleted in the LDAP (Redmine issue #195)."""
  62. for obj in queryset:
  63. obj.delete()
  64. delete_selected.short_description = "Supprimer tous les objets sélectionnés."