Created
October 30, 2019 11:21
-
-
Save 63phc/c8d2c70895604bb6465ba5d95efd45aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogEntryDetailsContractActivationSerialiser(serializers.ModelSerializer): | |
number = serializers.CharField(source='number.number') | |
name = serializers.CharField(source='contract.name') | |
class Meta: | |
model = UserContract | |
fields = ( | |
'number', | |
'name' | |
) | |
class LogEntryContractActivationSerializer(serializers.ModelSerializer): | |
user = serializers.CharField(source='user.username') | |
details = serializers.SerializerMethodField() | |
operation_type = serializers.SerializerMethodField() | |
account = serializers.SerializerMethodField() | |
amount = serializers.SerializerMethodField() | |
balance = serializers.CharField(source='user.euro.balance.amount') | |
class Meta: | |
model = UserContract | |
fields = ( | |
'user', | |
'operation_type', | |
'account', | |
'amount', | |
'balance', | |
'details', | |
'created_at', | |
) | |
def get_details(self, obj): | |
return LogEntryDetailsContractActivationSerialiser(obj).data | |
def get_operation_type(self, obj): | |
return LogEntry.CONTRACT_ACTIVATION | |
def get_account(self, obj): | |
return LogEntry.EURO | |
def get_amount(self, obj): | |
return (-1) * obj.activation_amount.amount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update(self, request, *args, **kwargs): | |
user = request.user | |
serializer = self.get_serializer(data=request.data) | |
serializer.is_valid(raise_exception=True) | |
with cache.lock('buy_contract_{}'.format(user.id)): | |
contract = Contract.objects.get(pk=serializer.data['contract_type_id']) | |
if user.euro.balance < contract.activation_amount: | |
raise ValidationError({'detail': _('insufficient funds')}) | |
if UserContract.objects.filter(user=user, contract=contract, finished_at=None).exists(): | |
raise ValidationError({'detail': _('you already have {} contract').format(contract.name)}) | |
user.save() | |
user_contract = UserContract.objects.create( | |
user=user, | |
contract=contract, | |
activation_amount=contract.activation_amount, | |
balance=contract.activation_amount | |
) | |
Transaction.objects.create( | |
source=user.euro, | |
target=SystemCashAccount.objects.get(slug='contract'), | |
amount=contract.activation_amount, | |
tx_type=TransactionType.objects.get(slug='contract_activation'), | |
comment="UserContract#{}".format(user_contract.pk) | |
) | |
user.euro.refresh_from_db() | |
serializer = LogEntryContractActivationSerializer(user_contract) | |
LogEntry.objects.create(serializer.data) | |
# LogEntry.objects.create( | |
# user=user, | |
# operation_type=LogEntry.CONTRACT_ACTIVATION, | |
# account=LogEntry.EURO, | |
# amount=(-1) * contract.activation_amount.amount, | |
# balance=user.euro.balance.amount, | |
# details={ | |
# 'number': user_contract.number.number, | |
# 'name': user_contract.contract.name | |
# } | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment