Skip to content

Commit

Permalink
[#1770] Title, subtitle and default language
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Sep 3, 2015
1 parent ffa1b28 commit 1f9d94d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 46 deletions.
7 changes: 5 additions & 2 deletions akvo/iati/imports/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# 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 .title import title
from .defaults import default_language
from .titles import title, subtitle

__all__ = [
'default_language'
'title',
]
'subtitle',
]
28 changes: 28 additions & 0 deletions akvo/iati/imports/fields/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- 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 >.




def default_language(activity, project, activities_globals):
"""
Retrieve and store the default language from the XML.
: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 []
33 changes: 0 additions & 33 deletions akvo/iati/imports/fields/title.py

This file was deleted.

71 changes: 71 additions & 0 deletions akvo/iati/imports/fields/titles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- 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.conf import settings


def title(activity, project, activities_globals):
"""
Retrieve and store the title.
The title will be extracted from the 'title' 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
"""
title_text = None

if activities_globals['version'][0] == '1':
title_element = activity.find('title')
if title_element is not None:
title_text = title_element.text[:45]
else:
title_element = activity.find('title')
if title_element is not None:
narrative_element = title_element.find('narrative')
if narrative_element is not None:
title_text = narrative_element.text[:45]

if title_text is not None and project.title != title_text:
project.title = title_text
project.save(update_fields=['title'])
return ['title']

return []


def subtitle(activity, project, activities_globals):
"""
Retrieve and store the subtitle.
In case the Akvo NS is used, the subtitle will be extracted from a 'description' element
with akvo type 4. Without an Akvo NS, we use the first 'description' 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
"""
subtitle_text = None
subtitle_element = activity.find("description[@{%s}type='4']" % settings.AKVO_NS)

if subtitle_element is None:
subtitle_element = activity.find('description')

if not subtitle_element is None:
if activities_globals['version'][0] == '1':
subtitle_text = subtitle_element.text[:75]
else:
narrative_element = subtitle_element.find('narrative')
if narrative_element is not None:
subtitle_text = narrative_element.text[:75]

if subtitle_text is not None and project.subtitle != subtitle_text:
project.subtitle = subtitle_text
project.save(update_fields=['subtitle'])
return ['subtitle']

return []
20 changes: 10 additions & 10 deletions akvo/iati/imports/iati_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@
import datetime
import urllib2

SUPPORTED_VERSIONS = [
'1.01',
'1.02',
'1.03',
'1.04',
'1.05',
'2.01',
]


class IatiImportProcess(object):
def set_start_date(self):
Expand Down Expand Up @@ -56,7 +47,7 @@ def check_version(self):
Check if the version of the IATI XML file is specified and supported.
"""
version = self.activities.attrib['version']
if not version in SUPPORTED_VERSIONS:
if not version in self.SUPPORTED_VERSIONS:
raise DataError(u'Version %s not supported.' % version)

def get_activities(self):
Expand Down Expand Up @@ -129,6 +120,15 @@ def __init__(self, iati_import):
:param iati_import: IatiImport instance
"""
self.SUPPORTED_VERSIONS = [
'1.01',
'1.02',
'1.03',
'1.04',
'1.05',
'2.01',
]

self.iati_import = iati_import
self.organisation = iati_import.reporting_organisation
self.user = iati_import.user
Expand Down
4 changes: 3 additions & 1 deletion akvo/iati/imports/iati_import_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import fields

FIELDS = [
'default_language',
'title',
'subtitle',
]


Expand Down Expand Up @@ -79,7 +81,7 @@ def set_sync_owner(self):
self.set_status(4)
self.set_errors_true()
raise ProjectException({
'message': u'Project has a different sync_owner: %s' % sync_owner.name,
'message': u'Project has a different sync_owner: %s.' % sync_owner.name,
'project': self.project
})
self.project.sync_owner = self.organisation
Expand Down
1 change: 1 addition & 0 deletions akvo/settings/30-rsr.conf
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ REQUIRED_AUTH_GROUPS = [

# IATI settings
IATI_VERSION = '2.01'
AKVO_NS = 'http://akvo.org/iati-activities'


# Notify when project is published
Expand Down

0 comments on commit 1f9d94d

Please sign in to comment.