Test coverage report: ftp_deploy.views

# ftp_deploy.views: 89 total statements, 5.3% covered

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

Source file: /var/www/service.dev/service/ftp_deploy/views.py

Stats: 4 executed, 71 missed, 14 excluded, 38 ignored

1. `# -*- coding: utf-8 -*-`
2. `import os`
3. `import json`
4. `from ftplib import FTP`
5. `import tempfile`
6. ``
7. `from django.views.generic.base import View`
8. `from django.http import HttpResponse, Http404`
9. `from django.utils.decorators import method_decorator`
10. `from django.views.decorators.csrf import csrf_exempt`
11. ``
12. `from .models import Log, Service`
13. `from .conf import *`
14. `from .utils.core import absolute_url, LockError`
15. `from .utils.ftp import ftp_connection`
16. `from .utils.email import notification_success, notification_fail`
17. `from .utils.curl import curl_connection`
18. ``
19. ``
20. `class DeployView(View):`
21. ``
22. `"""Main view receive POST Hook from repository"""`
23. ``
24. `@method_decorator(csrf_exempt)`
25. `def dispatch(self, *args, **kwargs):`
26. `try:`
27. `self.service = Service.objects.get(secret_key=kwargs['secret_key'])`
28. `except Exception, e:`
29. `raise Http404`
30. ``
31. `self.status = 200`
32. ``
33. `self.bitbucket_username = BITBUCKET_SETTINGS['username']`
34. `self.bitbucket_password = BITBUCKET_SETTINGS['password']`
35. `self.bitbucket_branch = self.service.repo_branch`
36. ``
37. `self.ftp_host = self.service.ftp_host`
38. `self.ftp_username = self.service.ftp_username`
39. `self.ftp_password = self.service.ftp_password`
40. `self.ftp_path = self.service.ftp_path`
41. ``
42. `return super(DeployView, self).dispatch(*args, **kwargs)`
43. ``
44. `def post(self, request, *args, **kwargs):`
45. ``
46. `self.json_string = request.POST['payload'].decode('string_escape').replace('\n', '')`
47. `self.data = json.loads(self.json_string)`
48. `last_commit = len(self.data['commits']) - 1`
49. ``
50. ``
51. `if self.data['commits'][last_commit]['branch'] == self.bitbucket_branch:`
52. `self.log = Log()`
53. `self.log.payload = self.json_string`
54. `self.log.service = self.service`
55. `self.log.save()`
56. ``
57. `try:`
58. `self.ftp = ftp_connection(self.ftp_host, self.ftp_username, self.ftp_password, self.ftp_path)`
59. ``
60. `if self.service.lock:`
61. `raise LockError()`
62. ``
63. `self.service.check = False`
64. `self.service.lock = True`
65. `self.service.save()`
66. ``
67. `self.ftp.connect()`
68. ``
69. `except LockError, e:`
70. `self.set_fail(request, 'Service Locked', e)`
71. `except Exception, e:`
72. `self.set_fail(request, 'FTP Connection', e)`
73. `else:`
74. `try:`
75. `curl = curl_connection(self.bitbucket_username, self.bitbucket_password)`
76. `curl.authenticate()`
77. ``
78. `for i, commit in enumerate(self.data['commits']):`
79. ``
80. `for files in commit['files']:`
81. `file_path = files['file']`
82. ``
83. `if files['type'] == 'removed':`
84. `self.ftp.remove_file(file_path)`
85. `else:`
86. `url = 'https://api.bitbucket.org/1.0/repositories%sraw/%s/%s' % (self.data['repository']['absolute_url'], commit['node'], file_path)`
87. `url = str(url.encode('utf-8'))`
88. ``
89. `value = curl.perform(url)`
90. ``
91. `temp_file = tempfile.NamedTemporaryFile(delete=False)`
92. `temp_file.write(value)`
93. `temp_file.close()`
94. `temp_file = open(temp_file.name, 'rb')`
95. ``
96. `self.ftp.make_dirs(file_path)`
97. `self.ftp.create_file(file_path, temp_file)`
98. ``
99. `temp_file.close()`
100. `os.unlink(temp_file.name)`
101. ``
102. `except Exception, e:`
103. `self.set_fail(request, self.data['user'], e)`
104. `else:`
105. `self.log.user = self.data['user']`
106. `self.log.status = True`
107. `self.log.save()`
108. `notification_success(absolute_url(request).build(), self.service, self.json_string)`
109. `finally:`
110. `curl.close()`
111. ``
112. `finally:`
113. `self.ftp.quit()`
114. `self.service.lock = False`
115. `self.service.check = True`
116. `self.service.save()`
117. ``
118. `return HttpResponse(status=self.status)`
119. ``
120. `def set_fail(self, request, user, message):`
121. `self.log.user = user`
122. `self.log.status_message = message`
123. `self.log.status = False`
124. `self.log.save()`
125. `self.service.status = False`
126. `self.status = 500`
127. `notification_fail(absolute_url(request).build(), self.service, self.json_string, message)`