def post(self, request, *args, **kwargs): """ 创建用户钱包 """ form = WalletCreateForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data wallet = self.get_wallet_object(request) if not isinstance(wallet, Exception): serializer_res = WalletResponseSerializer(wallet) return Response(serializer_res.data, status=status.HTTP_400_BAD_REQUEST) serializer = WalletSerializer(data=cld, request=request) if not serializer.is_valid(): return Response({'Detail': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) try: serializer.save() except Exception as e: return Response({'Detail': e.args}, status=status.HTTP_400_BAD_REQUEST) serializer_res = WalletResponseSerializer(data=serializer.data) if not serializer_res.is_valid(): return Response({'Detail': serializer_res.errors}, status=status.HTTP_400_BAD_REQUEST) return Response(serializer_res.data, status=status.HTTP_201_CREATED)
def post(self, request): data = request.data user = request.user serializer = WalletSerializer(data=data) if serializer.is_valid(): serializer.save(user=user) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, pk): wallet = get_object_or_404(Wallet, pk=pk) data = request.data serializer = WalletSerializer(wallet, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): data = request.data user = request.user validated_data = wall_helper.validate_wallet_transaction_data( data, user) if not (validated_data.get('ret_status', status.HTTP_400_BAD_REQUEST) == status.HTTP_200_OK): return Response(status=validated_data.pop('ret_status'), data=validated_data) with transaction.atomic(): wallet = validated_data.pop('wallet') txn_data = { "type": data["txn_type"], "wallet": wallet, "amount": data["amount"] } try: txn = Transaction.objects.create(**txn_data) except Exception as e: logging.info(e) transaction.set_rollback(True) return Response(e.args[0], status=status.HTTP_400_BAD_REQUEST) wallet_serializer = WalletSerializer( instance=wallet, data={"balance": validated_data.get('balance')}, partial=True) if wallet_serializer.is_valid(): wallet_serializer.save() else: logging.info(wallet_serializer.errors) transaction.set_rollback(True) return Response(status=status.HTTP_400_BAD_REQUEST, data=wallet_serializer.errors) response = { "status": "PASS", "transaction_no": str(txn), "message": validated_data.get('message'), "balance": validated_data.get('balance') } return Response(status=status.HTTP_200_OK, data=response)
def post(self, request, *args, **kwargs): """ 展示用户钱包余额 """ _instance = self.get_wallet_info(request) serializer = WalletSerializer(_instance) return Response(serializer.data, status=status.HTTP_200_OK)
class FullUserSerializer(UserSerializer): wallet = WalletSerializer() class Meta: model = User fields = ('uuid', 'name', 'picture', 'location', 'username', 'last_login', 'wallet', 'email', 'phone')
def post(self, request, *args, **kwargs): """ 创建用户钱包 """ form = WalletCreateForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data serializer = WalletSerializer(data=cld, _request=request) if serializer.is_valid(): serializer.save() serializer_res = WalletResponseSerializer(serializer.data) if serializer_res.is_valid(): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'Detail': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
class TransactionsSerializer(serializers.ModelSerializer): wallet_linked = WalletSerializer() card_linked = CardSerializer() class Meta: model = Transactions fields = [ 'transaction_id', 'invoice_id', 'transaction_amount', 'transaction_date_time', 'transaction_state', 'payment_mode', 'bank_name', 'wallet_linked', 'card_linked', 'refund_status', 'refund_request_status', 'ticket_status' ]
def create_wallet_for_user(user): response = {'ret_status': status.HTTP_400_BAD_REQUEST, 'message': ''} if not user: response['message'] = 'No user found' return response wallet_creation_data = {'balance': 0, 'user': user.id} wallet_serializer = WalletSerializer(data=wallet_creation_data) if wallet_serializer.is_valid(): wallet_serializer.save() else: logging.info(wallet_serializer.errors) response['message'] = wallet_serializer.errors return response response['ret_status'] = status.HTTP_201_CREATED response['message'] = 'Walled Created' return response
class UserProfileSerializer(serializers.ModelSerializer): accounts_linked = AccountsSerializer(many=True) cards_linked = CardSerializer(many=True) transactions_linked = TransactionsSerializer(many=True) tickets_linked = TicketSerializer(many=True) wallets_linked = WalletSerializer(many=True) class Meta: model = UserProfile fields = ('first_name', 'last_name', 'email_address', 'phone_number', 'alternate_phone', 'kyc_verified', 'profile_pic', 'user_joined_date', 'accounts_linked', 'tickets_linked', 'transactions_linked', 'wallets_linked', 'cards_linked')
class TransactionsSerializer(serializers.ModelSerializer): wallet_linked = WalletSerializer() card_linked = CardSerializer() # for_account = AccountSerializer() class Meta: model = Transactions fields = [ 'transaction_amount', 'transaction_date_time', 'transaction_state', # 'for_account', 'wallet_linked', 'card_linked' ]
def get(self, request): wallets = Wallet.objects.all() serializer = WalletSerializer(wallets, many=True) return Response(serializer.data, status=status.HTTP_200_OK)