Skip to content
Browse files

Create trans if it doesn't exist in bill config

  • Loading branch information...
1 parent 591f974 commit aa0ea76b9f68cda68bab8081da1cfc0159571fb9 @kumar303 kumar303 committed Jan 4, 2013
Showing with 42 additions and 8 deletions.
  1. +6 −3 lib/bango/tests/test_resources.py
  2. +5 −4 lib/transactions/models.py
  3. +19 −1 solitude/base.py
  4. +12 −0 solitude/tests/test.py
View
9 lib/bango/tests/test_resources.py
@@ -297,12 +297,15 @@ def test_good(self):
eq_(res.status_code, 201, res.content)
assert 'billingConfigurationId' in json.loads(res.content)
- def test_not_found(self):
+ def test_create_trans_if_not_existing(self):
data = self.good()
+ data['transaction_uuid'] = '<some-new-trans-uuid>'
self.transaction.provider = constants.SOURCE_PAYPAL
self.transaction.save()
- with self.assertRaises(Transaction.DoesNotExist):
- self.client.post(self.list_url, data=data)
+ res = self.client.post(self.list_url, data=data)
+ data = json.loads(res.content)
+ tr = Transaction.objects.get(uid_pay=data['billingConfigurationId'])
+ assert tr is not self.transaction
def test_changed(self):
res = self.client.post(self.list_url, data=self.good())
View
9 lib/transactions/models.py
@@ -112,13 +112,14 @@ def create_bango_transaction(sender, **kwargs):
form = kwargs['form']
seller_product = form.cleaned_data['seller_product_bango'].seller_product
- transaction = Transaction.objects.get(uuid=data['transaction_uuid'],
- status=constants.STATUS_RECEIVED,
- provider=constants.SOURCE_BANGO)
+ transaction, c = Transaction.objects.safer_get_or_create(
+ uuid=data['transaction_uuid'],
+ status=constants.STATUS_RECEIVED,
+ provider=constants.SOURCE_BANGO,
+ seller_product=seller_product)
transaction.source = data.get('source', '')
transaction.uid_support = data['externalTransactionId']
transaction.uid_pay = bundle['billingConfigurationId']
- transaction.seller_product = seller_product
transaction.status = constants.STATUS_PENDING
transaction.type = constants.TYPE_PAYMENT
transaction.save()
View
20 solitude/base.py
@@ -8,7 +8,7 @@
from django.conf import settings
from django.core.cache import cache
from django.core.urlresolvers import reverse, resolve
-from django.db import models
+from django.db import models, transaction
from django.test.client import Client
from django.views import debug
@@ -396,9 +396,27 @@ def delete(self):
cache.delete(self.prefixed)
+class ManagerBase(models.Manager):
+
+ def safer_get_or_create(self, defaults=None, **kw):
+ """
+ This is subjective, but I don't trust get_or_create until #13906
+ gets fixed. It's probably fine, but this makes me happy for the moment
+ and solved a get_or_create we've had in the past.
+ """
+ with transaction.commit_on_success():
+ try:
+ return self.get(**kw), False
+ except self.model.DoesNotExist:
+ if defaults is not None:
+ kw.update(defaults)
+ return self.create(**kw), True
+
+
class Model(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
+ objects = ManagerBase()
class Meta:
abstract = True
View
12 solitude/tests/test.py
@@ -11,6 +11,7 @@
import test_utils
from lib.paypal.errors import PaypalError
+from lib.sellers.models import Seller
from lib.sellers.resources import SellerResource
from solitude.base import APITest, JWTDecodeError, JWTSerializer, Resource
from solitude.fields import URLField
@@ -184,3 +185,14 @@ def test_empty(self):
self.field = URLField(to='lib.sellers.resources.SellerResource',
required=False)
eq_(self.field.clean(''), None)
+
+
+class TestModel(test_utils.TestCase):
+
+ def test_safer_get_or_create(self):
+ data = dict(uuid='some-unique-value')
+ a, c = Seller.objects.safer_get_or_create(**data)
+ assert c
+ b, c = Seller.objects.safer_get_or_create(**data)
+ assert not c
+ eq_(a, b)

0 comments on commit aa0ea76

Please sign in to comment.
Something went wrong with that request. Please try again.