This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [x] country api refactor - [x] country suppliers api refactor - [x] product table refactor - [x] country api custom add - [x] country detail api custom add - [x] test cases updated
- Loading branch information
1 parent
cd9661d
commit dc433fc
Showing
10 changed files
with
127 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import socket | ||
|
||
from django.db.models import Count, Max, Sum | ||
from django.http import JsonResponse | ||
from rest_framework.views import APIView | ||
|
||
from country.models import Country, Tender | ||
|
||
|
||
class CountryDetailView(APIView): | ||
def get(self, request, *args, **kwargs): | ||
slug = self.kwargs["slug"] | ||
filter_args = {} | ||
filter_args["country__slug"] = slug | ||
|
||
country = Country.objects.filter(slug=slug).first() | ||
country_detail = [] | ||
try: | ||
tender_data = Tender.objects.filter(**filter_args).aggregate( | ||
amount_usd=Sum("contract_value_usd"), | ||
amount_local=Sum("contract_value_local"), | ||
tender_count=Count("id"), | ||
contract_last_date=Max("contract_date"), | ||
) | ||
|
||
country_detail.append( | ||
{ | ||
"url": "https://" + socket.gethostbyname(socket.gethostname()) + "/api/v1/country/" + slug, | ||
"id": country.id, | ||
"continent": country.continent, | ||
"amount_usd": tender_data["amount_usd"], | ||
"amount_local": tender_data["amount_local"], | ||
"tender_count": tender_data["tender_count"], | ||
"last_contract_date": tender_data["contract_last_date"], | ||
"slug": country.slug, | ||
"name": country.name, | ||
"population": country.population, | ||
"gdp": country.gdp, | ||
"country_code": country.country_code, | ||
"country_code_alpha_2": country.country_code_alpha_2, | ||
"currency": country.currency, | ||
"healthcare_budget": country.healthcare_budget, | ||
"healthcare_gdp_pc": country.healthcare_gdp_pc, | ||
"covid_cases_total": country.covid_cases_total, | ||
"covid_deaths_total": country.covid_deaths_total, | ||
"covid_data_last_updated": country.covid_data_last_updated, | ||
} | ||
) | ||
return JsonResponse(country_detail, safe=False) | ||
except Exception: | ||
return JsonResponse([{"error": "Invalid country_code"}], safe=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,53 @@ | ||
from django.utils.translation import gettext_lazy as _ | ||
from rest_framework import viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
import socket | ||
|
||
from country.models import Country | ||
from country.serializers import CountrySerializer | ||
from django.db.models import Count, Max, Sum | ||
from django.http import JsonResponse | ||
from rest_framework.views import APIView | ||
|
||
from country.models import Country, Tender | ||
|
||
class CountryView(viewsets.ModelViewSet): | ||
pagination_class = None | ||
queryset = Country.objects.all() | ||
serializer_class = CountrySerializer | ||
lookup_field = "slug" | ||
extensions_auto_optimize = True | ||
|
||
def get_queryset(self): | ||
return Country.objects.all() | ||
|
||
@action(detail=False, methods=["get"]) | ||
def choices(self, request): | ||
class CountryView(APIView): | ||
def get(self, request): | ||
countries = Country.objects.all().order_by("name") | ||
serializer = self.get_serializer(countries, many=True) | ||
country_id_and_name = [{"id": country["id"], "name": _(country["name"])} for country in serializer.data] | ||
country_detail = [] | ||
filter_args = {} | ||
|
||
return Response(country_id_and_name) | ||
try: | ||
for country in countries: | ||
filter_args["country__name"] = country | ||
tender_data = Tender.objects.filter(**filter_args).aggregate( | ||
amount_usd=Sum("contract_value_usd"), | ||
amount_local=Sum("contract_value_local"), | ||
tender_count=Count("id"), | ||
contract_last_date=Max("contract_date"), | ||
) | ||
country_detail.append( | ||
{ | ||
"url": "https://" | ||
+ socket.gethostbyname(socket.gethostname()) | ||
+ "/api/v1/country/" | ||
+ country.slug, | ||
"id": country.id, | ||
"continent": country.continent, | ||
"amount_usd": tender_data["amount_usd"], | ||
"amount_local": tender_data["amount_local"], | ||
"tender_count": tender_data["tender_count"], | ||
"last_contract_date": tender_data["contract_last_date"], | ||
"slug": country.slug, | ||
"name": country.name, | ||
"population": country.population, | ||
"gdp": country.gdp, | ||
"country_code": country.country_code, | ||
"country_code_alpha_2": country.country_code_alpha_2, | ||
"currency": country.currency, | ||
"healthcare_budget": country.healthcare_budget, | ||
"healthcare_gdp_pc": country.healthcare_gdp_pc, | ||
"covid_cases_total": country.covid_cases_total, | ||
"covid_deaths_total": country.covid_deaths_total, | ||
"covid_data_last_updated": country.covid_data_last_updated, | ||
} | ||
) | ||
return JsonResponse(country_detail, safe=False) | ||
except Exception: | ||
return JsonResponse([{"error": "Invalid country_code"}], safe=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters