Skip to content

Commit

Permalink
Add country delegation in country page
Browse files Browse the repository at this point in the history
  • Loading branch information
k9845 committed Feb 27, 2024
1 parent 340be57 commit b85e1db
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 42 deletions.
72 changes: 72 additions & 0 deletions DO.csv

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class CountryKeyDocumentViewSet(viewsets.ReadOnlyModelViewSet):
queryset = CountryKeyDocument.objects.select_related('country')
serializer_class = CountryKeyDocumentSerializer
search_fields = ("name",)
permission_classes = (IsAuthenticated,)
# permission_classes = (IsAuthenticated,)
filterset_class = CountryKeyDocumentFilter


Expand Down
18 changes: 18 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
DrefOperationalUpdate,
DrefFinalReport
)
from local_units.serializers import MiniDelegationOfficeSerializer
from local_units.models import DelegationOffice


class GeoSerializerMixin:
Expand Down Expand Up @@ -691,6 +693,7 @@ class CountryRelationSerializer(ModelSerializer):
source="countryicrcpresence",
read_only=True,
)
country_delegation = serializers.SerializerMethodField()

class Meta:
model = Country
Expand Down Expand Up @@ -746,6 +749,7 @@ class Meta:
"organizational_capacity",
"icrc_presence",
"disaster_law_url",
"country_delegation",
)

@staticmethod
Expand All @@ -756,6 +760,20 @@ def get_bbox(country) -> dict:
def get_centroid(country) -> dict:
return country.centroid and json.loads(country.centroid.geojson)

@extend_schema_field(
MiniDelegationOfficeSerializer
)
def get_country_delegation(self, country):
return DelegationOffice.objects.filter(
dotype__name="Country Delegation",
country=country,
).values(
'hod_first_name',
'hod_last_name',
'hod_mobile_number',
'hod_email',
).first()


class CountryKeyDocumentSerializer(ModelSerializer):
country_details = MiniCountrySerializer(source='country', read_only=True)
Expand Down
53 changes: 27 additions & 26 deletions databank/management/commands/ingest_worldbank.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Command(BaseCommand):

