Ejemplo n.º 1
0
def asset_list(request):
    """
    List all assets, or create a new one
    
    :param request: HTTP request object
    :return: JSON response
    """
    if request.method == 'GET':
        assets = Asset.objects.all()
        serializer = AssetSerializer(assets, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        # Check that admin user is creating objects
        user = request.META.get('HTTP_X_USER', 'anon')
        if user != 'admin':
            print([user for user in sorted(request.META.keys())])
            return Response('Only admin user can create assets',
                            status=status.HTTP_401_UNAUTHORIZED)

        data = JSONParser().parse(request)
        serializer = AssetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    else:
        return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 2
0
def AssetList(request):
    if request.method == 'GET':
        asset_list = models.Asset.objects.all()
        serializer = AssetSerializer(asset_list,many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = AssetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
    def test_create_asset(self):
        """Asset data is saved correctly."""
        asset_uuid = uuid.uuid4()
        data = {
            "id": str(asset_uuid),
            "name": "Asset Name",
            "asset_class": self.asset_class.name,
            "easy_to_borrow": True,
            "exchange": self.exchange.name,
            "marginable": True,
            "shortable": True,
            "status": Asset.ACTIVE,
            "symbol": "SYMBOL",
            "tradable": True,
        }

        serializer = AssetSerializer(data=data,
                                     context={"request": self.request})
        self.assertTrue(serializer.is_valid())
        asset = serializer.save()

        self.assertEqual(Asset.objects.count(), 1)
        self.assertEqual(str(asset.id), data["id"])
        self.assertEqual(asset.asset_class.name, data["asset_class"])
        self.assertTrue(asset.easy_to_borrow)
        self.assertEqual(asset.exchange.name, data["exchange"])
        self.assertTrue(asset.marginable)
        self.assertTrue(asset.shortable)
        self.assertEqual(asset.status, data["status"])
        self.assertEqual(asset.symbol, data["symbol"])
        self.assertTrue(asset.tradable)
Ejemplo n.º 4
0
    def test_update_asset(self):
        """Asset data is updated correctly."""
        asset = AssetFactory(name="Old Asset Name",
                             asset_class=self.asset_class,
                             exchange=self.exchange)
        serializer = AssetSerializer(
            asset,
            data={"name": "New Asset Name"},
            partial=True,
            context={"request": self.request},
        )

        self.assertTrue(serializer.is_valid())
        serializer.save()

        asset.refresh_from_db()
        self.assertEqual(asset.name, "New Asset Name")
Ejemplo n.º 5
0
def AssetList(request):
    '''
    django调用restframework的serializer和response定制自己的视图方法
    '''
    if request.method == 'GET':
        asset_list = models.Asset.objects.all()
        #many表示传入的是一个结果集,得到的结果是一个可序列化的OrderedDict
        serializer = AssetSerializer(asset_list, many=True)
        print(serializer.data)
        #使用restframework封装的Response
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = AssetSerializer(data=request.POST)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)