Skip to content

Commit

Permalink
[#1770] Add project defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 7, 2015
1 parent 81a4ab4 commit f714d2e
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 33 deletions.
10 changes: 8 additions & 2 deletions akvo/iati/imports/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from .dates import actual_end_date, actual_start_date, planned_end_date, planned_start_date
from .defaults import currency
from .defaults import (currency, default_aid_type, default_finance_type, default_flow_type,
default_tied_status, hierarchy, language, scope)
from .descriptions import (background, current_status, goals_overview, project_plan,
project_plan_summary, sustainability, target_group)
from .image import current_image
from .language import language
from .status import status
from .titles import title, subtitle

Expand All @@ -20,12 +20,18 @@
'currency',
'current_image',
'current_status',
'default_aid_type',
'default_finance_type',
'default_flow_type',
'default_tied_status',
'goals_overview',
'hierarchy',
'language',
'planned_end_date',
'planned_start_date',
'project_plan',
'project_plan_summary',
'scope',
'status',
'subtitle',
'sustainability',
Expand Down
172 changes: 171 additions & 1 deletion akvo/iati/imports/fields/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


def language(activity, project, activities_globals):
"""
Retrieve and store the language.
The language will be extracted from the 'lang' attribute of the activity root element.
: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
"""
xml_ns = 'http://www.w3.org/XML/1998/namespace'

if '{%s}lang' % xml_ns in activity.attrib.keys():
default_language_value = activity.attrib['{%s}lang' % xml_ns].lower()
if project.language != default_language_value:
project.language = default_language_value
project.save(update_fields=['language'])
return ['language']

return []


def currency(activity, project, activities_globals):
"""
Retrieve and store the currency.
The title will be extracted from the 'default-currency' attribute of the activity root element.
The currency will be extracted from the 'default-currency' attribute of the activity root element.
:param activity: ElementTree; contains all data for the activity
:param project: Project instance
Expand All @@ -22,3 +44,151 @@ def currency(activity, project, activities_globals):
project.save(update_fields=['currency'])
return ['currency']
return []


def hierarchy(activity, project, activities_globals):
"""
Retrieve and store the hierarchy.
The hierarchy will be extracted from the 'hierarchy' attribute of the activity root element.
: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
"""
if 'hierarchy' in activity.attrib.keys():
hierarchy_value = int(activity.attrib['hierarchy'])
if project.hierarchy != hierarchy_value:
project.hierarchy = hierarchy_value
project.save(update_fields=['hierarchy'])
return ['hierarchy']
return []


def scope(activity, project, activities_globals):
"""
Retrieve and store the activity scope.
The scope will be extracted from the 'code' attribute of the 'activity-scope' element.
: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
"""
scope_element = activity.find("activity-scope")

if not scope_element is None and 'code' in scope_element.attrib.keys():
scope_value = scope_element['code']
if project.project_scope != scope_value:
project.project_scope = scope_value
project.save(update_fields=['project_scope'])
return ['project_scope']
return []


def collaboration_type(activity, project, activities_globals):
"""
Retrieve and store the collaboration type.
The collaboration type will be extracted from the 'code' attribute of the
'collaboration-type' element.
: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
"""
ct_element = activity.find("collaboration-type")

if not ct_element is None and 'code' in ct_element.attrib.keys():
ct_value = ct_element['code']
if project.collaboration_type != ct_value:
project.collaboration_type = ct_value
project.save(update_fields=['collaboration_type'])
return ['collaboration_type']
return []


def default_flow_type(activity, project, activities_globals):
"""
Retrieve and store the default flow type.
The flow type will be extracted from the 'code' attribute of the 'default-flow-type' element.
: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
"""
dft_element = activity.find("default-flow-type")

if not dft_element is None and 'code' in dft_element.attrib.keys():
dft_value = dft_element['code']
if project.default_flow_type != dft_value:
project.default_flow_type = dft_value
project.save(update_fields=['default_flow_type'])
return ['default_flow_type']
return []


def default_finance_type(activity, project, activities_globals):
"""
Retrieve and store the default finance type.
The finance type will be extracted from the 'code' attribute of the 'default-finance-type'
element.
: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
"""
dft_element = activity.find("default-finance-type")

if not dft_element is None and 'code' in dft_element.attrib.keys():
dft_value = dft_element['code']
if project.default_finance_type != dft_value:
project.default_finance_type = dft_value
project.save(update_fields=['default_finance_type'])
return ['default_finance_type']
return []


def default_aid_type(activity, project, activities_globals):
"""
Retrieve and store the default aid type.
The aid type will be extracted from the 'code' attribute of the 'default-aid-type' element.
: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
"""
dat_element = activity.find("default-aid-type")

if not dat_element is None and 'code' in dat_element.attrib.keys():
dat_value = dat_element['code']
if project.default_aid_type != dat_value:
project.default_aid_type = dat_value
project.save(update_fields=['default_aid_type'])
return ['default_aid_type']
return []


def default_tied_status(activity, project, activities_globals):
"""
Retrieve and store the default tied status.
The tied status will be extracted from the 'code' attribute of the 'default-tied-status'
element.
: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
"""
dts_element = activity.find("default-tied-status")

if not dts_element is None and 'code' in dts_element.attrib.keys():
dts_value = dts_element['code']
if project.default_tied_status != dts_value:
project.default_tied_status = dts_value
project.save(update_fields=['default_tied_status'])
return ['default_tied_status']
return []
27 changes: 0 additions & 27 deletions akvo/iati/imports/fields/language.py

This file was deleted.

12 changes: 9 additions & 3 deletions akvo/iati/imports/iati_import_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
'target_group',
'project_plan',
'sustainability',
'current_image',
'language',
'currency',
'planned_start_date',
'actual_start_date',
'planned_end_date',
'actual_end_date',
'current_image',
'language',
'currency',
'hierarchy',
'scope',
'default_aid_type',
'default_finance_type',
'default_flow_type',
'default_tied_status',
]


Expand Down

0 comments on commit f714d2e

Please sign in to comment.