Test coverage report: ftp_deploy.utils.email

# 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. ``
3. `from abc import ABCMeta, abstractmethod`
4. `from django.core.mail import EmailMultiAlternatives`
5. `from django.template.loader import render_to_string`
6. ``
7. `from ftp_deploy.conf import *`
8. `from .core import commits_parser`
9. `from .curl import curl_connection`
10. ``
11. ``
12. `class notification():`
13. ``
14. `"""Notification abstract class, take three arguments, host, service object and payload json string"""`
15. `__metaclass__ = ABCMeta`
16. ``
17. `def __init__(self, host, service, payload):`
18. `self.host = host`
19. `self.service = service`
20. `self.payload = json.loads(payload)`
21. `self.commits = self.payload['commits']`
22. `self.user = self.payload['user']`
23. `self.from_email = 'noreply@ftpdeploy.com'`
24. `self.send()`
25. ``
26. `@property`
27. `def template_html(self):`
28. `raise NotImplementedError`
29. ``
30. `@property`
31. `def template_text(self):`
32. `raise NotImplementedError`
33. ``
34. `@abstractmethod`
35. `def subject(self):`
36. `pass`
37. ``
38. `@abstractmethod`
39. `def emails(self):`
40. `pass`
41. ``
42. `@abstractmethod`
43. `def context(self):`
44. `pass`
45. ``
46. `def deploy_user(self):`
47. `"""Method return email of deploy user"""`
48. `if self.payload['user'] == 'Restore':`
49. `return []`
50. ``
51. `try:`
52. `curl = curl_connection(BITBUCKET_SETTINGS['username'], BITBUCKET_SETTINGS['password'])`
53. `curl.authenticate()`
54. `url = 'https://bitbucket.org/api/1.0/users/%s/emails' % self.payload['user']`
55. `context = json.loads(curl.perform(url))`
56. `return [context[0]['email']]`
57. `except Exception, e:`
58. `return []`
59. ``
60. `def send(self):`
61. `"""Sent method process emails from list returned by emails() method"""`
62. ``
63. `for recipient in self.emails():`
64. `text_content = render_to_string(self.template_text, self.context())`
65. `html_content = render_to_string(self.template_html, self.context())`
66. ``
67. `msg = EmailMultiAlternatives(self.subject(), text_content, self.from_email, [recipient])`
68. `msg.attach_alternative(html_content, "text/html")`
69. `msg.send()`
70. ``
71. ``
72. `class notification_success(notification):`
73. ``
74. `"""Notification class for success"""`
75. ``
76. `template_html = 'ftp_deploy/email/email_success.html'`
77. `template_text = 'ftp_deploy/email/email_success.txt'`
78. ``
79. `def subject(self):`
80. `return '%s - Deploy Successfully' % self.service`
81. ``
82. `def emails(self):`
83. `emails_list = list()`
84. `notifications = self.service.notification`
85. ``
86. `if notifications:`
87. `emails_list += notifications.get_success()`
88. ``
89. `if notifications.deploy_user_success():`
90. `emails_list += self.deploy_user()`
91. ``
92. `if notifications.commit_user_success():`
93. `emails_list += commits_parser(self.commits).email_list()`
94. ``
95. `return list(set(emails_list))`
96. ``
97. `def context(self):`
98. `context = dict()`
99. `context['service'] = self.service`
100. `context['host'] = self.host`
101. `context['commits_info'] = commits_parser(self.commits).commits_info()`
102. `context['files_added'], context['files_modified'], context['files_removed'] = commits_parser(self.commits).file_diff()`
103. `return context`
104. ``
105. ``
106. `class notification_fail(notification):`
107. ``
108. `"""Notification class for fail"""`
109. ``
110. `template_html = 'ftp_deploy/email/email_fail.html'`
111. `template_text = 'ftp_deploy/email/email_fail.txt'`
112. ``
113. `def __init__(self, host, service, payload, error):`
114. `self.error = error`
115. `super(notification_fail, self).__init__(host, service, payload)`
116. ``
117. `def subject(self):`
118. `return '%s - Deploy Fail' % self.service`
119. ``
120. `def emails(self):`
121. `emails_list = list()`
122. `notifications = self.service.notification`
123. ``
124. `if notifications:`
125. ``
126. `emails_list += notifications.get_fail()`
127. ``
128. `if notifications.deploy_user_fail():`
129. `emails_list += self.deploy_user()`
130. ``
131. `if notifications.commit_user_fail():`
132. `emails_list += commits_parser(self.commits).email_list()`
133. ``
134. `return list(set(emails_list))`
135. ``
136. `def context(self):`
137. `context = dict()`
138. `context['host'] = self.host`
139. `context['service'] = self.service`
140. `context['error'] = self.error`
141. `return context`