ftp_deploy.server.views.api: 51 total statements, 72.1% covered

Generated: Sun 2014-01-12 11:05 GMT

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

Stats: 31 executed, 12 missed, 8 excluded, 32 ignored

  1. from django.views.generic.base import View
  2. from django.views.generic.detail import SingleObjectMixin
  3. from django.http import HttpResponse
  4. from braces.views import JSONResponseMixin, LoginRequiredMixin
  5. from ftp_deploy.conf import *
  6. from ftp_deploy.models import Service
  7. from ftp_deploy.utils.curl import curl_connection
  8. from ftp_deploy.utils.core import absolute_url
  9. class BitbucketAPIView(LoginRequiredMixin, JSONResponseMixin, SingleObjectMixin, View):
  10. """View for managing BitBucket API"""
  11. model = Service
  12. def dispatch(self, request, *args, **kwargs):
  13. self.bitbucket_username = BITBUCKET_SETTINGS['username']
  14. self.bitbucket_password = BITBUCKET_SETTINGS['password']
  15. return super(BitbucketAPIView, self).dispatch(request, *args, **kwargs)
  16. def get(self, request, *args, **kwargs):
  17. curl = curl_connection(self.bitbucket_username, self.bitbucket_password)
  18. curl.authenticate()
  19. url = 'https://api.bitbucket.org/1.0/repositories'
  20. post = 'name=project_name&is_private=True'
  21. context = curl.perform_post(url,post)
  22. curl.close()
  23. return HttpResponse(context)
  24. def post(self, request, *args, **kwargs):
  25. try:
  26. curl = curl_connection(self.bitbucket_username, self.bitbucket_password)
  27. curl.authenticate()
  28. post = str()
  29. if self.request.POST['data'] == 'respositories':
  30. context = self.repositories(curl)
  31. elif self.request.POST['data'] == 'addhook':
  32. context = self.add_hook(curl, request)
  33. return self.render_json_response(context)
  34. finally:
  35. curl.close()
  36. return HttpResponse()
  37. def repositories(self, curl):
  38. """Load list of repositories from bitbucket account"""
  39. url = 'https://bitbucket.org/api/1.0/user/repositories'
  40. context = curl.perform(url)
  41. return context
  42. def add_hook(self, curl, request):
  43. """Add hook and change repo_hook flag for service"""
  44. service = self.get_object()
  45. url = 'https://api.bitbucket.org/1.0/repositories/%s/%s/services/ ' % (self.bitbucket_username, self.get_object().repo_slug_name)
  46. post = 'type=POST&URL=%s%s' % (absolute_url(request).build(), service.hook_url())
  47. service.repo_hook = True
  48. service.save()
  49. context = curl.perform_post(url, post)
  50. return context
  51. def add_respository(self, curl):
  52. """Add respository to repository account"""
  53. url = 'https://api.bitbucket.org/1.0/repositories'
  54. post = 'name=project_name'
  55. context = curl.perform_post(url,post)
  56. return context