Skip to content

Commit

Permalink
[#1770] Use model constants in IATI import process
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Oct 1, 2015
1 parent 0e6142d commit 979beb2
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 119 deletions.
6 changes: 4 additions & 2 deletions akvo/iati/imports/fields/classifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from decimal import Decimal, InvalidOperation
Expand Down Expand Up @@ -59,7 +60,7 @@ def sectors(iati_import, activity, project, activities_globals):
text = get_text(sector, activities_globals['version'])
if len(text) > 100:
add_log(iati_import, 'sector', 'description is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
text = text[:100]

if 'code' in sector.attrib.keys():
Expand Down Expand Up @@ -130,7 +131,8 @@ def policy_markers(iati_import, activity, project, activities_globals):
text = get_text(marker, activities_globals['version'])
if len(text) > 255:
add_log(iati_import, 'policy_marker',
'description is too long (255 characters allowed)', project, 3)
'description is too long (255 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
text = text[:255]

if 'code' in marker.attrib.keys():
Expand Down
18 changes: 10 additions & 8 deletions akvo/iati/imports/fields/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from django.db.models import get_model
Expand Down Expand Up @@ -45,39 +46,39 @@ def contacts(iati_import, activity, project, activities_globals):
organisation_text = get_text(organisation_element, activities_globals['version'])
if len(organisation_text) > 100:
add_log(iati_import, 'contact', 'organisation is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
organisation_text = organisation_text[:100]

department_element = contact.find('department')
if not department_element is None:
department_text = get_text(department_element, activities_globals['version'])
if len(department_text) > 100:
add_log(iati_import, 'contact', 'department is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
department_text = department_text[:100]

person_name_element = contact.find('person-name')
if not person_name_element is None:
person_name_text = get_text(person_name_element, activities_globals['version'])
if len(person_name_text) > 100:
add_log(iati_import, 'contact', 'person name is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
person_name_text = person_name_text[:100]

job_title_element = contact.find('job-title')
if not job_title_element is None:
job_title_text = get_text(job_title_element, activities_globals['version'])
if len(job_title_text) > 100:
add_log(iati_import, 'contact', 'job title is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
job_title_text = job_title_text[:100]

telephone_element = contact.find('telephone')
if not telephone_element is None and not telephone_element.text is None:
telephone_text = telephone_element.text
if len(telephone_text) > 30:
add_log(iati_import, 'contact', 'telephone is too long (30 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
telephone_text = telephone_text[:30]

email_element = contact.find('email')
Expand All @@ -91,10 +92,11 @@ def contacts(iati_import, activity, project, activities_globals):
mail_addr_element = contact.find('mailing-address')
if not mail_addr_element is None:
mailing_address_text = get_text(mail_addr_element, activities_globals['version'])
if len(telephone_text) > 255:
if len(mailing_address_text) > 255:
add_log(iati_import, 'contact',
'mailing address is too long (30 characters allowed)', project, 3)
telephone_text = telephone_text[:255]
'mailing address is too long (30 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
mailing_address_text = mailing_address_text[:255]

c, created = get_model('rsr', 'projectcontact').objects.get_or_create(
project=project,
Expand Down
3 changes: 2 additions & 1 deletion akvo/iati/imports/fields/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from django.db.models import get_model
Expand Down Expand Up @@ -343,7 +344,7 @@ def conditions(iati_import, activity, project, activities_globals):
condition_text = get_text(condition, activities_globals['version'])
if len(condition_text) > 100:
add_log(iati_import, 'condition', 'condition is too long (100 character allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
condition_text = condition_text[:100]

cond, created = get_model('rsr', 'projectcondition').objects.get_or_create(
Expand Down
21 changes: 14 additions & 7 deletions akvo/iati/imports/fields/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from django.conf import settings
Expand Down Expand Up @@ -83,7 +84,8 @@ def title(iati_import, activity, project, activities_globals):
if title_element is not None:
title_text = get_text(title_element, activities_globals['version'])
if len(title_text) > 45:
add_log(iati_import, 'title', 'title is too long (45 characters allowed)', project, 3)
add_log(iati_import, 'title', 'title is too long (45 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'title', title_text, 1)
title_text = title_text[:45]
else:
Expand Down Expand Up @@ -132,7 +134,8 @@ def subtitle(iati_import, activity, project, activities_globals):
subtitle_text = get_text(subtitle_element, activities_globals['version'])

if len(subtitle_text) > 75:
add_log(iati_import, 'subtitle', 'subtitle is too long (75 characters allowed)', project, 3)
add_log(iati_import, 'subtitle', 'subtitle is too long (75 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'subtitle', subtitle_text, 1)
subtitle_text = subtitle_text[:75]
else:
Expand Down Expand Up @@ -184,7 +187,7 @@ def project_plan_summary(iati_import, activity, project, activities_globals):

if len(pps_text) > 400:
add_log(iati_import, 'project_plan_summary', 'summary is too long (400 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'project_plan_summary', pps_text, 4)
pps_text = pps_text[:400]
else:
Expand Down Expand Up @@ -226,7 +229,8 @@ def goals_overview(iati_import, activity, project, activities_globals):

if len(go_text) > 600:
add_log(iati_import, 'goals_overview',
'goals overview is too long (600 characters allowed)', project, 3)
'goals overview is too long (600 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'goals_overview', go_text, 4)
go_text = go_text[:600]
else:
Expand Down Expand Up @@ -272,7 +276,8 @@ def background(iati_import, activity, project, activities_globals):
background_text = get_text(background_element, activities_globals['version'])
if len(background_text) > 1000:
add_log(iati_import, 'background',
'background is too long (1000 characters allowed)', project, 3)
'background is too long (1000 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'background', background_text, 4)
background_text = background_text[:1000]
else:
Expand Down Expand Up @@ -318,7 +323,8 @@ def current_status(iati_import, activity, project, activities_globals):
current_status_text = get_text(current_status_element, activities_globals['version'])
if len(current_status_text) > 600:
add_log(iati_import, 'current_status',
'current status is too long (600 characters allowed)', project, 3)
'current status is too long (600 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'current_status', current_status_text, 4)
current_status_text = current_status_text[:600]
else:
Expand Down Expand Up @@ -354,7 +360,8 @@ def target_group(iati_import, activity, project, activities_globals):
target_group_text = get_text(target_group_element, activities_globals['version'])
if len(target_group_text) > 600:
add_log(iati_import, 'target_group',
'target group is too long (600 characters allowed)', project, 3)
'target group is too long (600 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
_add_custom_field(project, 'target_group', target_group_text, 4)
target_group_text = target_group_text[:600]
else:
Expand Down
16 changes: 11 additions & 5 deletions akvo/iati/imports/fields/financials.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_or_create_organisation, get_text

from decimal import Decimal, InvalidOperation
Expand Down Expand Up @@ -117,7 +118,8 @@ def transactions(iati_import, activity, project, activities_globals):
transaction_description = get_text(description_element, activities_globals['version'])
if len(transaction_description) > 255:
add_log(iati_import, 'transaction_description',
'description too long (255 characters allowed)', project, 3)
'description too long (255 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
transaction_description = transaction_description[:255]

prov_org_element = transaction.find('provider-org')
Expand Down Expand Up @@ -294,7 +296,8 @@ def transaction_sectors(iati_import, transaction_element, transaction, activitie
sector_description = get_text(sector, activities_globals['version'])
if len(sector_description) > 100:
add_log(iati_import, 'transaction_sector_description',
'description too long (100 characters allowed)', transaction.project, 3)
'description too long (100 characters allowed)', transaction.project,
IatiImportLog.VALUE_PARTLY_SAVED)
sector_description = sector_description[:100]

sec, created = get_model('rsr', 'transactionsector').objects.get_or_create(
Expand Down Expand Up @@ -365,13 +368,15 @@ def budget_items(iati_import, activity, project, activities_globals):
pk=int(budget.attrib['{%s}type' % settings.AKVO_NS])
)
except (ValueError, ObjectDoesNotExist) as e:
add_log(iati_import, 'budget_item_label', str(e), project, 3)
add_log(iati_import, 'budget_item_label', str(e), project,
IatiImportLog.VALUE_PARTLY_SAVED)

if '{%s}label' % settings.AKVO_NS in budget.attrib.keys():
other_extra = budget.attrib['{%s}label' % settings.AKVO_NS]
if len(other_extra) > 30:
add_log(iati_import, 'budget_item_label',
'label too long (30 characters allowed)', project, 3)
'label too long (30 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
other_extra = other_extra[:30]

period_start_element = budget.find('period-start')
Expand Down Expand Up @@ -487,7 +492,8 @@ def country_budget_items(iati_import, activity, project, activities_globals):
description_text = get_text(description_element, activities_globals['version'])
if len(description_text) > 100:
add_log(iati_import, 'country_budget_item_description',
'description is too long (100 characters allowed)', project, 3)
'description is too long (100 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
description_text = description_text[:100]

try:
Expand Down
12 changes: 8 additions & 4 deletions akvo/iati/imports/fields/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from django.conf import settings
Expand Down Expand Up @@ -69,7 +70,8 @@ def current_image(iati_import, activity, project, activities_globals):
image_caption = get_text(title_element, activities_globals['version'])
if len(image_caption) > 50:
add_log(iati_import, 'image_caption',
'caption too long (50 characters allowed)', project, 3)
'caption too long (50 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
image_caption = image_caption[:50]

if project.current_image_caption != image_caption:
Expand All @@ -86,7 +88,8 @@ def current_image(iati_import, activity, project, activities_globals):
]
if len(image_credit) > 50:
add_log(iati_import, 'image_credit',
'credit too long (50 characters allowed)', project, 3)
'credit too long (50 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
image_credit = image_credit[:50]

if project.current_image_credit != image_credit:
Expand Down Expand Up @@ -147,7 +150,7 @@ def links(iati_import, activity, project, activities_globals):
caption = get_text(title_element, activities_globals['version'])
if len(caption) > 50:
add_log(iati_import, 'link_caption', 'caption is too long (50 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
caption = caption[:50]

link, created = get_model('rsr', 'link').objects.get_or_create(
Expand Down Expand Up @@ -222,7 +225,8 @@ def documents(iati_import, activity, project, activities_globals):
title = get_text(title_element, activities_globals['version'])
if len(title) > 100:
add_log(iati_import, 'document_link_title',
'title is too long (100 characters allowed)', project, 3)
'title is too long (100 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
title = title[:100]

if activities_globals['version'][0] == '1' and \
Expand Down
18 changes: 12 additions & 6 deletions akvo/iati/imports/fields/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# 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 ....rsr.models.iati_import_log import IatiImportLog
from ..utils import add_log, get_text

from decimal import Decimal, InvalidOperation
Expand Down Expand Up @@ -77,23 +78,25 @@ def locations(iati_import, activity, project, activities_globals):
name = get_text(name_element, activities_globals['version'])
if len(name) > 100:
add_log(iati_import, 'location_name', 'name is too long (100 characters allowed)',
project, 3)
project, IatiImportLog.VALUE_PARTLY_SAVED)
name = name[:100]

description_element = location.find('description')
if not description_element is None:
description = get_text(description_element, activities_globals['version'])
if len(description) > 255:
add_log(iati_import, 'location_decription',
'description is too long (255 characters allowed)', project, 3)
'description is too long (255 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
description = description[:255]

act_description_element = location.find('activity-description')
if not act_description_element is None:
activity_description = get_text(act_description_element, activities_globals['version'])
if len(activity_description) > 255:
add_log(iati_import, 'location_activity_decription',
'description is too long (255 characters allowed)', project, 3)
'description is too long (255 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
activity_description = activity_description[:255]

try:
Expand Down Expand Up @@ -287,15 +290,17 @@ def recipient_countries(iati_import, activity, project, activities_globals):
text = get_text(country, activities_globals['version'])
if len(text) > 50:
add_log(iati_import, 'recipient_country_description',
'description is too long (50 characters allowed)', project, 3)
'description is too long (50 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
text = text[:50]

if 'code' in country.attrib.keys():
if not len(country.attrib['code']) > 2:
code = country.attrib['code'].upper()
else:
add_log(iati_import, 'recipient_country_code',
'code is too long (2 characters allowed)', project, 3)
'code is too long (2 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)

try:
if 'percentage' in country.attrib.keys():
Expand Down Expand Up @@ -347,7 +352,8 @@ def recipient_regions(iati_import, activity, project, activities_globals):
text = get_text(region, activities_globals['version'])
if len(text) > 50:
add_log(iati_import, 'recipient_region_description',
'decription is too long (50 characters allowed)', project, 3)
'decription is too long (50 characters allowed)', project,
IatiImportLog.VALUE_PARTLY_SAVED)
text = text[:50]

if 'code' in region.attrib.keys():
Expand Down
Loading

0 comments on commit 979beb2

Please sign in to comment.