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

Commit

Permalink
total-spending-api-refactor
Browse files Browse the repository at this point in the history
 - [x] remove unused loc
 - [x] query reduced
 - [x] spelling changed from spendings to spending
  • Loading branch information
sonikabaniya committed May 7, 2021
1 parent c082d5c commit 7fe1a54
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 115 deletions.
4 changes: 2 additions & 2 deletions visualization/tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
TopBuyers,
TopSuppliers,
TotalContractsView,
TotalSpendingsView,
TotalSpendingView,
WorldMapView,
)

Expand All @@ -51,7 +51,7 @@ def test_total_contracts_url_is_resolved(self):

def test_total_spending_url_is_resolved(self):
url = reverse("total_spending")
self.assertEquals(resolve(url).func.view_class, TotalSpendingsView)
self.assertEquals(resolve(url).func.view_class, TotalSpendingView)

def test_average_bids_url_is_resolved(self):
url = reverse("average_bids")
Expand Down
2 changes: 1 addition & 1 deletion visualization/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

urlpatterns = [
path("total-contracts/", views.TotalContractsView.as_view(), name="total_contracts"),
path("total-spending/", views.TotalSpendingsView.as_view(), name="total_spending"),
path("total-spending/", views.TotalSpendingView.as_view(), name="total_spending"),
path("average-bids/", views.AverageBidsView.as_view(), name="average_bids"),
path("world-map-race/", views.GlobalOverView.as_view(), name="world_map_race"),
path("top-suppliers/", views.TopSuppliers.as_view(), name="top_suppliers"),
Expand Down
2 changes: 1 addition & 1 deletion visualization/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
from .top_buyers import TopBuyers
from .top_suppliers import TopSuppliers
from .total_contracts_view import TotalContractsView
from .total_spendings_view import TotalSpendingsView
from .total_spending_view import TotalSpendingView
from .upcoming_event_view import UpcomingEventView
from .world_map_view import WorldMapView
76 changes: 76 additions & 0 deletions visualization/views/total_spending_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from django.db.models import Sum
from django.db.models.functions import TruncMonth
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework.views import APIView

from country.models import Tender
from visualization.helpers.general import page_expire_period
from visualization.views.lib.general import add_filter_args


class TotalSpendingView(APIView):
@method_decorator(cache_page(page_expire_period()))
def get(self, request):
"""
Return a list of all contracts.
"""

# Calculating total tender
country = self.request.GET.get("country", None)
buyer = self.request.GET.get("buyer")
supplier = self.request.GET.get("supplier")

filter_args = {}
exclude_args = {"status": "canceled"}
if country:
filter_args["country__country_code_alpha_2"] = country
if buyer:
filter_args = add_filter_args("buyer", buyer, filter_args)
if supplier:
filter_args = add_filter_args("supplier", supplier, filter_args)

bar_chart = (
Tender.objects.filter(**filter_args)
.exclude(**exclude_args)
.values("procurement_procedure")
.annotate(usd=Sum("contract_value_usd"), local=Sum("contract_value_local"))
)

total_country_usd_sum = 0
total_country_local_sum = 0
bar_chart_usd = []
bar_chart_local = []

for i in bar_chart:
total_country_usd_sum += i["usd"] if i["usd"] else 0
total_country_local_sum += i["local"] if i["local"] else 0
bar_chart_usd.append({"method": i["procurement_procedure"], "value": i["usd"]})

bar_chart_local.append({"method": i["procurement_procedure"], "value": i["local"]})

line_chart = (
Tender.objects.filter(**filter_args)
.exclude(**exclude_args)
.annotate(month=TruncMonth("contract_date"))
.values("month")
.annotate(usd=Sum("contract_value_usd"), local=Sum("contract_value_local"))
.order_by("-month")
)
line_chart_local_list = [{"date": i["month"], "value": i["local"]} for i in line_chart]
line_chart_list = [{"date": i["month"], "value": i["usd"]} for i in line_chart]

result = {
"usd": {
"total": total_country_usd_sum,
"line_chart": line_chart_list,
"bar_chart": bar_chart_usd,
},
"local": {
"total": total_country_local_sum,
"line_chart": line_chart_local_list,
"bar_chart": bar_chart_local,
},
}
return JsonResponse(result)
111 changes: 0 additions & 111 deletions visualization/views/total_spendings_view.py

This file was deleted.

0 comments on commit 7fe1a54

Please sign in to comment.