Test coverage report: ftp_deploy.server.views.service

# ftp_deploy.server.views.service: 146 total statements, 39.5% covered

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

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

Stats: 51 executed, 78 missed, 17 excluded, 87 ignored

1. `import json`
2. ``
3. `from django.views.generic.base import View, TemplateResponseMixin, TemplateView`
4. `from django.views.generic.edit import ModelFormMixin, ProcessFormView`
5. `from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin`
6. `from django.views.generic import ListView, UpdateView, DeleteView, DetailView, CreateView, FormView`
7. `from django.core.urlresolvers import reverse_lazy, reverse`
8. `from django.shortcuts import render_to_response, get_object_or_404`
9. `from django.template import RequestContext`
10. `from django.contrib import messages`
11. `from django.db.models import Max`
12. `from django.http import HttpResponse, Http404`
13. ``
14. `from braces.views import JSONResponseMixin, LoginRequiredMixin`
15. ``
16. `from ftp_deploy.conf import *`
17. `from ftp_deploy.models import Service, Log, Notification`
18. `from ftp_deploy.utils.curl import curl_connection`
19. `from ftp_deploy.utils.core import commits_parser, absolute_url`
20. `from ftp_deploy.server.forms import ServiceForm, ServiceNotificationForm`
21. ``
22. ``
23. `class DashboardView(LoginRequiredMixin, ListView):`
24. ``
25. `"""View for dashboard"""`
26. ``
27. `model = Service`
28. `queryset = Service.objects.all().select_related().order_by("status", "-log__created").annotate(date=Max('log__created'))`
29. `context_object_name = 'services'`
30. `template_name = "ftp_deploy/dashboard.html"`
31. `paginate_by = 25`
32. ``
33. `def post(self, request, *args, **kwargs):`
34. `services = self.get_queryset()`
35. `if self.request.POST['services']:`
36. `services = services.filter(pk=self.request.POST['services'])`
37. `return render_to_response('ftp_deploy/service/list.html', locals(), context_instance=RequestContext(request))`
38. ``
39. ``
40. `class ServiceManageView(LoginRequiredMixin, DetailView):`
41. ``
42. `"""View for manage services"""`
43. ``
44. `model = Service`
45. `context_object_name = 'service'`
46. `template_name = "ftp_deploy/service/manage.html"`
47. ``
48. `def get_context_data(self, **kwargs):`
49. ``
50. `context = super(ServiceManageView, self).get_context_data(**kwargs)`
51. `context['recent_logs'] = self.object.log_set.all()[:15]`
52. `context['fail_logs'] = self.object.log_set.filter(status=0).filter(skip=0)`
53. `return context`
54. ``
55. ``
56. `class ServiceAddView(LoginRequiredMixin, CreateView):`
57. ``
58. `"""View for add serives"""`
59. `model = Service`
60. `form_class = ServiceForm`
61. `success_url = reverse_lazy('ftpdeploy_dashboard')`
62. `template_name = "ftp_deploy/service/form.html"`
63. ``
64. `def form_valid(self, form):`
65. `messages.add_message(self.request, messages.SUCCESS, 'Service has been added.')`
66. `return super(ServiceAddView, self).form_valid(form)`
67. ``
68. ``
69. `class ServiceEditView(LoginRequiredMixin, UpdateView):`
70. ``
71. `"""View for edit services"""`
72. `model = Service`
73. `form_class = ServiceForm`
74. `template_name = "ftp_deploy/service/form.html"`
75. ``
76. `def get_success_url(self):`
77. `return reverse('ftpdeploy_service_manage', kwargs={'pk': self.kwargs['pk']})`
78. ``
79. `def form_valid(self, form):`
80. `messages.add_message(self.request, messages.SUCCESS, 'Service has been updated.')`
81. `return super(ServiceEditView, self).form_valid(form)`
82. ``
83. ``
84. `class ServiceDeleteView(LoginRequiredMixin, DeleteView):`
85. ``
86. `"""View for delete services"""`
87. ``
88. `model = Service`
89. `success_url = reverse_lazy('ftpdeploy_dashboard')`
90. `template_name = "ftp_deploy/service/delete.html"`
91. ``
92. `def delete(self, request, *args, **kwargs):`
93. `messages.add_message(request, messages.SUCCESS, 'Service has been removed.')`
94. `return super(ServiceDeleteView, self).delete(request, *args, **kwargs)`
95. ``
96. ``
97. `class ServiceStatusView(JSONResponseMixin, LoginRequiredMixin, SingleObjectMixin, View):`
98. ``
99. `"""View for update(save) and check service status"""`
100. ``
101. `model = Service`
102. ``
103. `def post(self, request, *args, **kwargs):`
104. `service = self.get_object()`
105. `service.save()`
106. `response = request.POST.get('response', '')`
107. ``
108. `if response == 'list':`
109. `services = list()`
110. `services.append(service)`
111. `return render_to_response('ftp_deploy/service/list.html', locals(), context_instance=RequestContext(request))`
112. ``
113. `if response == 'manage':`
114. `recent_logs = service.log_set.all()[:15]`
115. `fail_logs = service.log_set.filter(status=0).filter(skip=0)`
116. `return render_to_response('ftp_deploy/service/manage.html', locals(), context_instance=RequestContext(request))`
117. ``
118. `if response == 'json':`
119. `context = {`
120. `'status': service.status,`
121. `'status_message': service.status_message,`
122. `'updated': service.updated`
123. `}`
124. `return self.render_json_response(context)`
125. ``
126. `raise Http404`
127. ``
128. ``
129. `class ServiceRestoreView(LoginRequiredMixin, DetailView):`
130. ``
131. `""""View for build restore path for service"""`
132. ``
133. `model = Service`
134. `prefetch_related = ["log_set"]`
135. `template_name = "ftp_deploy/service/restore-modal.html"`
136. ``
137. `def get_context_data(self, **kwargs):`
138. `context = super(ServiceRestoreView, self).get_context_data(**kwargs)`
139. ``
140. `logs = self.get_logs_tree()`
141. ``
142. `context['payload'] = json.loads(logs[0].payload)`
143. `context['payload']['user'] = 'Restore'`
144. `context['service'] = self.get_object()`
145. ``
146. `commits = list()`
147. `for log in logs:`
148. `payload = json.loads(log.payload)`
149. `commits += payload['commits']`
150. ``
151. `context['payload']['commits'] = commits`
152. `context['payload'] = json.dumps(context['payload'])`
153. ``
154. `context['files_added'], context['files_modified'], context['files_removed'] = commits_parser(commits).file_diff()`
155. `context['commits_info'] = commits_parser(commits).commits_info()`
156. ``
157. `return context`
158. ``
159. `def post(self, request, *args, **kwargs):`
160. ``
161. `if request.POST['payload']:`
162. `self.get_logs_tree().delete()`
163. ``
164. `return HttpResponse(reverse('ftpdeploy_deploy', args=(self.get_object().secret_key,)))`
165. ``
166. `def get_logs_tree(self):`
167. `"""get logs tree for restore deploys. Include all logs since first fail apart of skiped."""`
168. `first_fail_log = self.get_object().log_set.filter(status=0).filter(skip=0).order_by('pk')[:1]`
169. `logs = self.get_object().log_set.filter(skip=0).filter(pk__gte=first_fail_log[0].pk).order_by('pk')`
170. `return logs`
171. ``
172. ``
173. `class ServiceNotificationView(LoginRequiredMixin, UpdateView):`
174. ``
175. `model = Service`
176. `form_class = ServiceNotificationForm`
177. `template_name = "ftp_deploy/notification/notification-modal.html"`
178. ``
179. `def get_success_url(self):`
180. `return reverse('ftpdeploy_service_manage', kwargs={'pk': self.kwargs['pk']})`
181. ``
182. `def form_valid(self, form):`
183. `self.object.check = False`
184. `messages.add_message(self.request, messages.SUCCESS, 'Service notification has been updated.')`
185. `return super(ServiceNotificationView, self).form_valid(form)`
186. ``
187. ``
188. `class BitbucketAPIView(LoginRequiredMixin, JSONResponseMixin, SingleObjectMixin, View):`
189. ``
190. `"""View for managing BitBucket API"""`
191. ``
192. `model = Service`
193. ``
194. `def dispatch(self, *args, **kwargs):`
195. `self.bitbucket_username = BITBUCKET_SETTINGS['username']`
196. `self.bitbucket_password = BITBUCKET_SETTINGS['password']`
197. `return super(BitbucketAPIView, self).dispatch(*args, **kwargs)`
198. ``
199. `def post(self, request, *args, **kwargs):`
200. `try:`
201. `curl = curl_connection(self.bitbucket_username, self.bitbucket_password)`
202. `curl.authenticate()`
203. `post = str()`
204. ``
205. `if self.request.POST['data'] == 'respositories':`
206. `context = self.repositories(curl)`
207. `elif self.request.POST['data'] == 'addhook':`
208. `context = self.add_hook(curl, request)`
209. ``
210. `return self.render_json_response(context)`
211. ``
212. `finally:`
213. `curl.close()`
214. ``
215. `return HttpResponse()`
216. ``
217. `def repositories(self, curl):`
218. `"""Load list of repositories from bitbucket account"""`
219. ``
220. `url = 'https://bitbucket.org/api/1.0/user/repositories'`
221. `context = curl.perform(url)`
222. `return context`
223. ``
224. `def add_hook(self, curl, request):`
225. `"""Add hook and change repo_hook flag for service"""`
226. ``
227. `service = self.get_object()`
228. `url = 'https://api.bitbucket.org/1.0/repositories/%s/%s/services/ ' % (self.bitbucket_username, self.get_object().repo_slug_name)`
229. `post = 'type=POST&URL=%s%s' % (absolute_url(request).build(), service.hook_url())`
230. `service.repo_hook = True`
231. `service.save()`
232. `context = curl.perform_post(url, post)`
233. `return context`