Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions poms/currencies/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ class Meta:
]


class CurrencyUserCodeOnlySerializer(serializers.ModelSerializer):
class Meta:
model = Currency
fields = ["user_code"]


class CurrencyHistorySerializer(ModelMetaSerializer, ModelWithTimeStampSerializer, ModelWithObjectStateSerializer):
currency = CurrencyField()
currency_object = CurrencyViewSerializer(source="currency", read_only=True)
Expand Down Expand Up @@ -250,6 +256,42 @@ def update(self, instance, validated_data):
return instance


class CurrencyHistoryLightSerializer(ModelMetaSerializer):
currency_object = serializers.SerializerMethodField()
pricing_policy_object = serializers.SerializerMethodField()
fx_rate = FloatEvalField()
procedure_modified_datetime = ReadOnlyField()

class Meta:
model = CurrencyHistory
fields = [
"id",
"currency_object",
"pricing_policy_object",
"date",
"fx_rate",
"procedure_modified_datetime",
"modified_at",
"is_temporary_fx_rate",
]

read_only_fields = fields

def get_currency_object(self, obj):
# there was a cyclical import, so I moved it here
from poms.currencies.serializers import CurrencyUserCodeOnlySerializer

serializer = CurrencyUserCodeOnlySerializer(obj.currency)
return serializer.data

def get_pricing_policy_object(self, obj):
# there was a cyclical import, so I moved it here
from poms.instruments.serializers import PricingPolicyUserCodeOnlySerializer

serializer = PricingPolicyUserCodeOnlySerializer(obj.pricing_policy)
return serializer.data


class CurrencyEvalSerializer(ModelWithUserCodeSerializer, ModelWithTimeStampSerializer):
master_user = MasterUserField()

Expand Down
20 changes: 20 additions & 0 deletions poms/currencies/tests/test_currency_history_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ def test__list_attributes(self):
response_json = response.json()
self.assertEqual(len(response_json["results"]), 5)

def test__list_light(self):
currency_history = self.create_currency_history()
response = self.client.get(path=f"{self.url}light/")
self.assertEqual(response.status_code, 200, response.content)

response_json = response.json()
self.assertEqual(response_json["count"], 1)
result = response_json["results"][0]

self.assertIsInstance(result["currency_object"], dict)
self.assertEqual(
result["currency_object"]["user_code"], currency_history.currency.user_code
)

self.assertIsInstance(result["pricing_policy_object"], dict)
self.assertEqual(
result["pricing_policy_object"]["user_code"],
currency_history.pricing_policy.user_code,
)

def test__get_filters(self): # sourcery skip: extract-duplicate-method
currency_history = self.create_currency_history()
response = self.client.get(path=f"{self.url}?currency={currency_history.currency.id}")
Expand Down
14 changes: 14 additions & 0 deletions poms/currencies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from poms.currencies.models import Currency, CurrencyHistory
from poms.currencies.serializers import (
CurrencyHistorySerializer,
CurrencyHistoryLightSerializer,
CurrencyLightSerializer,
CurrencySerializer,
)
Expand Down Expand Up @@ -230,6 +231,19 @@ class CurrencyHistoryViewSet(AbstractModelViewSet):
"pricing_policy__public_name",
]

@action(
detail=False,
methods=["get"],
url_path="light",
serializer_class=CurrencyHistoryLightSerializer,
)
def list_light(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginator.post_paginate_queryset(queryset, request)
serializer = self.get_serializer(page, many=True)

return self.get_paginated_response(serializer.data)

@action(detail=False, methods=["post"], url_path="bulk-create")
def bulk_create(self, request, *args, **kwargs):
valid_data = []
Expand Down
44 changes: 44 additions & 0 deletions poms/instruments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ class Meta:
fields = ["id", "user_code", "name", "short_name", "notes", "expr"]


class PricingPolicyUserCodeOnlySerializer(serializers.ModelSerializer):
class Meta:
model = PricingPolicy
fields = ["user_code"]


class InstrumentTypePricingPolicySerializer(serializers.ModelSerializer):
pricing_policy_id = serializers.IntegerField(read_only=False, required=True)
pricing_policy = PricingPolicyLightSerializer(read_only=True)
Expand Down Expand Up @@ -1304,6 +1310,12 @@ def save_accrual_events(self, instrument, created, accrual_events):
)


class InstrumentUserCodeOnlySerializer(serializers.ModelSerializer):
class Meta:
model = Instrument
fields = ["user_code"]


class InstrumentLightSerializer(ModelWithUserCodeSerializer):
master_user = MasterUserField()

Expand Down Expand Up @@ -1640,6 +1652,38 @@ def validate(self, attrs):
}


class PriceHistoryLightSerializer(ModelMetaSerializer):
instrument_object = InstrumentUserCodeOnlySerializer(source="instrument", read_only=True)
pricing_policy_object = PricingPolicyUserCodeOnlySerializer(
source="pricing_policy", read_only=True
)
principal_price = FloatEvalField()
accrued_price = AutocalculateFloatEvalField()
procedure_modified_datetime = ReadOnlyField()
ytm = ReadOnlyField()

class Meta:
model = PriceHistory
fields = [
"id",
"instrument_object",
"pricing_policy_object",
"date",
"principal_price",
"accrued_price",
"procedure_modified_datetime",
"nav",
"cash_flow",
"factor",
"long_delta",
"short_delta",
"is_temporary_price",
"ytm",
"modified_duration",
"error_message",
]


class PriceHistorySerializer(ModelMetaSerializer, ModelWithObjectStateSerializer):
instrument = InstrumentField()
instrument_object = InstrumentViewSerializer(source="instrument", read_only=True)
Expand Down
21 changes: 21 additions & 0 deletions poms/instruments/tests/views/test_price_history_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,27 @@ def test__list_attributes(self):
response_json = response.json()
self.assertEqual(len(response_json["results"]), 12)

def test__list_light(self):
pricing_history = self.create_pricing_history()
response = self.client.get(path=f"{self.url}light/")
self.assertEqual(response.status_code, 200, response.content)

response_json = response.json()
self.assertEqual(response_json["count"], 1)
result = response_json["results"][0]

self.assertIsInstance(result["instrument_object"], dict)
self.assertEqual(
result["instrument_object"]["user_code"],
pricing_history.instrument.user_code,
)

self.assertIsInstance(result["pricing_policy_object"], dict)
self.assertEqual(
result["pricing_policy_object"]["user_code"],
pricing_history.pricing_policy.user_code,
)

def test__get_filters(self): # sourcery skip: extract-duplicate-method
pricing_history = self.create_pricing_history()
response = self.client.get(path=f"{self.url}?instrument={pricing_history.instrument.id}")
Expand Down
14 changes: 14 additions & 0 deletions poms/instruments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
PricingPolicyLightSerializer,
PricingPolicySerializer,
ShortUnderlyingExposureSerializer,
PriceHistoryLightSerializer,
)
from poms.instruments.tasks import (
calculate_pricehistory,
Expand Down Expand Up @@ -1627,6 +1628,19 @@ def bulk_create(self, request, *args, **kwargs):
# return Response({'errors': errors}, status=status.HTTP_400_BAD_REQUEST)

return Response(status=status.HTTP_201_CREATED)

@action(
detail=False,
methods=["get"],
url_path="light",
serializer_class=PriceHistoryLightSerializer,
)
def list_light(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginator.post_paginate_queryset(queryset, request)
serializer = self.get_serializer(page, many=True)

return self.get_paginated_response(serializer.data)

@action(detail=False, methods=["get"], url_path="attributes")
def list_attributes(self, request, *args, **kwargs):
Expand Down
Loading