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 #313 from open-contracting/CPA-312
Browse files Browse the repository at this point in the history
312-quantity-correlation-refactor
  • Loading branch information
KushalRaj committed May 14, 2021
2 parents c7a6f65 + 80a08ba commit cc7c366
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions visualization/views/quantity_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,18 @@ def get(self, request):
filter_args = {}
if country:
filter_args["country__country_code_alpha_2"] = country
if country:
contracts_quantity = (
Tender.objects.filter(country__country_code_alpha_2=country)
.annotate(month=TruncMonth("contract_date"))
.values("month", "country__currency")
.annotate(
count=Count("id"),
usd=Sum("goods_services__contract_value_usd"),
local=Sum("goods_services__contract_value_local"),
)
.order_by("-month")
)
else:
contracts_quantity = (
Tender.objects.annotate(month=TruncMonth("contract_date"))
.values("month")
.annotate(count=Count("id"), usd=Sum("goods_services__contract_value_usd"))
.order_by("-month")

contracts_quantity = (
Tender.objects.filter(**filter_args)
.annotate(month=TruncMonth("contract_date"))
.values("month", "country__currency")
.annotate(
count=Count("id"),
usd=Sum("contract_value_usd"),
local=Sum("contract_value_local"),
)
.order_by("-month")
)

contracts_quantity_list = []

Expand All @@ -54,17 +47,18 @@ def get(self, request):
active_case_count += j["active_cases_count"]
death_count += j["death_count"]
except Exception:
active_case_count = 0
death_count = 0
a = {
"active_cases": active_case_count,
"death_cases": death_count,
"amount_local": i["local"] if "local" in i else "",
"amount_usd": i["usd"],
"local_currency_code": i["country__currency"] if "country__currency" in i else "",
"month": i["month"],
"tender_count": i["count"],
}
contracts_quantity_list.append(a)
pass

contracts_quantity_list.append(
{
"active_cases": active_case_count,
"death_cases": death_count,
"amount_local": i["local"] if "local" in i else "",
"amount_usd": i["usd"],
"local_currency_code": i["country__currency"] if "country__currency" in i else "",
"month": i["month"],
"tender_count": i["count"],
}
)

return JsonResponse(contracts_quantity_list, safe=False)

0 comments on commit cc7c366

Please sign in to comment.