import json
import requests

from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from requests.exceptions import MissingSchema

from bluebottle.clients import properties


class PayoutValidationError(Exception):
    pass


class PayoutCreationError(Exception):
    pass


class DoradoPayoutAdapter(object):

    def __init__(self, project):
        self.settings = getattr(properties, 'PAYOUT_SERVICE', None)
        self.project = project
        self.tenant = connection.tenant

    def trigger_payout(self):
        # Send the signal to Dorado
        data = {
            'project_id': self.project.id,
            'tenant': self.tenant.schema_name
        }

        try:
            response = requests.post(self.settings['url'], data)
            response.raise_for_status()

            self.project.payout_status = 'created'
            self.project.save()
        except requests.HTTPError:
            try:
                raise PayoutValidationError(json.loads(response.content))
            except ValueError:
                raise PayoutCreationError(response.content)
        except MissingSchema:
            raise ImproperlyConfigured("Incorrect Payout URL")
        except IOError, e:
            raise PayoutCreationError(unicode(e))
        except TypeError:
            raise ImproperlyConfigured("Invalid Payout settings")

import json
import requests

from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from requests.exceptions import MissingSchema

from bluebottle.clients import properties


class PayoutValidationError(Exception):
    pass


class PayoutCreationError(Exception):
    pass


class DoradoPayoutAdapter(object):

    def __init__(self, project):
        self.settings = getattr(properties, 'PAYOUT_SERVICE', None)
        self.project = project
        self.tenant = connection.tenant

    def trigger_payout(self):
        # Send the signal to Dorado
        data = {
            'project_id': self.project.id,
            'tenant': self.tenant.schema_name
        }

        try:
            self.project.payout_status = 'created'
            self.project.save()

            response = requests.post(self.settings['url'], data)
            response.raise_for_status()
        except requests.HTTPError:
            try:
                raise PayoutValidationError(json.loads(response.content))
            except ValueError:
                raise PayoutCreationError(response.content)
        except MissingSchema:
            raise ImproperlyConfigured("Incorrect Payout URL")
        except IOError, e:
            raise PayoutCreationError(unicode(e))
        except TypeError:
            raise ImproperlyConfigured("Invalid Payout settings")

Set the payout status to created BEFORE we call out to dorado. This way we do not override that status that dorado set.
Set the payout status to created BEFORE we call out to dorado. This way
we do not override that status that dorado set.

BB-9471 #resolve
