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
  • Loading branch information
sonikabaniya committed Apr 16, 2021
1 parent 67137e4 commit bff5db6
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions country/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -42,23 +46,35 @@ class Meta:

def get_amount_usd(self, obj):
try:
total_country_tenders = obj.tenders.filter(country_id=self.id).annotate(
amount_usd=Sum("contract_value_usd"),
amount_local=Sum("contract_value_local"),
tender_count=Count("id"),
)
if obj.amount_usd:
return obj.amount_usd
else:
None

tender_data = {
"amount_usd": total_country_tenders.amount_usd,
"amount_local": total_country_tenders.amount_local,
"tender_count": total_country_tenders.tender_count,
"latest_contract_Date": "0000-00-00",
}
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:
tender_data = {"amount_usd": 0, "amount_local": 0, "tender_count": 0, "latest_contract_Date": "0000-00-00"}
return obj.tenders.all().aggregate(amount_local=Sum("goods_services__contract_value_local"))[
"amount_local"
]

return tender_data
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):
Expand Down

0 comments on commit bff5db6

Please sign in to comment.