Skip to content

Commit

Permalink
Merge pull request #2079 from IFRCGo/fix/staging-issue
Browse files Browse the repository at this point in the history
Staging issue fixes
  • Loading branch information
thenav56 authored Apr 4, 2024
2 parents 95f20b3 + 7e5dba3 commit ec8d589
Show file tree
Hide file tree
Showing 28 changed files with 784 additions and 202 deletions.
21 changes: 10 additions & 11 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Subquery,
Avg
)
from django.db.models.functions import TruncMonth, TruncYear, Coalesce
from django.db.models.functions import TruncMonth, Coalesce
from django.db.models.fields import IntegerField
from django.utils import timezone
from drf_spectacular.utils import extend_schema, extend_schema_view
Expand Down Expand Up @@ -150,7 +150,6 @@
CountryKeyDocumentFilter,
CountrySupportingPartnerFilter
)
from api.utils import bad_request
from api.visibility_class import ReadOnlyVisibilityViewsetMixin


Expand Down Expand Up @@ -338,7 +337,7 @@ def get_country_figure(self, request, pk):
def get_country_disaster_count(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)

Expand Down Expand Up @@ -378,23 +377,23 @@ def get_country_disaster_count(self, request, pk):
def get_country_disaster_monthly_count(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)
queryset = Event.objects.filter(
queryset = Event.objects.filter(
countries__in=[country.id],
dtype__isnull=False,
).annotate(
date=TruncMonth('created_at')
).values('date', 'countries').annotate(
targeted_population=Avg(
).values('date', 'countries', 'dtype').annotate(
targeted_population=Coalesce(Avg(
'appeals__num_beneficiaries',
filter=models.Q(appeals__num_beneficiaries__isnull=False)
),
filter=models.Q(appeals__num_beneficiaries__isnull=False),
output_field=models.IntegerField(),
), 0),
disaster_name=F('dtype__name'),
disaster_id=F('dtype__id'),
).order_by('date', 'countries', 'dtype__name')

if start_date and end_date:
queryset = queryset.filter(
disaster_start_date__gte=start_date,
Expand All @@ -420,7 +419,7 @@ def get_country_disaster_monthly_count(self, request, pk):
def get_country_historical_disaster(self, request, pk):
country = self.get_object()
end_date = timezone.now()
start_date = end_date + timedelta(days=-2*365)
start_date = end_date + timedelta(days=-2 * 365)
start_date = request.GET.get("start_date", start_date)
end_date = request.GET.get("end_date", end_date)
dtype = request.GET.get("dtype", None)
Expand Down
13 changes: 9 additions & 4 deletions api/management/commands/ingest_ns_contact.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import requests
from requests.auth import HTTPBasicAuth
import xmltodict
import json
from datetime import datetime
import re

from django.core.management.base import BaseCommand
from django.conf import settings
Expand Down Expand Up @@ -46,19 +46,24 @@ def handle(self, *args, **kwargs):
city_code = data['ADD_city_code'] if type(data['ADD_city_code']) == str else None
phone = data['ADD_phone'] if type(data['ADD_phone']) == str else None
website = data['ADD_webSite'] if type(data['ADD_webSite']) == str else None
email = data['ADD_email'] if type(data['ADD_email']) == str and data['ADD_email'] != None else None
emails = data['ADD_email'] if type(data['ADD_email']) == str and data['ADD_email'] != None else None
founded_date = data['ADD_orgCreation'] if type(data['ADD_orgCreation']) == str else None
iso = data['ADD_country_code']
# # get the country and try to update the data for those country
country = Country.objects.filter(iso=iso.upper()).first()
email_splitted = None
if emails:
# NOTE: Split the email
# eg; secretariatgeneral@creuroja.ad;secretariatadmin@@creuroja.ad
email_splitted = re.split('[,; ]+', emails)
if country:
added += 1
country.address_1 = address_1
country.address_2 = address_2
country.city_code = city_code
country.phone = phone
country.website = website
country.email = email
country.emails = email_splitted
if founded_date:
try:
country.founded_date = datetime.strptime(founded_date, "%d.%m.%Y").date()
Expand All @@ -79,7 +84,7 @@ def handle(self, *args, **kwargs):
'city_code',
'phone',
'website',
'email',
'emails',
'founded_date'
]
)
Expand Down
23 changes: 23 additions & 0 deletions api/migrations/0208_auto_20240404_0518.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-04-04 05:18

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0207_auto_20240311_1044'),
]

