Skip to content

Commit

Permalink
[#1770] Added country budget item import
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 9, 2015
1 parent f40ccec commit 6854d63
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion akvo/iati/imports/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from .budget_items import budget_items
from .budget_items import budget_items, country_budget_items
from .conditions import conditions
from .contacts import contacts
from .dates import actual_end_date, actual_start_date, planned_end_date, planned_start_date
Expand All @@ -26,6 +26,7 @@
'budget_items',
'conditions',
'contacts',
'country_budget_items',
'currency',
'current_image',
'current_status',
Expand Down
60 changes: 59 additions & 1 deletion akvo/iati/imports/fields/budget_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from ..utils import get_text

from decimal import Decimal, InvalidOperation
from datetime import datetime

Expand All @@ -23,7 +25,6 @@ def budget_items(activity, project, activities_globals):
imported_budgets = []
changes = []

budgets_count = len(activity.findall('budget'))
original_budgets_count = len(activity.findall("budget[@type='1']"))
revised_budgets_count = len(activity.findall("budget[@type='2']"))

Expand Down Expand Up @@ -102,3 +103,60 @@ def budget_items(activity, project, activities_globals):
budget_item.delete()

return changes


def country_budget_items(activity, project, activities_globals):
"""
Retrieve and store the country budget items.
The country budget items will be extracted from the 'country-budget-items' element.
: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_cbis = []
changes = []

country_budget_vocabulary = ''

cbis_element = activity.find('country-budget-items')
if not cbis_element is None:
if 'vocabulary' in cbis_element.attrib.keys():
country_budget_vocabulary = cbis_element.attrib['vocabulary']

for cbi_element in cbis_element.findall('budget-item'):
code_text = ''
description_text = ''

if 'code' in cbi_element.attrib.keys():
code_text = cbi_element.attrib['code']

description_element = cbi_element.find('description')
if not description_element is None:
description_text = get_text(description_element, activities_globals['version'])

cbi, created = get_model('rsr', 'countrybudgetitem').objects.get_or_create(
project=project,
code=code_text,
description=description_text
)

if created:
changes.append(u'added country budget item (id: %s): %s' % (str(cbi.pk), cbi))

imported_cbis.append(cbi)

for cbi in project.country_budget_items.all():
if not cbi in imported_cbis:
changes.append(u'deleted country budget item (id: %s): %s' %
(str(cbi.pk),
cbi.__unicode__()))
cbi.delete()

if project.country_budget_vocabulary != country_budget_vocabulary:
project.country_budget_vocabulary = country_budget_vocabulary
project.save(update_fields=['country_budget_vocabulary'])
changes.append('country_budget_vocabulary')

return changes
1 change: 1 addition & 0 deletions akvo/iati/imports/iati_import_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'results',
'conditions',
'budget_items',
'country_budget_items',
]


Expand Down

0 comments on commit 6854d63

Please sign in to comment.