Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
country-api enhancement: api/v1/country
Browse files Browse the repository at this point in the history
- [x] Contract value USD added
- [x] Contract value local added
- [x] Tender count added
- [x] locale folder added
  • Loading branch information
sonikabaniya committed Apr 16, 2021
1 parent 02987ca commit 0c7157e
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion country/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db.models import Sum
from django.db.models import Count, Sum
from rest_framework import serializers
from rest_framework_serializer_extensions.serializers import SerializerExtensionsMixin

Expand All @@ -25,10 +25,14 @@ def to_internal_value(self, data):
class CountrySerializer(serializers.HyperlinkedModelSerializer, SerializerExtensionsMixin):
id = serializers.IntegerField(read_only=True)
continent = ChoiceField(choices=Country.CONTINENT_CHOICES)
amount_usd = serializers.SerializerMethodField()
amount_local = serializers.SerializerMethodField()
tender_count = serializers.SerializerMethodField()

class Meta:
model = Country
fields = "__all__"
extra_fields = ["continent", "id", "amount_usd", "amount_local", "tender_count"]
lookup_field = "slug"

extra_kwargs = {"url": {"lookup_field": "slug"}}
Expand All @@ -40,6 +44,38 @@ class Meta:
"slug",
)

def get_amount_usd(self, obj):
try:
if obj.amount_usd:
return obj.amount_usd
else:
None

except Exception:
return obj.tenders.all().aggregate(amount_usd=Sum("goods_services__contract_value_usd"))["amount_usd"]

def get_amount_local(self, obj):
try:
if obj.amount_local:
return obj.amount_local
else:
return None

except Exception:
return obj.tenders.all().aggregate(amount_local=Sum("goods_services__contract_value_local"))[
"amount_local"
]

def get_tender_count(self, obj):
try:
if obj.tender_count:
return obj.tender_count
else:
return None

except Exception:
return obj.tenders.all().aggregate(tender_count=Count("id"))["tender_count"]


class LanguageSerializer(serializers.ModelSerializer, SerializerExtensionsMixin):
class Meta:
Expand Down

0 comments on commit 0c7157e

Please sign in to comment.