Skip to content

Commit

Permalink
[#1770] Added recipient country and region import
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 10, 2015
1 parent b40056a commit a439974
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
4 changes: 3 additions & 1 deletion akvo/iati/imports/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .descriptions import (background, current_status, goals_overview, project_plan,
project_plan_summary, sustainability, target_group)
from .image import current_image
from .locations import locations
from .locations import locations, recipient_countries, recipient_regions
from .partnerships import partnerships
from .planned_disbursements import planned_disbursements
from .related_projects import related_projects
Expand Down Expand Up @@ -47,6 +47,8 @@
'planned_start_date',
'project_plan',
'project_plan_summary',
'recipient_countries',
'recipient_regions',
'related_projects',
'results',
'scope',
Expand Down
107 changes: 107 additions & 0 deletions akvo/iati/imports/fields/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from ..utils import get_text

from decimal import Decimal, InvalidOperation

from django.core.exceptions import ObjectDoesNotExist
from django.db.models import get_model

Expand Down Expand Up @@ -193,3 +195,108 @@ def administratives(location_element, location, activities_globals):
administrative.delete()

return changes


def recipient_countries(activity, project, activities_globals):
"""
Retrieve and store the recipient countries.
The conditions will be extracted from the 'recipient-country' elements.
:param activity: ElementTree; contains all data of the activity
:param project: Project instance
:param activities_globals: Dictionary; contains all global activities information
:return: List; contains fields that have changed
"""
imported_countries = []
changes = []

for country in activity.findall('recipient-country'):
code = ''
percentage = None

if 'code' in country.attrib.keys():
code = country.attrib['code'].upper()

try:
if 'percentage' in country.attrib.keys():
percentage = Decimal(country.attrib['percentage'])
except InvalidOperation:
pass

text = get_text(country, activities_globals['version'])

rc, created = get_model('rsr', 'recipientcountry').objects.get_or_create(
project=project,
country=code,
percentage=percentage,
text=text
)

if created:
changes.append(u'added recipient country (id: %s): %s' % (str(rc.pk), rc))

imported_countries.append(rc)

for country in project.recipient_countries.all():
if not country in imported_countries:
changes.append(u'deleted recipient country (id: %s): %s' %
(str(country.pk),
country.__unicode__()))
country.delete()

return changes


def recipient_regions(activity, project, activities_globals):
"""
Retrieve and store the recipient regions.
The conditions will be extracted from the 'recipient-region' elements.
:param activity: ElementTree; contains all data of the activity
:param project: Project instance
:param activities_globals: Dictionary; contains all global activities information
:return: List; contains fields that have changed
"""
imported_regions = []
changes = []

for region in activity.findall('recipient-region'):
code = ''
percentage = None
vocabulary = ''

text = get_text(region, activities_globals['version'])

if 'code' in region.attrib.keys():
code = region.attrib['code'].upper()

try:
if 'percentage' in region.attrib.keys():
percentage = Decimal(region.attrib['percentage'])
except InvalidOperation:
pass

if 'vocabulary' in region.attrib.keys():
vocabulary = region.attrib['vocabulary'].upper()

rr, created = get_model('rsr', 'recipientregion').objects.get_or_create(
project=project,
region=code,
percentage=percentage,
text=text,
region_vocabulary=vocabulary
)

if created:
changes.append(u'added recipient region (id: %s): %s' % (str(rr.pk), rr))

imported_regions.append(rr)

for region in project.recipient_regions.all():
if not region in imported_regions:
changes.append(u'deleted recipient region (id: %s): %s' %
(str(region.pk),
region.__unicode__()))
region.delete()

return changes
2 changes: 2 additions & 0 deletions akvo/iati/imports/iati_import_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
'transactions',
'planned_disbursements',
'locations',
'recipient_countries',
'recipient_regions',
]


Expand Down

0 comments on commit a439974

Please sign in to comment.