def handle(self, *args, **kwargs):
world_bank_indicator_map = (
('SP.POP.TOTL', CO.world_bank_population),
('SP.POP.65UP.TO', CO.world_bank_population_above_age_65),
('SP.POP.0014.TO', CO.world_bank_population_age_14),
('SP.URB.TOTL.IN.ZS', CO.world_bank_urban_population_percentage),
('NY.GDP.MKTP.CD', CO.world_bank_gdp),
('NY.GNP.MKTP.CD', CO.world_bank_gni),
('IQ.CPA.GNDR.XQ', CO.world_bank_gender_inequality_index),
('SP.DYN.LE00.IN', CO.world_bank_life_expectancy),
# ('SP.POP.TOTL', CO.world_bank_population),
# ('SP.POP.65UP.TO', CO.world_bank_population_above_age_65),
# ('SP.POP.0014.TO', CO.world_bank_population_age_14),
# ('SP.URB.TOTL.IN.ZS', CO.world_bank_urban_population_percentage),
# ('NY.GDP.MKTP.CD', CO.world_bank_gdp),
# ('NY.GNP.MKTP.CD', CO.world_bank_gni),
# ('IQ.CPA.GNDR.XQ', CO.world_bank_gender_inequality_index),
# ('SP.DYN.LE00.IN', CO.world_bank_life_expectancy),
('SE.ADT.LITR.ZS', CO.world_bank_literacy_rate),
('SI.POV.NAHC', CO.world_bank_poverty_rate)
)
Expand Down Expand Up @@ -73,6 +73,7 @@ def handle(self, *args, **kwargs):
existing_data = next((data for data in country_dict[geo_id] if data[2] == indicator), None)
if existing_data is None or existing_data[1] < year:
country_dict[geo_id].append((pop, year, indicator))
print(country_dict)
logger.info(json.dumps(country_dict))
if 'pages' in response.json()[0]:
if page >= response.json()[0]['pages']:
Expand All @@ -82,26 +83,26 @@ def handle(self, *args, **kwargs):
for country_code, indicators in country_dict.items():
overview = CO.objects.filter(country__iso=country_code).first()
if overview:
overview.world_bank_population = indicators[0][0]
overview.world_bank_population_above_age_65 = indicators[1][0]
overview.world_bank_population_age_14 = indicators[2][0]
overview.world_bank_urban_population_percentage = indicators[3][0]
overview.world_bank_gdp = indicators[4][0]
overview.world_bank_gni = indicators[5][0]
overview.world_bank_gender_inequality_index = indicators[6][0]
overview.world_bank_life_expectancy = indicators[7][0]
overview.world_bank_literacy_rate = indicators[8][0]
overview.world_bank_poverty_rate = indicators[9][0]
# overview.world_bank_population = indicators[0][0]
# overview.world_bank_population_above_age_65 = indicators[1][0]
# overview.world_bank_population_age_14 = indicators[2][0]
# overview.world_bank_urban_population_percentage = indicators[3][0]
# overview.world_bank_gdp = indicators[4][0]
# overview.world_bank_gni = indicators[5][0]
# overview.world_bank_gender_inequality_index = indicators[6][0]
# overview.world_bank_life_expectancy = indicators[7][0]
overview.world_bank_literacy_rate = indicators[0][0]
overview.world_bank_poverty_rate = indicators[1][0]
overview.save(
update_fields=[
'world_bank_population',
'world_bank_population_above_age_65',
'world_bank_population_age_14',
'world_bank_urban_population_percentage',
'world_bank_gdp',
'world_bank_gni',
'world_bank_gender_inequality_index',
'world_bank_life_expectancy',
# 'world_bank_population',
# 'world_bank_population_above_age_65',
# 'world_bank_population_age_14',
# 'world_bank_urban_population_percentage',
# 'world_bank_gdp',
# 'world_bank_gni',
# 'world_bank_gender_inequality_index',
# 'world_bank_life_expectancy',
'world_bank_literacy_rate',
'world_bank_poverty_rate'
]
Expand Down
11 changes: 11 additions & 0 deletions local_units/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,14 @@ def get_type(self, office):
class LocalUnitOptionsSerializer(serializers.Serializer):
type = LocalUnitTypeSerializer(many=True)
level = LocalUnitLevelSerializer(many=True)


class MiniDelegationOfficeSerializer(serializers.ModelSerializer):
class Meta:
model = DelegationOffice
fields = (
'hod_first_name',
'hod_last_name',
'hod_mobile_number',
'hod_email',
)
30 changes: 15 additions & 15 deletions local_units/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ class LocalUnitViewSet(viewsets.ModelViewSet):
filterset_class = LocalUnitFilters
search_fields = ('local_branch_name', 'english_branch_name',)

@action(
detail=False,
url_path="options",
methods=("get",),
serializer_class=LocalUnitOptionsSerializer,
)
def get_options(self, request, pk=None):
return response.Response(
LocalUnitOptionsSerializer(
instance=dict(
type=LocalUnitType.objects.all(),
level=LocalUnitLevel.objects.all(),
)
).data
)

# class LocalUnitDetailAPIView(RetrieveAPIView):
# queryset = LocalUnit.objects.all()
Expand Down Expand Up @@ -60,18 +75,3 @@ class DelegationOfficeDetailAPIView(RetrieveAPIView):
serializer_class = DelegationOfficeSerializer
permission_classes = [permissions.IsAuthenticated]

@action(
detail=False,
url_path="options",
methods=("get",),
serializer_class=LocalUnitOptionsSerializer,
)
def get_options(self, request, pk=None):
return response.Response(
LocalUnitOptionsSerializer(
instance=dict(
type=LocalUnitType.objects.all(),
level=LocalUnitLevel.objects.all(),
)
).data
)

0 comments on commit b85e1db

Please sign in to comment.