operations = [
migrations.RemoveField(
model_name='country',
name='email',
),
migrations.AddField(
model_name='country',
name='emails',
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Email'), blank=True, default=list, null=True, size=None),
),
]
15 changes: 10 additions & 5 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ class Country(models.Model):
null=True, blank=True,
max_length=255
)
email = models.CharField(
verbose_name=_('Email'),
null=True, blank=True,
max_length=255
emails = ArrayField(
models.CharField(
verbose_name=_('Email'),
null=True, blank=True,
max_length=255,
),
default=list,
null=True, blank=True
)
founded_date = models.DateField(
verbose_name=_('Found date'),
Expand Down Expand Up @@ -372,6 +376,7 @@ class SupportingPartnerType(models.IntegerChoices):
def __str__(self):
return f'{self.country.name} - {self.first_name} - {self.last_name}'


class CountryKeyDocument(models.Model):
country = models.ForeignKey(
Country,
Expand All @@ -386,7 +391,7 @@ class CountryKeyDocument(models.Model):
verbose_name=_('Url'),
max_length=255
)
thumbnail= models.CharField(
thumbnail = models.CharField(
verbose_name=_('Thumbnail'),
max_length=255
)
Expand Down
4 changes: 2 additions & 2 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Meta:
"city_code",
"phone",
"website",
"email"
"emails"
)


Expand Down Expand Up @@ -756,7 +756,7 @@ class Meta:
"city_code",
"phone",
"website",
"email",
"emails",
"directory",
"initiatives",
"capacity",
Expand Down
6 changes: 1 addition & 5 deletions databank/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
from . import models

enum_register = {
'fdrs_indicators': models.FDRSIncome.FDRSIndicator,
}
enum_register = {}
115 changes: 115 additions & 0 deletions databank/fixtures/fdrs_indicator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

[
{
"model": "databank.FDRSIndicator",
"pk": 1,
"fields": {
"title": "Home Government",
"description": null
}
},
{
"model": "databank.FDRSIndicator",
"pk": 2,
"fields": {
"title": "Foreign Government",
"description": "Other governments and government departments from outside the country. This includes the EU. Examples are: UK (DFID/FCO), US (USAID/OFDA/OTI/PRM), Sweden (SIDA) etc."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 3,
"fields": {
"title": "Individuals",
"description": "Members of the public, including funds from membership, major donors and legacies."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 4,
"fields": {
"title": "Corporations",
"description": "Including donations, proceeds from joint promotions or sponsorship."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 5,
"fields": {
"title": "Foundations",
"description": "Funds from a 'not for profit' set up with the explicit aim of making donations to other organisations (e.g., Charities Aid Foundation). This includes foundations established by corporations or individuals (e.g., Lebara Foundation and Bill & Melinda Gates Foundation)."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 6,
"fields": {
"title": "UN Agencies and other Multilateral Agencies",
"description": "These include the development institutions of the UN (e.g., UNDP), the World Bank, Global Fund and regional development banks."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 7,
"fields": {
"title": "Pooled funds",
"description": "Mechanisms used to receive contributions from multiple financial partners (mainly government donors) and allocate such resources to multiple implementing entities. Humanitarian pooled funds can be global (the START Fund) or country-based (e.g., the CHF, ERF)."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 8,
"fields": {
"title": "Non-governmental organizations",
"description": "Local and international 'not for profit' civil society organizations independent from states and international governmental organizations."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 9,
"fields": {
"title": "Service income",
"description": "Income linked to the provision of public services; for example, blood, hospital or ambulance services, search and rescue, etc."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 10,
"fields": {
"title": "Income generating activity",
"description": "Income linked to the sale of a product or service; for example, commercial first aid training, retail, sale and/or rental of other products and services."
}
},
{
"model": "databank.FDRSIndicator",
"pk": 11,
"fields": {
"title": "Other National Society",
"description": null
}
},
{
"model": "databank.FDRSIndicator",
"pk": 12,
"fields": {
"title": "IFRC (HQ, regional and countries delegations)",
"description": null
}
},
{
"model": "databank.FDRSIndicator",
"pk": 13,
"fields": {
"title": "ICRC",
"description": null
}
},
{
"model": "databank.FDRSIndicator",
"pk": 14,
"fields": {
"title": "Other",
"description": "Income that does not come from any of the above categories."
}
}
]
35 changes: 28 additions & 7 deletions databank/management/commands/FDRS_INCOME.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,43 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from databank.models import CountryOverview, FDRSIncome
#from .utils import catch_error
from databank.models import (
CountryOverview,
FDRSIncome,
FDRSIndicator
)

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = 'Import FDRS income data'

def handle(self, *args, **kwargs):

FDRS_INDICATORS = [e.value for e in FDRSIncome.FDRSIndicator]

fdrs_indicator_enum_data = {
"h_gov_CHF": "Home Government",
"f_gov_CHF": "Foreign Government",
"ind_CHF": "Individuals",
"corp_CHF": "Corporations",
"found_CHF": "Foundations",
"un_CHF": "UN Agencies and other Multilateral Agencies",
"pooled_f_CHF": "Pooled funds",
"ngo_CHF": "Non-governmental organizations",
"si_CHF": "Service income",
"iga_CHF": "Income generating activity",
"KPI_incomeFromNSsLC_CHF": "Other National Society",
"ifrc_CHF": "IFRC (HQ, regional and countries delegations)",
"icrc_CHF": "ICRC",
"other_CHF": "Other",
}
FDRS_INDICATORS = [key for key in fdrs_indicator_enum_data]
map_indicators = {
indicator.title: indicator
for indicator in FDRSIndicator.objects.all()
}
for overview in CountryOverview.objects.all():
country_fdrs_code = overview.country.fdrs
FDRS_DATA_API_ENDPOINT = f'https://data-api.ifrc.org/api/data?apiKey={settings.FDRS_APIKEY}&KPI_Don_Code={country_fdrs_code}&indicator=' + ','.join(FDRS_INDICATORS)
#@catch_error('Error occured while fetching from FDRS API.')
fdrs_entities = requests.get(FDRS_DATA_API_ENDPOINT)
if fdrs_entities.status_code != 200:
return
Expand All @@ -33,7 +54,7 @@ def handle(self, *args, **kwargs):
data = {
'date': str(income['year']) + '-01-01',
'value': income['value'],
'indicator': indicator,
'indicator': map_indicators.get(fdrs_indicator_enum_data.get(indicator)),
'overview': overview
}
FDRSIncome.objects.create(**data)
Loading

0 comments on commit ec8d589

Please sign in to comment.