diff --git a/country/management/commands/evaluate_contract_red_flag.py b/country/management/commands/evaluate_contract_red_flag.py new file mode 100644 index 0000000..7ec5164 --- /dev/null +++ b/country/management/commands/evaluate_contract_red_flag.py @@ -0,0 +1,40 @@ +from django.core.management.base import BaseCommand + +from country.models import Country, Tender +from country.tasks import clear_redflag, process_redflag, process_redflag6, process_redflag7 + + +class Command(BaseCommand): + help = "Compute red flag for specific country" + + def add_arguments(self, parser): + parser.add_argument("country", nargs="+", type=str) + + def handle(self, *args, **options): + country_code = options["country"][0] + country_code = str(country_code).upper() + country_list = Country.objects.exclude(country_code_alpha_2="gl").values_list( + "country_code_alpha_2", flat=True + ) + + if country_code not in country_list: + self.stderr.write("Country code is invalid.") + return + + country = Country.objects.get(country_code_alpha_2=country_code) + + self.stdout.write(f"Processing Red flag for {country.name}") + + country_tenders = Tender.objects.filter( + country_id=country.id, supplier__isnull=False, buyer__isnull=False + ).values("id", "buyer__buyer_name", "supplier__supplier_name", "supplier__supplier_address") + + for tender in country_tenders: + tender_id = tender["id"] + self.stdout.write("Created task for id :" + str(tender_id)) + clear_redflag.apply_async(args=(tender_id,), queue="covid19") + process_redflag6.apply_async(args=(tender_id,), queue="covid19") + process_redflag7.apply_async(args=(tender_id,), queue="covid19") + process_redflag.apply_async(args=(tender_id,), queue="covid19") + + self.stdout.write("Done") diff --git a/country/management/commands/import_red_flag.py b/country/management/commands/import_red_flag.py deleted file mode 100644 index 78bdc77..0000000 --- a/country/management/commands/import_red_flag.py +++ /dev/null @@ -1,30 +0,0 @@ -from django.core.management.base import BaseCommand - -from country.models import Tender -from country.tasks import clear_redflag, process_redflag, process_redflag6, process_redflag7 - - -class Command(BaseCommand): - def handle(self, *args, **kwargs): - self.stdout.write("Removing all the relations of Red flag") - tenders = Tender.objects.all() - for tender in tenders: - id = tender.id - clear_redflag.apply_async(args=(id,), queue="covid19") - - self.stdout.write("All cleared") - self.stdout.write("Processing Red Flag in tender") - - tender_instance = Tender.objects.filter(supplier__isnull=False, buyer__isnull=False).values( - "id", "buyer__buyer_name", "supplier__supplier_name", "supplier__supplier_address" - ) - for tender in tender_instance: - id = tender["id"] - process_redflag7.apply_async(args=(id, tender), queue="covid19") - process_redflag6.apply_async(args=(id, tender), queue="covid19") - - for tender in tenders: - id = tender.id - process_redflag.apply_async(args=(id,), queue="covid19") - self.stdout.write("Created task for id :" + str(id)) - self.stdout.write("Done") diff --git a/country/tests/commands/test_fetch_covid_active_cases.py b/country/tests/commands/test_fetch_covid_active_cases.py index 282b6d0..7ffaffa 100644 --- a/country/tests/commands/test_fetch_covid_active_cases.py +++ b/country/tests/commands/test_fetch_covid_active_cases.py @@ -20,6 +20,7 @@ def assertCommand(self, *args, expected_out="", expected_err="", **kwargs): self.assertEqual(out.getvalue(), dedent(expected_out)) def test_command(self): + Country.objects.all().delete() Country.objects.create(name="Global", country_code="GLO", country_code_alpha_2="gl", currency="USD") Country.objects.create(name="Kenya", country_code="KEN", country_code_alpha_2="KE", currency="KES") diff --git a/visualization/tests/test_views.py b/visualization/tests/test_views.py index eb23b8c..3df1313 100644 --- a/visualization/tests/test_views.py +++ b/visualization/tests/test_views.py @@ -1,63 +1,120 @@ +from django.core.management import call_command from django.test import TestCase from django.urls import reverse from rest_framework import status +from content.models import CountryPartner +from country.models import Buyer, Country, Language, Supplier, Tender, Topic + + +def setUpModule(): + call_command("loaddata", "country", "redflag", "equitycategory", "equitykeywords") + Language.objects.create( + name="english", + code="en", + ) + Topic.objects.create(title="corona virus") + supplier = Supplier.objects.create( + supplier_id="1", + supplier_name="sample supplier", + supplier_address="kathmandu", + ) + + buyer = Buyer.objects.create( + buyer_id="1", + buyer_name="sample buyer", + buyer_address="kathmandu", + ) + + Tender.objects.create( + country=Country.objects.all().first(), + supplier=supplier, + buyer=buyer, + contract_id=1, + contract_date="2021-01-01", + procurement_procedure="open", + status="active", + link_to_contract="http://test.com", + link_to_tender="http://test.com", + data_source="http://test.com", + no_of_bidders=1, + contract_title="test", + contract_value_local=1.0, + contract_value_usd=1.0, + contract_desc="test description", + tender_value_local=1.0, + tender_value_usd=1.0, + award_value_local=1.0, + award_value_usd=1.0, + ) + + CountryPartner.objects.create( + name="united kingdom", + description="country description", + country=Country.objects.all().first(), + email="country@email.com", + website="example.com", + logo="country/partner/logo/gl.jpg", + ) + class VisualizationViewTest(TestCase): def setUp(self): - self.total_contract_list_url = "total_contracts" - self.total_contracts_url = "total_contracts" - self.total_spending_url = "total_spending" self.average_bids_url = "average_bids" - self.world_map_race_url = "world_map_race" - self.top_suppliers_url = "top_suppliers" - self.top_buyers_url = "top_buyers" - self.direct_open_url = "direct_open" + self.buyer_detail_url = "buyer_detail" + self.buyer_summary_url = "buyer_summary" + self.buyer_trend_url = "buyer_trend" + self.contract_red_flags_url = "contract_red_flags" self.contract_status_url = "contract_status" - self.quantity_correlation_url = "quantity_correlation" - self.monopolization_url = "monopolization" - self.country_suppliers_url = "country_suppliers" - self.country_map_url = "country_map" - self.world_map_url = "world_map" self.country_map_api_url = "country_map_api" - self.global_suppliers_url = "global_suppliers" - self.product_distribution_url = "product_distribution" - self.equity_indicators_url = "equity_indicators" - self.product_timeline_url = "product_timeline" - self.product_timeline_race_url = "product_timeline_race" - self.supplier_detail_url = "supplier_detail" - self.buyer_detail_url = "buyer_detail" + self.country_map_url = "country_map" self.country_partners_url = "country_partners" + self.country_suppliers_url = "country_suppliers" self.data_providers_url = "data_providers" - self.buyer_summary_url = "buyer_summary" - self.supplier_summary_url = "supplier_summary" - self.filter_parameters_url = "filter_parameters" - self.product_summary_url = "product_summary" + self.direct_open_contract_trend_url = "direct_open_contract_trend" + self.direct_open_url = "direct_open" + self.equity_indicators_url = "equity_indicators" self.equity_summary_url = "equity_summary" - self.products_url = "products" - self.filters_parameters_suppliers_url = "filters_parameters_suppliers" + self.filter_parameters_url = "filter_parameters" self.filters_parameters_buyers_url = "filters_parameters_buyers" self.filters_parameters_static_url = "filters_parameters_static" + self.filters_parameters_suppliers_url = "filters_parameters_suppliers" + self.global_suppliers_url = "global_suppliers" + self.monopolization_url = "monopolization" + self.product_distribution_url = "product_distribution" + self.product_flow_view_url = "product_flow_view" self.product_spending_comparison_url = "product_spending_comparison" - self.buyer_trend_url = "buyer_trend" - self.supplier_trend_url = "supplier_trend" - self.direct_open_contract_trend_url = "direct_open_contract_trend" - self.contract_red_flags_url = "contract_red_flags" + self.product_summary_url = "product_summary" + self.product_timeline_race_url = "product_timeline_race" + self.product_timeline_url = "product_timeline" + self.products_url = "products" + self.quantity_correlation_url = "quantity_correlation" self.red_flag_summary_url = "red_flag_summary" - self.product_flow_view_url = "product_flow_view" + self.supplier_detail_url = "supplier_detail" self.supplier_flow_view_url = "supplier_flow_view" + self.supplier_summary_url = "supplier_summary" + self.supplier_trend_url = "supplier_trend" + self.top_buyers_url = "top_buyers" + self.top_suppliers_url = "top_suppliers" + self.total_contract_list_url = "total_contracts" + self.total_contracts_url = "total_contracts" + self.total_spending_url = "total_spending" + self.world_map_race_url = "world_map_race" + self.world_map_url = "world_map" def test_total_contracts_GET(self): - response = self.client.get(reverse(self.total_contracts_url)) + url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.total_contracts_url) + response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_total_spending_GET(self): - url = "%s?country=GB&buyer=1&supplier=1" % reverse(self.total_spending_url) + url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.total_spending_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_average_bids_GET(self): - response = self.client.get(reverse(self.average_bids_url)) + url = "%s?country=KE&buyer=1" % reverse(self.average_bids_url) + response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_world_map_race_GET(self): @@ -73,31 +130,32 @@ def test_top_buyers_GET(self): self.assertEquals(response.status_code, status.HTTP_200_OK) def test_direct_open_GET(self): - response = self.client.get(reverse(self.direct_open_url)) + url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.direct_open_url) + response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_contract_status_GET(self): - url = "%s?country=GB&buyer=1" % reverse(self.contract_status_url) + url = "%s?country=KE&buyer=1" % reverse(self.contract_status_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_quantity_correlation_GET(self): - url = "%s?country=GB" % reverse(self.quantity_correlation_url) + url = "%s?country=KE" % reverse(self.quantity_correlation_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_monopolization_GET(self): - url = "%s?country=GB&buyer=1" % reverse(self.monopolization_url) + url = "%s?country=KE&buyer=1" % reverse(self.monopolization_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_country_suppliers_GET(self): - url = "%s?country=GB" % reverse(self.country_suppliers_url) + url = "%s?country=KE" % reverse(self.country_suppliers_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_country_map_GET(self): - url = "%s?country=GB" % reverse(self.country_map_url) + url = "%s?country=KE" % reverse(self.country_map_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) @@ -107,26 +165,31 @@ def test_world_map_GET(self): self.assertEquals(response.status_code, status.HTTP_200_OK) def test_country_map_api_GET(self): - url = "%s?country=GB" % reverse(self.country_map_api_url) + url = "%s?country=KE" % reverse(self.country_map_api_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) + def test_invalid_country_map_api_GET(self): + url = "%s?country=AB" % reverse(self.country_map_api_url) + response = self.client.get(url) + self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + def test_global_suppliers_GET(self): response = self.client.get(reverse(self.global_suppliers_url)) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_product_distribution_GET(self): - url = "%s?country=GB&buyer=1" % reverse(self.product_distribution_url) + url = "%s?country=KE&buyer=1" % reverse(self.product_distribution_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_equity_indicators_GET(self): - url = "%s?country=GB&buyer=1" % reverse(self.equity_indicators_url) + url = "%s?country=KE&buyer=1" % reverse(self.equity_indicators_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_product_timeline_GET(self): - url = "%s?country=GB&buyer=1&supplier=1" % reverse(self.product_timeline_url) + url = "%s?country=KE&buyer=1&supplier=1" % reverse(self.product_timeline_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) @@ -139,26 +202,30 @@ def test_supplier_detail_GET(self): self.assertEquals(response.status_code, status.HTTP_200_OK) def test_buyer_detail_GET(self): - response = self.client.get(reverse(self.buyer_detail_url, args=["1"])) + response = self.client.get(reverse(self.buyer_detail_url, args=[1])) self.assertEquals(response.status_code, status.HTTP_200_OK) + def test_buyer_detail_failed_GET(self): + response = self.client.get(reverse(self.buyer_detail_url, args=[99999])) + self.assertEquals(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + def test_country_partners_GET(self): - url = "%s?country=GB" % reverse(self.country_partners_url) + url = "%s?country=KE" % reverse(self.country_partners_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_data_providers_GET(self): - url = "%s?country=GB" % reverse(self.data_providers_url) + url = "%s?country=KE" % reverse(self.data_providers_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_buyer_summary_GET(self): - url = "%s?country=GB" % reverse(self.buyer_summary_url) + url = "%s?country=gl" % reverse(self.buyer_summary_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_supplier_summary_GET(self): - url = "%s?country=GB" % reverse(self.supplier_summary_url) + url = "%s?country=KE" % reverse(self.supplier_summary_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) @@ -167,25 +234,28 @@ def test_filter_parameters_GET(self): self.assertEquals(response.status_code, status.HTTP_200_OK) def test_product_summary_GET(self): - response = self.client.get(reverse(self.product_summary_url)) + url = "%s?country=KE" % reverse(self.product_summary_url) + response = self.client.get(url) + print(response.json()) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_equity_summary_GET(self): - url = "%s?country=GB" % reverse(self.equity_summary_url) + url = "%s?country=KE" % reverse(self.equity_summary_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_products_GET(self): - response = self.client.get(reverse(self.products_url)) + url = "%s?country=KE&buyer=1" % reverse(self.products_url) + response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_filters_parameters_suppliers_GET(self): - url = "%s?country=GB&buyer=1" % reverse(self.filters_parameters_suppliers_url) + url = "%s?country=KE&buyer=1" % reverse(self.filters_parameters_suppliers_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_filters_parameters_buyers_GET(self): - url = "%s?country=GB&supplier=1" % reverse(self.filters_parameters_buyers_url) + url = "%s?country=KE&supplier=1" % reverse(self.filters_parameters_buyers_url) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) @@ -211,7 +281,8 @@ def test_direct_open_contract_trend_GET(self): self.assertEquals(response.status_code, status.HTTP_200_OK) def test_contract_red_flags_GET(self): - response = self.client.get(reverse(self.contract_red_flags_url)) + url = "%s?country=KE&buyer=1&supplier=1&product=1" % reverse(self.contract_red_flags_url) + response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) def test_red_flag_summary_GET(self): diff --git a/visualization/urls.py b/visualization/urls.py index 0bb8c10..8cb1abb 100644 --- a/visualization/urls.py +++ b/visualization/urls.py @@ -33,10 +33,10 @@ path("equity-summary/", views.EquitySummaryView.as_view(), name="equity_summary"), path("products/", views.ProductTableView.as_view(), name="products"), path( - "filters-parameters/suppliers/", views.FilterParametersSuppliers.as_view(), name="filters_parameters_suppliers" + "filter-parameters/suppliers/", views.FilterParametersSuppliers.as_view(), name="filters_parameters_suppliers" ), - path("filters-parameters/buyers/", views.FilterParametersBuyers.as_view(), name="filters_parameters_buyers"), - path("filters-parameters/static/", views.FilterParametersStatic.as_view(), name="filters_parameters_static"), + path("filter-parameters/buyers/", views.FilterParametersBuyers.as_view(), name="filters_parameters_buyers"), + path("filter-parameters/static/", views.FilterParametersStatic.as_view(), name="filters_parameters_static"), path( "product-spending-comparison/", views.ProductSpendingComparison.as_view(), name="product_spending_comparison" ), diff --git a/visualization/views/buyer_profile_view.py b/visualization/views/buyer_profile_view.py index a7230ee..0c207bf 100644 --- a/visualization/views/buyer_profile_view.py +++ b/visualization/views/buyer_profile_view.py @@ -2,9 +2,10 @@ from django.http import JsonResponse from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page +from rest_framework import status from rest_framework.views import APIView -from country.models import Buyer, Tender +from country.models import Buyer from visualization.helpers.general import page_expire_period @@ -12,28 +13,29 @@ class BuyerProfileView(APIView): @method_decorator(cache_page(page_expire_period())) def get(self, request, *args, **kwargs): pk = self.kwargs["pk"] - data = {} try: instance = Buyer.objects.get(id=pk) + tenders = instance.tenders buyer_detail = ( - Tender.objects.filter(buyer_id=pk) - .values("country__name", "country__country_code_alpha_2") + tenders.values("country__name", "country__country_code_alpha_2") .annotate( - total_usd=Sum("goods_services__contract_value_usd"), - total_local=Sum("goods_services__contract_value_local"), + total_usd=Sum("contract_value_usd"), + total_local=Sum("contract_value_local"), ) + .first() ) - tender_count = Tender.objects.filter(buyer_id=pk).count() - data["name"] = instance.buyer_name - data["id"] = pk - data["code"] = instance.buyer_id - data["address"] = instance.buyer_address - data["amount_usd"] = buyer_detail[0]["total_usd"] - data["amount_local"] = buyer_detail[0]["total_local"] - data["tender_count"] = tender_count - data["country_code"] = buyer_detail[0]["country__country_code_alpha_2"] - data["country_name"] = buyer_detail[0]["country__name"] - return JsonResponse(data, safe=False) - except Exception: - data["error"] = "Enter valid ID" - return JsonResponse(data, safe=False) + data = { + "name": instance.buyer_name, + "id": pk, + "code": instance.buyer_id, + "address": instance.buyer_address, + "amount_usd": buyer_detail.get("total_usd", 0), + "amount_local": buyer_detail.get("total_local", 0), + "tender_count": tenders.distinct().count(), + "country_code": buyer_detail.get("country__country_code_alpha_2", ""), + "country_name": buyer_detail.get("country__name", ""), + } + return JsonResponse(data, safe=False, status=status.HTTP_200_OK) + except Exception as E: + error = {"error": str(E)} + return JsonResponse(error, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/visualization/views/buyer_summary_view.py b/visualization/views/buyer_summary_view.py index dc0c14b..ca5541a 100644 --- a/visualization/views/buyer_summary_view.py +++ b/visualization/views/buyer_summary_view.py @@ -40,9 +40,7 @@ def get(self, request): .aggregate(total=Count("buyer")) ) for details in buyer_details: - data = {} - data["buyer_count"] = details["count"] - data["month"] = details["month"] + data = {"buyer_count": details["count"], "month": details["month"]} trend.append(data) try: dates_in_details = [i["month"] for i in buyer_details] diff --git a/visualization/views/country_map_view.py b/visualization/views/country_map_view.py index c704afd..94a1a6b 100644 --- a/visualization/views/country_map_view.py +++ b/visualization/views/country_map_view.py @@ -2,6 +2,7 @@ from django.http import JsonResponse from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page +from rest_framework import status from rest_framework.views import APIView from country.models import Country, Tender @@ -15,20 +16,22 @@ def get(self, request): try: country_instance = Country.objects.get(country_code_alpha_2=country) except Exception: - return JsonResponse({"result": "Invalid Alpha Code"}) + return JsonResponse({"result": "Invalid Alpha Code"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if country is not None and country_instance is not None: tender_instance = Tender.objects.filter(country__country_code_alpha_2=country).aggregate( total_usd=Sum("goods_services__contract_value_usd"), total_local=Sum("goods_services__contract_value_local"), ) count = Tender.objects.filter(country__country_code_alpha_2=country).count() - final = {} - final["country_code"] = country_instance.country_code_alpha_2 - final["country"] = country_instance.name - final["country_continent"] = country_instance.continent - final["amount_usd"] = tender_instance["total_usd"] - final["amount_local"] = tender_instance["total_local"] - final["tender_count"] = count + final = { + "country_code": country_instance.country_code_alpha_2, + "country": country_instance.name, + "country_continent": country_instance.continent, + "amount_usd": tender_instance["total_usd"], + "amount_local": tender_instance["total_local"], + "tender_count": count, + } + return JsonResponse(final) else: - final = {"result": "Invalid Alpha Code"} - return JsonResponse(final) + error = {"result": "Invalid Alpha Code"} + return JsonResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/visualization/views/country_partner_view.py b/visualization/views/country_partner_view.py index e779dde..66cd8c8 100644 --- a/visualization/views/country_partner_view.py +++ b/visualization/views/country_partner_view.py @@ -17,18 +17,19 @@ def get(self, request): try: data_provider = CountryPartner.objects.filter(**filter_args) except Exception: - data_provider = [{"error": "Country partner doesnot exist for this country"}] + data_provider = [{"error": "Country partner does not exist for this country"}] result = [] if data_provider: for i in data_provider: - data = {} - data["name"] = i.name - data["description"] = i.description - data["email"] = i.email - data["website"] = i.website - data["logo"] = str(i.logo) - data["order"] = i.order - data["country"] = str(i.country) + data = { + "name": i.name, + "description": i.description, + "email": i.email, + "website": i.website, + "logo": str(i.logo), + "order": i.order, + "country": str(i.country), + } result.append(data) else: result = {"error": "Country Partner not found for this country"} diff --git a/visualization/views/data_provider_view.py b/visualization/views/data_provider_view.py index 3287222..2673ce1 100644 --- a/visualization/views/data_provider_view.py +++ b/visualization/views/data_provider_view.py @@ -17,16 +17,17 @@ def get(self, request): try: data_provider = DataProvider.objects.filter(**filter_args) except Exception: - data_provider = [{"error": "Data Provider doesnot exist for this country"}] + data_provider = [{"error": "Data Provider does not exist for this country"}] result = [] if data_provider: for i in data_provider: - data = {} - data["name"] = i.name - data["country"] = str(i.country) - data["website"] = i.website - data["logo"] = str(i.logo) - data["remark"] = i.remark + data = { + "name": i.name, + "country": str(i.country), + "website": i.website, + "logo": str(i.logo), + "remark": i.remark, + } result.append(data) else: result = {"error": "Data Provider not found for this country"} diff --git a/visualization/views/equity_summary_view.py b/visualization/views/equity_summary_view.py index 72d33a1..05684a5 100644 --- a/visualization/views/equity_summary_view.py +++ b/visualization/views/equity_summary_view.py @@ -30,12 +30,13 @@ def get(self, request): .order_by("-month") ) for detail in equity_summary: - data = {} - data["amount_local"] = detail["local"] - data["amount_usd"] = detail["usd"] - data["equity"] = detail["equity_category__category_name"] - data["equity_category_id"] = detail["equity_category"] - data["month"] = detail["month"] - data["tender_count"] = detail["total"] + data = { + "amount_local": detail["local"], + "amount_usd": detail["usd"], + "equity": detail["equity_category__category_name"], + "equity_category_id": detail["equity_category"], + "month": detail["month"], + "tender_count": detail["total"], + } result.append(data) return JsonResponse(result, safe=False) diff --git a/visualization/views/filter_parameters_buyers.py b/visualization/views/filter_parameters_buyers.py index 087bf8e..da0d906 100644 --- a/visualization/views/filter_parameters_buyers.py +++ b/visualization/views/filter_parameters_buyers.py @@ -3,7 +3,7 @@ from django.views.decorators.cache import cache_page from rest_framework.views import APIView -from country.models import Country, Tender +from country.models import Tender from visualization.helpers.general import page_expire_period from visualization.views.lib.general import add_filter_args @@ -11,28 +11,31 @@ class FilterParametersBuyers(APIView): @method_decorator(cache_page(page_expire_period())) def get(self, request): - filter_args = {} - result = [] - country = self.request.GET.get("country", None) - supplier = self.request.GET.get("supplier", None) - if supplier: - filter_args = add_filter_args("supplier", supplier, filter_args) + country_code = self.request.GET.get("country", None) + supplier_id = self.request.GET.get("supplier", None) + + filter_args = {"buyer__isnull": False} + + if supplier_id: + filter_args = add_filter_args("supplier", supplier_id, filter_args) + + if country_code: + country_code = str(country_code).upper() + filter_args["country__country_code_alpha_2"] = country_code + try: - if country: - filter_args["country__country_code_alpha_2"] = country - instance = Country.objects.get(country_code_alpha_2=country) - country_code = instance.country_code_alpha_2 - filter_args["buyer__isnull"] = False buyers = Tender.objects.filter(**filter_args).values("buyer__id", "buyer__buyer_name").distinct() + + result = [] for buyer in buyers: - data = {} - data["id"] = buyer["buyer__id"] - data["name"] = buyer["buyer__buyer_name"] - if country: + data = {"id": buyer["buyer__id"], "name": buyer["buyer__buyer_name"]} + + if country_code: data["country_code"] = country_code - else: - data["country_code"] = "USD" + result.append(data) + return JsonResponse(result, safe=False) + except Exception: - return JsonResponse([{"error": "Country code doest not exists"}], safe=False) + return JsonResponse([{"error": "Country code does not exists"}], safe=False) diff --git a/visualization/views/filter_parameters_suppliers.py b/visualization/views/filter_parameters_suppliers.py index 7eaec6a..23d8a04 100644 --- a/visualization/views/filter_parameters_suppliers.py +++ b/visualization/views/filter_parameters_suppliers.py @@ -3,7 +3,7 @@ from django.views.decorators.cache import cache_page from rest_framework.views import APIView -from country.models import Country, Tender +from country.models import Tender from visualization.helpers.general import page_expire_period from visualization.views.lib.general import add_filter_args @@ -11,30 +11,33 @@ class FilterParametersSuppliers(APIView): @method_decorator(cache_page(page_expire_period())) def get(self, request): - filter_args = {} - result = [] - country = self.request.GET.get("country", None) + country_code = self.request.GET.get("country", None) buyer = self.request.GET.get("buyer", None) + + filter_args = {"supplier__isnull": False} + if buyer: filter_args = add_filter_args("buyer", buyer, filter_args) + + if country_code: + filter_args["country__country_code_alpha_2"] = country_code + try: - if country: - filter_args["country__country_code_alpha_2"] = country - instance = Country.objects.get(country_code_alpha_2=country) - country_code = instance.country_code_alpha_2 - filter_args["supplier__isnull"] = False suppliers = ( Tender.objects.filter(**filter_args).values("supplier__id", "supplier__supplier_name").distinct() ) + + result = [] + for supplier in suppliers: - data = {} - data["id"] = supplier["supplier__id"] - data["name"] = supplier["supplier__supplier_name"] - if country: + data = {"id": supplier["supplier__id"], "name": supplier["supplier__supplier_name"]} + + if country_code: data["country_code"] = country_code - else: - data["country_code"] = "USD" + result.append(data) + return JsonResponse(result, safe=False) + except Exception: - return JsonResponse([{"error": "Country code doest not exists"}], safe=False) + return JsonResponse([{"error": "Country code does not exists"}], safe=False) diff --git a/visualization/views/product_distribution_view.py b/visualization/views/product_distribution_view.py index 04d381e..d72edeb 100644 --- a/visualization/views/product_distribution_view.py +++ b/visualization/views/product_distribution_view.py @@ -17,10 +17,10 @@ def get(self, request): if country: filter_args["country__country_code_alpha_2"] = country if buyer: - if buyer != "notnull": - filter_args["contract__buyer__buyer_id"] = buyer - else: + filter_args["contract__buyer__buyer_id"] = buyer + if buyer == "notnull": filter_args["contract__buyer__isnull"] = False + result = [] goods_services = ( GoodsServices.objects.filter(**filter_args) @@ -32,14 +32,15 @@ def get(self, request): ) ) for goods in goods_services: - data = {} - data["product_name"] = goods["goods_services_category__category_name"] - data["product_id"] = goods["goods_services_category__id"] + data = { + "product_name": goods["goods_services_category__category_name"], + "product_id": goods["goods_services_category__id"], + "local_currency_code": "USD", + } if country: instance = Country.objects.get(country_code_alpha_2=country) data["local_currency_code"] = instance.currency - else: - data["local_currency_code"] = "USD" + data["tender_count"] = goods["tender"] data["amount_local"] = goods["local"] data["amount_usd"] = goods["usd"] diff --git a/visualization/views/product_summary_view.py b/visualization/views/product_summary_view.py index 54e80ab..29f4c17 100644 --- a/visualization/views/product_summary_view.py +++ b/visualization/views/product_summary_view.py @@ -33,12 +33,13 @@ def get(self, request): ) ) for tender in tenders_assigned: - data = {} - data["amount_local"] = tender["local"] - data["amount_usd"] = tender["usd"] - data["local_currency_code"] = currency - data["product_id"] = tender["goods_services__goods_services_category__id"] - data["product_name"] = tender["goods_services__goods_services_category__category_name"] - data["tender_count"] = tender["count"] + data = { + "amount_local": tender["local"], + "amount_usd": tender["usd"], + "local_currency_code": currency, + "product_id": tender["goods_services__goods_services_category__id"], + "product_name": tender["goods_services__goods_services_category__category_name"], + "tender_count": tender["count"], + } result.append(data) return JsonResponse(result, safe=False) diff --git a/visualization/views/product_table_view.py b/visualization/views/product_table_view.py index 061fe5e..563ffc4 100644 --- a/visualization/views/product_table_view.py +++ b/visualization/views/product_table_view.py @@ -31,13 +31,14 @@ def get(self, request): ) ) for product in product_tables: - data = {} - data["amount_local"] = product["local"] - data["amount_usd"] = product["usd"] - data["product_id"] = product["goods_services__goods_services_category__id"] - data["product_name"] = product["goods_services__goods_services_category__category_name"] - data["buyer_count"] = product["buyer"] - data["supplier_count"] = product["supplier"] - data["tender_count"] = product["total"] + data = { + "amount_local": product["local"], + "amount_usd": product["usd"], + "product_id": product["goods_services__goods_services_category__id"], + "product_name": product["goods_services__goods_services_category__category_name"], + "buyer_count": product["buyer"], + "supplier_count": product["supplier"], + "tender_count": product["total"], + } result.append(data) return JsonResponse(result, safe=False) diff --git a/visualization/views/product_timeline_race_view.py b/visualization/views/product_timeline_race_view.py index 650ccdc..839b089 100644 --- a/visualization/views/product_timeline_race_view.py +++ b/visualization/views/product_timeline_race_view.py @@ -40,9 +40,7 @@ def get(self, request): for tender in tenders: end_date = tender["month"] + dateutil.relativedelta.relativedelta(months=1) start_date = tender["month"] - result = {} - result["month"] = str(start_date.year) + "-" + str(start_date.month) - result["details"] = [] + result = {"month": str(start_date.year) + "-" + str(start_date.month), "details": []} for category in categories: data = {} if country: diff --git a/visualization/views/product_timeline_view.py b/visualization/views/product_timeline_view.py index 6e419fa..b0aba2e 100644 --- a/visualization/views/product_timeline_view.py +++ b/visualization/views/product_timeline_view.py @@ -46,14 +46,15 @@ def get(self, request): .order_by("-month") ) for tender in tenders_assigned: - data = {} - data["amount_local"] = tender["local"] - data["amount_usd"] = tender["usd"] - data["date"] = tender["month"] - data["local_currency_code"] = currency - data["product_id"] = tender["goods_services__goods_services_category__id"] - data["product_name"] = tender["goods_services__goods_services_category__category_name"] - data["tender_count"] = tender["count"] + data = { + "amount_local": tender["local"], + "amount_usd": tender["usd"], + "date": tender["month"], + "local_currency_code": currency, + "product_id": tender["goods_services__goods_services_category__id"], + "product_name": tender["goods_services__goods_services_category__category_name"], + "tender_count": tender["count"], + } result.append(data) return JsonResponse(result, safe=False) except Exception: @@ -89,4 +90,3 @@ def get(self, request): return JsonResponse(result, safe=False) except Exception: return JsonResponse([{"error": "Invalid country_code"}], safe=False) - return JsonResponse(data, safe=False) diff --git a/visualization/views/quantity_correlation.py b/visualization/views/quantity_correlation.py index 42d736e..7f126c4 100644 --- a/visualization/views/quantity_correlation.py +++ b/visualization/views/quantity_correlation.py @@ -56,14 +56,15 @@ def get(self, request): except Exception: active_case_count = 0 death_count = 0 - a = {} - a["active_cases"] = active_case_count - a["death_cases"] = death_count - a["amount_local"] = i["local"] if "local" in i else "" - a["amount_usd"] = i["usd"] - a["local_currency_code"] = i["country__currency"] if "country__currency" in i else "" - a["month"] = i["month"] - a["tender_count"] = i["count"] + 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) return JsonResponse(contracts_quantity_list, safe=False) diff --git a/visualization/views/red_flag_summary_view.py b/visualization/views/red_flag_summary_view.py index 1df9c80..d7d119e 100644 --- a/visualization/views/red_flag_summary_view.py +++ b/visualization/views/red_flag_summary_view.py @@ -31,12 +31,13 @@ def get(self, request): ) for detail in equity_summary: if detail["red_flag__implemented"]: - data = {} - data["amount_local"] = detail["local"] - data["amount_usd"] = detail["usd"] - data["red_flag"] = detail["red_flag__title"] - data["red_flag_id"] = detail["red_flag"] - data["month"] = detail["month"] - data["tender_count"] = detail["total"] + data = { + "amount_local": detail["local"], + "amount_usd": detail["usd"], + "red_flag": detail["red_flag__title"], + "red_flag_id": detail["red_flag"], + "month": detail["month"], + "tender_count": detail["total"], + } result.append(data) return JsonResponse(result, safe=False) diff --git a/visualization/views/supplier_profile_view.py b/visualization/views/supplier_profile_view.py index 3ae9e09..9a190fa 100644 --- a/visualization/views/supplier_profile_view.py +++ b/visualization/views/supplier_profile_view.py @@ -12,7 +12,7 @@ class SupplierProfileView(APIView): @method_decorator(cache_page(page_expire_period())) def get(self, request, *args, **kwargs): pk = self.kwargs["pk"] - data = {} + try: instance = Supplier.objects.get(id=pk) supplier_detail = ( @@ -24,16 +24,18 @@ def get(self, request, *args, **kwargs): ) ) tender_count = Tender.objects.filter(supplier_id=pk).count() - data["name"] = instance.supplier_name - data["id"] = pk - data["code"] = instance.supplier_id - data["address"] = instance.supplier_address - data["amount_usd"] = supplier_detail[0]["total_usd"] - data["amount_local"] = supplier_detail[0]["total_local"] - data["tender_count"] = tender_count - data["country_code"] = supplier_detail[0]["country__country_code_alpha_2"] - data["country_name"] = supplier_detail[0]["country__name"] + data = { + "name": instance.supplier_name, + "id": pk, + "code": instance.supplier_id, + "address": instance.supplier_address, + "amount_usd": supplier_detail[0]["total_usd"], + "amount_local": supplier_detail[0]["total_local"], + "tender_count": tender_count, + "country_code": supplier_detail[0]["country__country_code_alpha_2"], + "country_name": supplier_detail[0]["country__name"], + } return JsonResponse(data, safe=False) except Exception: - data["error"] = "Enter valid ID" - return JsonResponse(data, safe=False) + error = {"error": "Enter valid ID"} + return JsonResponse(error, safe=False) diff --git a/visualization/views/supplier_summary_view.py b/visualization/views/supplier_summary_view.py index c638a99..579a250 100644 --- a/visualization/views/supplier_summary_view.py +++ b/visualization/views/supplier_summary_view.py @@ -40,9 +40,7 @@ def get(self, request): .aggregate(total=Count("supplier")) ) for details in supplier_details: - data = {} - data["supplier_count"] = details["count"] - data["month"] = details["month"] + data = {"supplier_count": details["count"], "month": details["month"]} trend.append(data) try: dates_in_details = [i["month"] for i in supplier_details] diff --git a/visualization/views/top_buyers.py b/visualization/views/top_buyers.py index ac6907d..fd7bca8 100644 --- a/visualization/views/top_buyers.py +++ b/visualization/views/top_buyers.py @@ -46,21 +46,23 @@ def get(self, request): by_number = [] by_value = [] for value in for_value: - a = {} - a["amount_local"] = value["local"] if value["usd"] else 0 - a["amount_usd"] = value["usd"] if value["usd"] else 0 - a["local_currency_code"] = value["country__currency"] - a["buyer_id"] = value["buyer__id"] - a["buyer_name"] = value["buyer__buyer_name"] - a["tender_count"] = value["count"] + a = { + "amount_local": value["local"] if value["usd"] else 0, + "amount_usd": value["usd"] if value["usd"] else 0, + "local_currency_code": value["country__currency"], + "buyer_id": value["buyer__id"], + "buyer_name": value["buyer__buyer_name"], + "tender_count": value["count"], + } by_value.append(a) for value in for_number: - a = {} - a["amount_local"] = value["local"] if value["usd"] else 0 - a["amount_usd"] = value["usd"] if value["usd"] else 0 - a["local_currency_code"] = value["country__currency"] - a["buyer_id"] = value["buyer__id"] - a["buyer_name"] = value["buyer__buyer_name"] - a["tender_count"] = value["count"] + a = { + "amount_local": value["local"] if value["usd"] else 0, + "amount_usd": value["usd"] if value["usd"] else 0, + "local_currency_code": value["country__currency"], + "buyer_id": value["buyer__id"], + "buyer_name": value["buyer__buyer_name"], + "tender_count": value["count"], + } by_number.append(a) return JsonResponse({"by_number": by_number, "by_value": by_value}) diff --git a/visualization/views/top_suppliers.py b/visualization/views/top_suppliers.py index a5f0d22..9914d24 100644 --- a/visualization/views/top_suppliers.py +++ b/visualization/views/top_suppliers.py @@ -56,30 +56,33 @@ def get(self, request): ) for value in for_value: - a = {} - a["amount_local"] = value["local"] if value["local"] else 0 - a["amount_usd"] = value["usd"] if value["usd"] else 0 - a["local_currency_code"] = value["country__currency"] - a["supplier_id"] = value["supplier__id"] - a["supplier_name"] = value["supplier__supplier_name"] - a["tender_count"] = value["count"] + a = { + "amount_local": value["local"] if value["local"] else 0, + "amount_usd": value["usd"] if value["usd"] else 0, + "local_currency_code": value["country__currency"], + "supplier_id": value["supplier__id"], + "supplier_name": value["supplier__supplier_name"], + "tender_count": value["count"], + } by_value.append(a) for value in for_number: - a = {} - a["amount_local"] = value["local"] if value["local"] else 0 - a["amount_usd"] = value["usd"] if value["usd"] else 0 - a["local_currency_code"] = value["country__currency"] - a["supplier_id"] = value["supplier__id"] - a["supplier_name"] = value["supplier__supplier_name"] - a["tender_count"] = value["count"] + a = { + "amount_local": value["local"] if value["local"] else 0, + "amount_usd": value["usd"] if value["usd"] else 0, + "local_currency_code": value["country__currency"], + "supplier_id": value["supplier__id"], + "supplier_name": value["supplier__supplier_name"], + "tender_count": value["count"], + } by_number.append(a) for value in for_buyer: - a = {} - a["supplier_id"] = value["supplier__id"] - a["local_currency_code"] = value["country__currency"] - a["supplier_name"] = value["supplier__supplier_name"] - a["buyer_count"] = value["count"] + a = { + "supplier_id": value["supplier__id"], + "local_currency_code": value["country__currency"], + "supplier_name": value["supplier__supplier_name"], + "buyer_count": value["count"], + } by_buyer.append(a) return JsonResponse({"by_number": by_number, "by_value": by_value, "by_buyer": by_buyer}) diff --git a/visualization/views/total_spendings_view.py b/visualization/views/total_spendings_view.py index 4e3a4d0..d6cfd10 100644 --- a/visualization/views/total_spendings_view.py +++ b/visualization/views/total_spendings_view.py @@ -23,8 +23,7 @@ def get(self, request): supplier = self.request.GET.get("supplier") filter_args = {} - exclude_args = {} - exclude_args["status"] = "canceled" + exclude_args = {"status": "canceled"} if country: filter_args["country__country_code_alpha_2"] = country if buyer: