def update(self, instance, validated_data): refund_address = validated_data.pop('refund_address') addr_list = Address.objects.filter(address=refund_address['address']) if not addr_list: address = Address(**refund_address) address.type = Address.WITHDRAW address.currency = instance.pair.quote address.save() else: address = addr_list[0] instance.refund_address = address instance.save() return instance
def create(self, validated_data): for field in READABLE_FIELDS: validated_data.pop(field, None) withdraw_address = validated_data.pop('withdraw_address') refund_address = validated_data.pop('refund_address') validated_data.pop('pair') withdraw_addr_list = Address.objects.filter( address=withdraw_address['address']) order = LimitOrder(pair=self.pair, order_book=self.order_book, **validated_data) if not withdraw_addr_list: w_address = Address(**withdraw_address) w_address.type = Address.WITHDRAW w_address.currency = order.withdraw_currency w_address.save() else: w_address = withdraw_addr_list[0] order.withdraw_address = w_address refund_addr_list = Address.objects.filter( address=refund_address['address']) if not refund_addr_list: r_address = Address(**refund_address) r_address.type = Address.REFUND r_address.currency = order.refund_currency r_address.save() else: r_address = refund_addr_list[0] order.refund_address = r_address try: order.save() # get post_save stuff in sync order.refresh_from_db() return order except ValidationError as e: raise RestValidationError({'non_field_errors': [e.message]})
def create_withdraw_address(request, order_pk): error_message = 'Error creating address: %s' order = Order.objects.get(pk=order_pk) if not order.user.profile.is_verified and not order.exchange: pm = order.payment_preference.payment_method if pm.required_verification_buy or order.user.profile.anonymous_login: resp = { 'status': 'ERR', 'msg': 'You need to be a verified user to set withdrawal ' 'address for order with payment method \'{}\'' ''.format(pm.name) } return JsonResponse(resp, safe=False) address = request.POST.get('value') addr = Address() addr.type = Address.WITHDRAW addr.user = request.user addr.address = address if order.order_type == Order.BUY: currency = order.pair.base else: currency = order.pair.quote addr.currency = currency try: if currency.code == 'BTC': validate_btc(addr.address) elif currency.code == 'LTC': validate_ltc(addr.address) elif currency.code == 'ETH': validate_eth(addr.address) else: validate_address(addr.address) addr.save() resp = {'status': 'OK', 'pk': addr.pk, 'target': addr.currency.code} except ValidationError: resp = {'status': 'ERR', 'msg': 'The supplied address is invalid.'} except Exception as e: msg = error_message % (e) resp = {'status': 'ERR', 'msg': msg} return JsonResponse(resp, safe=False)
def create(self, validated_data): for field in READABLE_FIELDS: validated_data.pop(field, None) withdraw_address = validated_data.pop('withdraw_address') payment_id = withdraw_address.pop('payment_id', None) destination_tag = withdraw_address.pop('destination_tag', None) validated_data.pop('pair') # Just making sure addr_list = Address.objects.filter(address=withdraw_address['address']) order = Order(pair=self.pair, **validated_data) if payment_id: order.payment_id = payment_id if destination_tag: order.destination_tag = destination_tag if not addr_list: address = Address(**withdraw_address) address.type = Address.WITHDRAW address.currency = order.pair.base address.save() else: address = addr_list[0] order.withdraw_address = address try: order.save() # get post_save stuff in sync order.refresh_from_db() self.fields['deposit_address'] = NestedReadOnlyAddressSerializer( many=False, read_only=True, additional_params={ 'destination_tag': order.quote_destination_tag, 'payment_id': order.quote_payment_id }) self.fields['withdraw_address'] = NestedAddressSerializer( many=False, read_only=False, additional_params={ 'destination_tag': order.base_destination_tag, 'payment_id': order.base_payment_id }) return order except ValidationError as e: raise RestValidationError({'non_field_errors': [e.message]})