ftp_deploy.utils.email: 91 total statements, 79.8% covered

Generated: Thu 2013-12-19 21:13 GMT

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

Stats: 67 executed, 17 missed, 7 excluded, 50 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 .core import commits_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.user = self.payload['user']
  17. self.from_email = 'noreply@ftpdeploy.com'
  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 deploy_user(self):
  35. """Method return email of deploy user"""
  36. if self.payload['user'] == 'Restore':
  37. return []
  38. try:
  39. curl = curl_connection(BITBUCKET_SETTINGS['username'], BITBUCKET_SETTINGS['password'])
  40. curl.authenticate()
  41. url = 'https://bitbucket.org/api/1.0/users/%s/emails' % self.payload['user']
  42. context = json.loads(curl.perform(url))
  43. return [context[0]['email']]
  44. except Exception, e:
  45. return []
  46. def send(self):
  47. """Sent method process emails from list returned by emails() method"""
  48. for recipient in self.emails():
  49. text_content = render_to_string(self.template_text, self.context())
  50. html_content = render_to_string(self.template_html, self.context())
  51. msg = EmailMultiAlternatives(self.subject(), text_content, self.from_email, [recipient])
  52. msg.attach_alternative(html_content, "text/html")
  53. msg.send()
  54. class notification_success(notification):
  55. """Notification class for success"""
  56. template_html = 'ftp_deploy/email/email_success.html'
  57. template_text = 'ftp_deploy/email/email_success.txt'
  58. def subject(self):
  59. return '%s - Deploy Successfully' % self.service
  60. def emails(self):
  61. emails_list = list()
  62. notifications = self.service.notification
  63. if notifications:
  64. emails_list += notifications.get_success()
  65. if notifications.deploy_user_success():
  66. emails_list += self.deploy_user()
  67. if notifications.commit_user_success():
  68. emails_list += commits_parser(self.commits).email_list()
  69. return list(set(emails_list))
  70. def context(self):
  71. context = dict()
  72. context['service'] = self.service
  73. context['host'] = self.host
  74. context['commits_info'] = commits_parser(self.commits).commits_info()
  75. context['files_added'], context['files_modified'], context['files_removed'] = commits_parser(self.commits).file_diff()
  76. return context
  77. class notification_fail(notification):
  78. """Notification class for fail"""
  79. template_html = 'ftp_deploy/email/email_fail.html'
  80. template_text = 'ftp_deploy/email/email_fail.txt'
  81. def __init__(self, host, service, payload, error):
  82. self.error = error
  83. super(notification_fail, self).__init__(host, service, payload)
  84. def subject(self):
  85. return '%s - Deploy Fail' % self.service
  86. def emails(self):
  87. emails_list = list()
  88. notifications = self.service.notification
  89. if notifications:
  90. emails_list += notifications.get_fail()
  91. if notifications.deploy_user_fail():
  92. emails_list += self.deploy_user()
  93. if notifications.commit_user_fail():
  94. emails_list += commits_parser(self.commits).email_list()
  95. return list(set(emails_list))
  96. def context(self):
  97. context = dict()
  98. context['host'] = self.host
  99. context['service'] = self.service
  100. context['error'] = self.error
  101. return context