class OrderSerializer(BaseOrderSerializer): def __init__(self, *args, **kwargs): super(OrderSerializer, self).__init__(*args, **kwargs) add_deposit_params = add_withdraw_params = {} if args and isinstance(self.instance, Order): order = self.instance add_deposit_params = { 'destination_tag': order.quote_destination_tag, 'payment_id': order.quote_payment_id } add_withdraw_params = { 'destination_tag': order.base_destination_tag, 'payment_id': order.base_payment_id } self.fields['deposit_address'] = NestedReadOnlyAddressSerializer( many=False, read_only=True, additional_params=add_deposit_params) self.fields['withdraw_address'] = NestedAddressSerializer( many=False, read_only=False, partial=True, additional_params=add_withdraw_params) referral_code = ReferralCodeSerializer(many=True, read_only=True, source='user.referral_code') pair = NestedPairSerializer(many=False, read_only=False) transactions = TransactionSerializer(many=True, read_only=True) class Meta(MetaFlatOrder): fields = MetaFlatOrder.fields
def test_retreive_transactions(self): req_tag = self.client.post('/api/v1/tag/', { "tag_name": "test tag", "company": self.company }) tag = Tag.objects.get(id=req_tag.data['id']) transaction = Transaction.objects.create( account=self.account, company=self.company, transaction_amount=500, category=self.category, ) transaction.tags.set([tag]) transaction.save() res = self.client.get(TRANSACTION_URL) transactions = Transaction.objects.all().order_by('-id') serializer = TransactionSerializer(transactions, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def transaction_views(request, id): try: transaction = Transaction.objects.get(id=id) except Transaction.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = TransactionSerializer(transaction) return JsonResponse(serializer.data)
def test_ok(self): data = { 'sender': self.user_1, 'amount': '5000.00', 'receiver': str(self.user_2.inn) } serializer_data = TransactionSerializer(data).data expected_data = { 'sender': 1, 'amount': '5000.00', 'receiver': str(self.user_2.inn) } self.assertEqual(expected_data, serializer_data)
def list(self, request, *args, **kwargs): id_comercio = request.GET.get('id_comercio') id_pago = request.GET.get('id_pago') if id_pago and id_comercio: transaction = Transaction.objects.get(id_pago=id_pago) transaction.status = "pending" transaction.save() TransactionStatus.objects.create(transaction=transaction, status=transaction.status, details="pago hecho") data = TransactionSerializer(transaction).data response = Response(data, status=status.HTTP_200_OK) return response else: response = Response({'error': "check parameters"}, status=status.HTTP_400_BAD_REQUEST) return response
class LimitOrderSerializer(BaseOrderSerializer): deposit_address = NestedReadOnlyAddressSerializer( many=False, read_only=True, ) withdraw_address = NestedSimpleAddressSerializer(many=False, read_only=False, partial=True) refund_address = NestedSimpleAddressSerializer(many=False, read_only=False, partial=True) referral_code = ReferralCodeSerializer(many=True, read_only=True, source='user.referral_code') pair = NestedPairSerializer(many=False, read_only=False) transactions = TransactionSerializer(many=True, read_only=True) class Meta(MetaFlatOrder): fields = MetaFlatOrder.fields
def transaction(request): try: transaction_list = Transaction.objects.filter(consumer_cpf=request.user.username) except Transaction.DoesNotExist: return HttpResponse(status=404) if request.method == 'POST': data = JSONParser().parse(request) serializer = TransactionSerializer(data=data) if serializer.is_valid(): serializer.create(validated_data=data) consumer = Consumer.objects.get(user_hash=data['hash']) consumer.credit -= data['value'] consumer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) elif request.method == 'GET': serializer = TransactionSerializer(transaction_list, many=True) return JsonResponse(serializer.data, safe=False)
def post(self, request): serializer = TransactionSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({'error': False}) return Response({'error': serializer.errors})