mirror of
				https://github.com/HackSoftware/Django-Styleguide.git
				synced 2025-11-04 01:17:29 +03:00 
			
		
		
		
	Add transaction.atomic to an example
This commit is contained in:
		
							parent
							
								
									2720e607c6
								
							
						
					
					
						commit
						8a6e4e9635
					
				
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
									
									
									
									
								
							| 
						 | 
					@ -799,12 +799,14 @@ When creating the required state for a given test, one can use a combination of:
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
from django.contrib.auth.models import User
 | 
					from django.contrib.auth.models import User
 | 
				
			||||||
from django.core.exceptions import ValidationError
 | 
					from django.core.exceptions import ValidationError
 | 
				
			||||||
 | 
					from django.db import transaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from project.payments.selectors import items_get_for_user
 | 
					from project.payments.selectors import items_get_for_user
 | 
				
			||||||
from project.payments.models import Item, Payment
 | 
					from project.payments.models import Item, Payment
 | 
				
			||||||
from project.payments.tasks import payment_charge
 | 
					from project.payments.tasks import payment_charge
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@transaction.atomic
 | 
				
			||||||
def item_buy(
 | 
					def item_buy(
 | 
				
			||||||
    *,
 | 
					    *,
 | 
				
			||||||
    item: Item,
 | 
					    item: Item,
 | 
				
			||||||
| 
						 | 
					@ -813,13 +815,19 @@ def item_buy(
 | 
				
			||||||
    if item in items_get_for_user(user=user):
 | 
					    if item in items_get_for_user(user=user):
 | 
				
			||||||
        raise ValidationError(f'Item {item} already in {user} items.')
 | 
					        raise ValidationError(f'Item {item} already in {user} items.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    payment = Payment.objects.create(
 | 
					    payment = Payment(
 | 
				
			||||||
        item=item,
 | 
					        item=item,
 | 
				
			||||||
        user=user,
 | 
					        user=user,
 | 
				
			||||||
        successful=False
 | 
					        successful=False
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					    payment.full_clean()
 | 
				
			||||||
 | 
					    payment.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    payment_charge.delay(payment_id=payment.id)
 | 
					    # NOTE: We have to make sure that the objects is created
 | 
				
			||||||
 | 
					    # in the DB in order to use in the Celery task
 | 
				
			||||||
 | 
					    transaction.on_commit(
 | 
				
			||||||
 | 
					        lambda: payment_charge.delay(payment_id=payment.id)
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return payment
 | 
					    return payment
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user