Skip to content

Commit

Permalink
[#1770] Added related projects import
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 8, 2015
1 parent dcbc2a1 commit cff75ea
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions akvo/iati/imports/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
project_plan_summary, sustainability, target_group)
from .image import current_image
from .partnerships import partnerships
from .related_projects import related_projects
from .status import status
from .titles import title, subtitle

Expand All @@ -33,6 +34,7 @@
'planned_start_date',
'project_plan',
'project_plan_summary',
'related_projects',
'scope',
'status',
'subtitle',
Expand Down
6 changes: 4 additions & 2 deletions akvo/iati/imports/fields/partnerships.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def partnerships(activity, project, activities_globals):
)
imported_partnerships.append(ps)
if created:
changes.append(ps)
changes.append(u'added partnership (id: %s): %s' % (str(ps.pk), ps))

for partnership in project.partnerships.all():
if not partnership in imported_partnerships:
changes.append(u'deleted %s' % partnership.__unicode__())
changes.append(u'deleted partnership (id: %s): %s' %
(str(partnership.pk),
partnership.__unicode__()))
partnership.delete()

return changes
56 changes: 56 additions & 0 deletions akvo/iati/imports/fields/related_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- 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 django.core.exceptions import ObjectDoesNotExist
from django.db.models import get_model


def related_projects(activity, project, activities_globals):
"""
Retrieve and store the related projects.
The related projects will be extracted from the 'related-activity' elements.
:param activity: ElementTree; contains all data for the activity
:param project: Project instance
:param activities_globals: Dictionary; contains all global activities information
:return: List; contains fields that have changed
"""
imported_related_projects = []
changes = []

for related_project in activity.findall('related-activity'):
prj_ref = related_project.attrib['ref'] if 'ref' in related_project.attrib.keys() else ''
prj_type = related_project.attrib['type'] if 'type' in related_project.attrib.keys() else ''

try:
related_project_prj = get_model('rsr', 'project').objects.get(iati_activity_id=prj_ref)
except ObjectDoesNotExist:
related_project_prj = prj_ref

if isinstance(related_project_prj, basestring):
rp, created = get_model('rsr', 'relatedproject').objects.get_or_create(
project=project,
related_iati_id=related_project_prj,
relation=prj_type
)
else:
rp, created = get_model('rsr', 'relatedproject').objects.get_or_create(
project=project,
related_project=related_project_prj,
relation=prj_type
)
imported_related_projects.append(rp)
if created:
changes.append(u'added related project (id: %s): %s' % (str(rp.pk), rp))

for related_project in project.related_projects.all():
if not related_project in imported_related_projects:
changes.append(u'deleted related project (id: %s): %s' %
(str(related_project.pk),
related_project.__unicode__()))
related_project.delete()

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 @@ -36,6 +36,7 @@
'default_flow_type',
'default_tied_status',
'partnerships',
'related_projects',
]


Expand Down

0 comments on commit cff75ea

Please sign in to comment.