Skip to content

Commit f129769

Browse files
committed
api: org: Add method to provide backward compatibility with API v1
This provides API v1 compatibility with the new aggregate data format for currencies on entities.
1 parent 69f2120 commit f129769

1 file changed

Lines changed: 62 additions & 2 deletions

File tree

datastore/api/org/serializers.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def get_self(self, org):
2727

2828

2929
class OrganisationAggregateCurrencySerializer(serializers.Serializer):
30-
avg = serializers.FloatField()
30+
# NOTE: avg to be deprecated in version 2
31+
# https://github.com/ThreeSixtyGiving/datastore/issues/292
32+
avg = serializers.floatfield()
3133
max = serializers.FloatField()
3234
min = serializers.FloatField()
3335
total = serializers.FloatField()
@@ -36,8 +38,66 @@ class OrganisationAggregateCurrencySerializer(serializers.Serializer):
3638

3739
class OrganisationAggregateSerializer(serializers.Serializer):
3840
grants = serializers.IntegerField()
39-
currencies = serializers.DictField(child=OrganisationAggregateCurrencySerializer())
4041

42+
currencies = serializers.SerializerMethodField(serializer_class=OrganisationAggregateCurrencySerializer)
43+
44+
# NOTE: This field will be deprecated in future version in favour of
45+
# the direct representation from the model. This method is to provide API v1
46+
# compatibility.
47+
def get_currencies(self, org):
48+
def combine_currencies_org_ind(currency):
49+
""" recombine currency stats from both recipient_org and recipient_ind"""
50+
combined_grants = recipient_org[currency]["grants"] + recipient_ind[currency]["grants"]
51+
combined_total = recipient_org[currency]["total"] + recipient_ind[currency]["total"]
52+
53+
# Calculate the new average
54+
if combined_grants > 0:
55+
combined_avg = combined_total / combined_grants
56+
else:
57+
combined_avg = 0.0
58+
59+
# Determine the new min and max using direct key access
60+
combined_min = min(recipient_org[currency]["min"], recipient_ind[currency]["min"])
61+
combined_max = max(recipient_org[currency]["max"], recipient_ind[currency]["max"])
62+
63+
return {
64+
"min": combined_min,
65+
"max": combined_max,
66+
"avg": combined_avg,
67+
"grants": combined_grants,
68+
"total": combined_total,
69+
}
70+
71+
72+
recipient_org = org["currencies"].get("recipient_org")
73+
recipient_ind = org["currencies"].get("recipient_ind")
74+
75+
if recipient_org and not recipient_ind:
76+
return recipient_org
77+
78+
if recipient_ind and not recipient_org:
79+
return recipient_ind
80+
81+
if recipient_org and recipient_ind:
82+
ret = {}
83+
84+
set_recipient_org_currencies = set(recipient_org.keys())
85+
set_recipient_ind_currencies = set(recipient_ind.keys())
86+
87+
common_currencies = list(set_recipient_org_currencies.intersection(set_recipient_ind_currencies))
88+
currencies_only_in_recipient_org_currencies = list(set_recipient_org_currencies.difference(set_recipient_ind_currencies))
89+
currencies_only_in_recipient_ind_currencies = list(set_recipient_ind_currencies.difference(set_recipient_org_currencies))
90+
91+
for currency in common_currencies:
92+
ret[currency] =combine_currencies_org_ind(currency)
93+
94+
for currency in currencies_only_in_recipient_org_currencies:
95+
ret[currency] = recipient_org[currency]
96+
97+
for currency in currencies_only_in_recipient_ind_currencies:
98+
ret[currency] = recipient_org[currency]
99+
100+
return ret
41101

42102
class OrganisationFunderSerializer(serializers.ModelSerializer):
43103
class Meta:

0 commit comments

Comments
 (0)