ftp_deploy.utils.email: 80 total statements, 93.2% covered

Generated: Sun 2014-03-16 19:26 GMT

Source file: /var/www/service.dev/service/ftp_deploy/utils/email.py

Stats: 68 executed, 5 missed, 7 excluded, 47 ignored

  1. import json
  2. from abc import ABCMeta, abstractmethod
  3. from django.core.mail import EmailMultiAlternatives
  4. from django.template.loader import render_to_string
  5. from ftp_deploy.conf import *
  6. from .repo import commits_parser, repository_parser
  7. from .curl import curl_connection
  8. class notification():
  9. """Notification abstract class, take three arguments, host, service object and payload json string"""
  10. __metaclass__ = ABCMeta
  11. def __init__(self, host, service, payload):
  12. self.host = host
  13. self.service = service
  14. self.payload = json.loads(payload)
  15. self.commits = self.payload['commits']
  16. self.from_email = 'noreply@ftpdeploy.com'
  17. self.repo = repository_parser(self.payload, self.service)
  18. self.send()
  19. @property
  20. def template_html(self):
  21. raise NotImplementedError
  22. @property
  23. def template_text(self):
  24. raise NotImplementedError
  25. @abstractmethod
  26. def subject(self):
  27. pass
  28. @abstractmethod
  29. def emails(self):
  30. pass
  31. @abstractmethod
  32. def context(self):
  33. pass
  34. def send(self):
  35. """Sent method process emails from list returned by emails() method"""
  36. for recipient in self.emails():
  37. text_content = render_to_string(self.template_text, self.context())
  38. html_content = render_to_string(self.template_html, self.context())
  39. msg = EmailMultiAlternatives(self.subject(), text_content, self.from_email, [recipient])
  40. msg.attach_alternative(html_content, "text/html")
  41. msg.send()
  42. class notification_success(notification):
  43. """Notification class for success"""
  44. template_html = 'ftp_deploy/email/email_success.html'
  45. template_text = 'ftp_deploy/email/email_success.txt'
  46. def subject(self):
  47. return '%s - Deploy Successfully' % self.service
  48. def emails(self):
  49. emails_list = list()
  50. notifications = self.service.notification
  51. if notifications:
  52. emails_list += notifications.get_success()
  53. if notifications.deploy_user_success():
  54. emails_list += self.repo.deploy_email()
  55. if notifications.commit_user_success():
  56. emails_list += commits_parser(self.commits, self.service.repo_source).email_list()
  57. return list(set(emails_list))
  58. def context(self):
  59. context = dict()
  60. context['service'] = self.service
  61. context['host'] = self.host
  62. context['commits_info'] = commits_parser(self.commits, self.service.repo_source).commits_info()
  63. context['files_added'], context['files_modified'], context['files_removed'] = commits_parser(self.commits, self.service.repo_source).file_diff()
  64. return context
  65. class notification_fail(notification):
  66. """Notification class for fail"""
  67. template_html = 'ftp_deploy/email/email_fail.html'
  68. template_text = 'ftp_deploy/email/email_fail.txt'
  69. def __init__(self, host, service, payload, error):
  70. self.error = error
  71. super(notification_fail, self).__init__(host, service, payload)
  72. def subject(self):
  73. return '%s - Deploy Fail' % self.service
  74. def emails(self):
  75. emails_list = list()
  76. notifications = self.service.notification
  77. if notifications:
  78. emails_list += notifications.get_fail()
  79. if notifications.deploy_user_fail():
  80. emails_list += self.repo.deploy_email()
  81. if notifications.commit_user_fail():
  82. emails_list += commits_parser(self.commits, self.service.repo_source).email_list()
  83. return list(set(emails_list))
  84. def context(self):
  85. context = dict()
  86. context['host'] = self.host
  87. context['service'] = self.service
  88. context['error'] = self.error
  89. return context