-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1770] Add partnership import, and introduce utils
- Loading branch information
1 parent
c0bc3ad
commit dcbc2a1
Showing
8 changed files
with
138 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- 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_or_create_organisation, get_text | ||
|
||
from django.db.models import get_model | ||
|
||
ROLE_TO_CODE = { | ||
'accountable': '2', | ||
'extending': '3', | ||
'funding': '1', | ||
'implementing': '4' | ||
} | ||
|
||
|
||
def partnerships(activity, project, activities_globals): | ||
""" | ||
Retrieve and store the partnerships. | ||
The partnerships will be extracted from the 'participating-org' 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_partnerships = [] | ||
changes = [] | ||
|
||
for partnership in activity.findall('participating-org'): | ||
org_ref = partnership.attrib['ref'] if 'ref' in partnership.attrib.keys() else '' | ||
org_name = get_text(partnership, activities_globals['version']) | ||
partner = get_or_create_organisation(org_ref, org_name) | ||
partner_role = partnership.attrib['role'] if 'role' in partnership.attrib.keys() else None | ||
if activities_globals['version'][0] == '1': | ||
partner_role = ROLE_TO_CODE[partner_role.lower()] | ||
ps, created = get_model('rsr', 'partnership').objects.get_or_create( | ||
project=project, | ||
organisation=partner, | ||
iati_organisation_role=int(partner_role) if partner_role else None | ||
) | ||
imported_partnerships.append(ps) | ||
if created: | ||
changes.append(ps) | ||
|
||
for partnership in project.partnerships.all(): | ||
if not partnership in imported_partnerships: | ||
changes.append(u'deleted %s' % partnership.__unicode__()) | ||
partnership.delete() | ||
|
||
return changes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
'default_finance_type', | ||
'default_flow_type', | ||
'default_tied_status', | ||
'partnerships', | ||
] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- 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 get_text(element, version): | ||
""" | ||
Returns the text of an element. Based on the IATI version, this is the direct text of the | ||
element (v1) or the the text in the underlying 'narrative' element (v2). | ||
:param element: ElementTree node | ||
:param version: String; the full IATI version, e.g. '1.03' or '2.01' | ||
:return: String; text of the element or an empty string in case there is no text | ||
""" | ||
if version[0] == '1': | ||
return element.text | ||
else: | ||
narrative_element = element.find('narrative') | ||
if narrative_element is not None: | ||
return narrative_element.text | ||
return '' | ||
|
||
|
||
def get_or_create_organisation(ref, name): | ||
""" | ||
Looks for an organisation in the RSR database. | ||
First the ref will be looked up in the Organisation.iati_org_id field. If this does not exist, | ||
the name will be looked up in the Organisation.name and Organisation.long_name fields. | ||
If none of these return a match, a new organisation will be created. | ||
:param ref: String; the reference of the organisation that is specified in the IATI file. | ||
:param name: String; the name of the organisation that is specified in the IATI file. | ||
:return: Organisation instance | ||
""" | ||
if ref: | ||
try: | ||
return get_model('rsr', 'organisation').objects.get(iati_org_id=ref) | ||
except ObjectDoesNotExist: | ||
pass | ||
|
||
if name: | ||
try: | ||
return get_model('rsr', 'organisation').objects.get(name=name[:25]) | ||
except ObjectDoesNotExist: | ||
try: | ||
return get_model('rsr', 'organisation').objects.get(long_name=name[:75]) | ||
except ObjectDoesNotExist: | ||
pass | ||
|
||
return get_model('rsr', 'organisation').objects.create( | ||
name=name[:25], | ||
long_name=name[:75], | ||
iati_org_id=ref, | ||
organisation_type='N' | ||
) |