diff --git a/akvo/iati/imports/fields/__init__.py b/akvo/iati/imports/fields/__init__.py index f8f6a300cf..10ee89b22c 100644 --- a/akvo/iati/imports/fields/__init__.py +++ b/akvo/iati/imports/fields/__init__.py @@ -4,6 +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 .conditions import conditions from .contacts import contacts from .dates import actual_end_date, actual_start_date, planned_end_date, planned_start_date from .defaults import (currency, default_aid_type, default_finance_type, default_flow_type, @@ -21,6 +22,7 @@ 'actual_end_date', 'actual_start_date', 'background', + 'conditions', 'contacts', 'currency', 'current_image', diff --git a/akvo/iati/imports/fields/conditions.py b/akvo/iati/imports/fields/conditions.py new file mode 100644 index 0000000000..bb0f004df9 --- /dev/null +++ b/akvo/iati/imports/fields/conditions.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +# Akvo RSR is covered by the GNU Affero General Public License. +# 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 django.db.models import get_model + + +def conditions(activity, project, activities_globals): + """ + Retrieve and store the conditions. + The conditions will be extracted from the 'conditions' elements in the 'conditions' 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_conditions = [] + changes = [] + + conditions_element = activity.find('conditions') + + if not conditions_element is None and 'attached' in conditions_element.attrib.keys() and \ + conditions_element.attrib['attached'] == '1': + for condition in conditions_element.findall('condition'): + condition_type = condition.attrib['type'] if 'type' in condition.attrib.keys() else '' + condition_text = get_text(condition, activities_globals['version']) + + cond, created = get_model('rsr', 'projectcondition').objects.get_or_create( + project=project, + type=condition_type, + text=condition_text + ) + imported_conditions.append(cond) + if created: + changes.append(u'added condition (id: %s): %s' % (str(cond.pk), cond)) + + for condition in project.conditions.all(): + if not condition in imported_conditions: + changes.append(u'deleted condition (id: %s): %s' % + (str(condition.pk), + condition.__unicode__())) + condition.delete() + + return changes diff --git a/akvo/iati/imports/iati_import_activity.py b/akvo/iati/imports/iati_import_activity.py index dfeb5feb05..302485928f 100644 --- a/akvo/iati/imports/iati_import_activity.py +++ b/akvo/iati/imports/iati_import_activity.py @@ -39,6 +39,7 @@ 'related_projects', 'contacts', 'results', + 'conditions', ]