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")

@@ -699,32 +699,116 @@
 %7D%0A%0A        try:%0A
+            self.project.payout_status = 'created'%0A            self.project.save()%0A%0A
             resp
@@ -899,92 +899,8 @@
 us()
-%0A%0A            self.project.payout_status = 'created'%0A            self.project.save()
 %0A   

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