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

Commit

Permalink
Merge pull request #244 from open-contracting/160-open-direct-api-fixes
Browse files Browse the repository at this point in the history
API fixes
  • Loading branch information
KushalRaj committed Apr 13, 2021
2 parents a9c520f + 39b4455 commit ad722c1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions country/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ def get_buyer_code(self, obj):
def get_goods_services(self, obj):
a = obj.goods_services.all().values(
"goods_services_category__category_name",
"contract_desc",
"contract_value_usd",
"quantity_units",
"ppu_including_vat",
Expand Down
78 changes: 62 additions & 16 deletions vizualization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def get(self, request):
Tender.objects.filter(country__country_code_alpha_2=country, procurement_procedure="direct")
.values("country__currency")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -473,7 +473,17 @@ def get(self, request):
Tender.objects.filter(country__country_code_alpha_2=country, procurement_procedure="open")
.values("country__currency")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
)

amount_not_identified = (
Tender.objects.filter(country__country_code_alpha_2=country, procurement_procedure="not_identified")
.values("country__currency")
.annotate(
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -483,7 +493,7 @@ def get(self, request):
Tender.objects.filter(**filter_args, procurement_procedure="direct")
.values("procurement_procedure")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -492,7 +502,16 @@ def get(self, request):
Tender.objects.filter(**filter_args, procurement_procedure="open")
.values("procurement_procedure")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
)
amount_not_identified = (
Tender.objects.filter(**filter_args, procurement_procedure="not_identified")
.values("procurement_procedure")
.annotate(
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -502,7 +521,7 @@ def get(self, request):
Tender.objects.filter(procurement_procedure="direct")
.values("procurement_procedure")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -511,7 +530,16 @@ def get(self, request):
Tender.objects.filter(procurement_procedure="open")
.values("procurement_procedure")
.annotate(
count=Count("id"),
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
)
amount_not_identified = (
Tender.objects.filter(procurement_procedure="not_identified")
.values("procurement_procedure")
.annotate(
count=Count("contract_id", distinct=True),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
Expand All @@ -528,7 +556,7 @@ def get(self, request):
"amount_local": i["local"] if i["local"] else 0,
"amount_usd": i["usd"] if i["usd"] else 0,
"tender_count": i["count"],
"local_currency_code": i["country__currency"] if "country__currency" in i else [],
"local_currency_code": i["country__currency"] if "country__currency" in i else "",
"procedure": "direct",
}
else:
Expand All @@ -546,7 +574,7 @@ def get(self, request):
"amount_local": i["local"] if i["local"] else 0,
"amount_usd": i["usd"] if i["usd"] else 0,
"tender_count": i["count"],
"local_currency_code": i["country__currency"] if "country__currency" in i else [],
"local_currency_code": i["country__currency"] if "country__currency" in i else "",
"procedure": "open",
}
else:
Expand All @@ -558,7 +586,25 @@ def get(self, request):
"procedure": "open",
}

result = [result_direct, result_open]
if amount_not_identified:
for i in amount_not_identified:
result_not_identified = {
"amount_local": i["local"] if i["local"] else 0,
"amount_usd": i["usd"] if i["usd"] else 0,
"tender_count": i["count"],
"local_currency_code": i["country__currency"] if "country__currency" in i else "",
"procedure": "not_identified",
}
else:
result_not_identified = {
"amount_local": 0,
"amount_usd": 0,
"tender_count": 0,
"local_currency_code": country_currency,
"procedure": "not_identified",
}

result = [result_direct, result_open, result_not_identified]

return JsonResponse(result, safe=False)

Expand Down Expand Up @@ -599,7 +645,7 @@ def get(self, request):

if country:
try:
country_res = Country.objects.get(name=country)
country_res = Country.objects.get(country_code_alpha_2=country)
currency_code = country_res.currency
except Exception as e:
print(e)
Expand Down Expand Up @@ -2035,7 +2081,7 @@ def get(self, request):
Tender.objects.filter(**filter_args)
.annotate(month=TruncMonth("contract_date"))
.values(
"country__country_code",
"country__country_code_alpha_2",
"country__currency",
"month",
"goods_services__goods_services_category__id",
Expand All @@ -2050,7 +2096,7 @@ def get(self, request):
Tender.objects.filter(**filter_args)
.annotate(month=TruncMonth("contract_date"))
.values(
"country__country_code",
"country__country_code_alpha_2",
"country__currency",
"month",
"goods_services__goods_services_category__id",
Expand All @@ -2063,7 +2109,7 @@ def get(self, request):
{
"amount_local": i["local"],
"amount_usd": i["usd"],
"country_code": i["country__country_code"],
"country_code": i["country__country_code_alpha_2"],
"currency": i["country__currency"],
"month": i["month"].strftime("%Y-%m"),
"product_id": i["goods_services__goods_services_category__id"],
Expand All @@ -2078,7 +2124,7 @@ def get(self, request):
amount_usd_local = (
Tender.objects.annotate(month=TruncMonth("contract_date"))
.values(
"country__country_code",
"country__country_code_alpha_2",
"country__currency",
"month",
"goods_services__goods_services_category__id",
Expand All @@ -2092,7 +2138,7 @@ def get(self, request):
count = (
Tender.objects.annotate(month=TruncMonth("contract_date"))
.values(
"country__country_code",
"country__country_code_alpha_2",
"country__currency",
"month",
"goods_services__goods_services_category__id",
Expand All @@ -2105,7 +2151,7 @@ def get(self, request):
{
"amount_local": i["local"],
"amount_usd": i["usd"],
"country_code": i["country__country_code"],
"country_code": i["country__country_code_alpha_2"],
"currency": i["country__currency"],
"month": i["month"].strftime("%Y-%m"),
"product_id": i["goods_services__goods_services_category__id"],
Expand Down

0 comments on commit ad722c1

Please sign in to comment.