Skip to content

Commit

Permalink
[#1770] Get or create project based on IATI activity ID
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 2, 2015
1 parent ac15ddb commit eb0e42c
Show file tree
Hide file tree
Showing 43 changed files with 1,864 additions and 4 deletions.
86 changes: 86 additions & 0 deletions akvo/iati/exports/elements/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- 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 .activity_date import activity_date
from .activity_scope import activity_scope
from .activity_status import activity_status
from .background import background
from .budget import budget
from .capital_spend import capital_spend
from .collaboration_type import collaboration_type
from .contact_info import contact_info
from .country_budget_items import country_budget_items
from .conditions import conditions
from .crs_add import crs_add
from .current_situation import current_situation
from .default_aid_type import default_aid_type
from .default_finance_type import default_finance_type
from .default_flow_type import default_flow_type
from .default_tied_status import default_tied_status
from .document_link import document_link
from .fss import fss
from .goals_overview import goals_overview
from .iati_identifier import iati_identifier
from .legacy_data import legacy_data
from .location import location
from .other_identifier import other_identifier
from .participating_org import participating_org
from .planned_disbursement import planned_disbursement
from .policy_marker import policy_marker
from .project_plan import project_plan
from .recipient_country import recipient_country
from .recipient_region import recipient_region
from .related_activity import related_activity
from .reporting_org import reporting_org
from .result import result
from .sector import sector
from .subtitle import subtitle
from .sustainability import sustainability
from .summary import summary
from .target_group import target_group
from .title import title
from .transaction import transaction

__all__ = [
'activity_date',
'activity_scope',
'activity_status',
'background',
'budget',
'capital_spend',
'collaboration_type',
'conditions',
'contact_info',
'country_budget_items',
'crs_add',
'current_situation',
'default_aid_type',
'default_finance_type',
'default_flow_type',
'default_tied_status',
'document_link',
'fss',
'goals_overview',
'iati_identifier',
'legacy_data',
'location',
'participating_org',
'planned_disbursement',
'policy_marker',
'project_plan',
'recipient_country',
'recipient_region',
'related_activity',
'reporting_org',
'result',
'sector',
'subtitle',
'sustainability',
'summary',
'target_group',
'title',
'transaction',
]
43 changes: 43 additions & 0 deletions akvo/iati/exports/elements/activity_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- 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 lxml import etree


def activity_date(project):
"""
Generate the activity-date elements.
:param project: Project object
:return: A list of Etree elements
"""
activity_date_elements = []

if project.date_start_planned:
date_start_planned_element = etree.Element("activity-date")
date_start_planned_element.attrib['iso-date'] = str(project.date_start_planned)
date_start_planned_element.attrib['type'] = '1'
activity_date_elements.append(date_start_planned_element)

if project.date_start_actual:
date_start_actual_element = etree.Element("activity-date")
date_start_actual_element.attrib['iso-date'] = str(project.date_start_actual)
date_start_actual_element.attrib['type'] = '2'
activity_date_elements.append(date_start_actual_element)

if project.date_end_planned:
date_end_planned_element = etree.Element("activity-date")
date_end_planned_element.attrib['iso-date'] = str(project.date_end_planned)
date_end_planned_element.attrib['type'] = '3'
activity_date_elements.append(date_end_planned_element)

if project.date_end_actual:
date_end_actual_element = etree.Element("activity-date")
date_end_actual_element.attrib['iso-date'] = str(project.date_end_actual)
date_end_actual_element.attrib['type'] = '4'
activity_date_elements.append(date_end_actual_element)

return activity_date_elements
22 changes: 22 additions & 0 deletions akvo/iati/exports/elements/activity_scope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- 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 lxml import etree


def activity_scope(project):
"""
Generate the activity-scope element.
:param project: Project object
:return: A list of Etree elements
"""
if project.project_scope:
element = etree.Element("activity-scope")
element.attrib['code'] = project.project_scope
return [element]

return []
31 changes: 31 additions & 0 deletions akvo/iati/exports/elements/activity_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- 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 lxml import etree

STATUS_TO_CODE = {
'N': '6',
'H': '1',
'A': '2',
'C': '3',
'L': '5',
'R': '6',
}


def activity_status(project):
"""
Generate the activity-status element.
:param project: Project object
:return: A list of Etree elements
"""
if project.status in STATUS_TO_CODE.keys():
element = etree.Element("activity-status")
element.attrib['code'] = STATUS_TO_CODE[project.status]
return [element]

return []
26 changes: 26 additions & 0 deletions akvo/iati/exports/elements/background.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- 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 lxml import etree


def background(project):
"""
Generate the background element, a description element with type "1" and akvo type "6".
:param project: Project object
:return: A list of Etree elements
"""
if project.background:
element = etree.Element("description")
element.attrib['type'] = '1'
element.attrib['{http://akvo.org/iati-activities}type'] = '6'

narrative_element = etree.SubElement(element, "narrative")
narrative_element.text = project.background
return [element]

return []
48 changes: 48 additions & 0 deletions akvo/iati/exports/elements/budget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- 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 lxml import etree


def budget(project):
"""
Generate the budget elements.
:param project: Project object
:return: A list of Etree elements
"""
budget_elements = []

for budget_item in project.budget_items.all():
if budget_item.amount and budget_item.period_start and budget_item.period_end:
element = etree.Element("budget")

if budget_item.type:
element.attrib['type'] = budget_item.type

period_start_element = etree.SubElement(element, "period-start")
period_start_element.attrib['iso-date'] = str(budget_item.period_start)

period_end_element = etree.SubElement(element, "period-end")
period_end_element.attrib['iso-date'] = str(budget_item.period_end)

value_element = etree.SubElement(element, "value")
value_element.text = str(budget_item.amount)

if budget_item.value_date:
value_element.attrib['value-date'] = str(budget_item.value_date)

if budget_item.currency:
value_element.attrib['currency'] = budget_item.currency

if budget_item.other_extra:
value_element.attrib['{http://akvo.org/iati-activities}label'] = budget_item.other_extra
elif budget_item.label.label:
value_element.attrib['{http://akvo.org/iati-activities}label'] = budget_item.label.label

budget_elements.append(element)

return budget_elements
22 changes: 22 additions & 0 deletions akvo/iati/exports/elements/capital_spend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- 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 lxml import etree


def capital_spend(project):
"""
Generate the capital-spend element.
:param project: Project object
:return: A list of Etree elements
"""
if project.capital_spend_percentage:
element = etree.Element("capital-spend")
element.attrib['percentage'] = str(project.capital_spend_percentage)
return [element]

return []
22 changes: 22 additions & 0 deletions akvo/iati/exports/elements/collaboration_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- 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 lxml import etree


def collaboration_type(project):
"""
Generate the collaboration-type element.
:param project: Project object
:return: A list of Etree elements
"""
if project.collaboration_type:
element = etree.Element("collaboration-type")
element.attrib['code'] = project.collaboration_type
return [element]

return []
35 changes: 35 additions & 0 deletions akvo/iati/exports/elements/conditions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- 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 lxml import etree


def conditions(project):
"""
Generate the conditions element.
:param project: Project object
:return: A list of Etree elements
"""
conditions_element = etree.Element("conditions")

if project.conditions.all():
conditions_element.attrib['attached'] = '1'
else:
conditions_element.attrib['attached'] = '0'

for condition in project.conditions.all():
if condition.type:
condition_element = etree.SubElement(conditions_element, "condition")
condition_element.attrib['type'] = condition.type

if condition.text:
narrative_element = etree.SubElement(condition_element, "narrative")
narrative_element.text = condition.text

conditions_element.append(condition_element)

return [conditions_element]
Loading

0 comments on commit eb0e42c

Please sign in to comment.