Skip to content

Commit

Permalink
[#1450 #1551] Fix all other landscape issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed May 8, 2015
1 parent 77a20eb commit bb6fdbf
Show file tree
Hide file tree
Showing 27 changed files with 469 additions and 421 deletions.
9 changes: 4 additions & 5 deletions akvo/api/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def bundle_related_data_info_factory(request=None, parent_bundle=None):
parent_info = parent_bundle.related_info
depth = parent_info.depth - 1
ancestors = parent_info.ancestors[:] # Create a copy of the list
if not parent_bundle.obj.__class__ in parent_info.ancestors:
if parent_bundle.obj.__class__ not in parent_info.ancestors:
ancestors.append(parent_bundle.obj.__class__)
full = parent_info.full
return BundleRelatedDataInfo(depth, ancestors, full)
Expand Down Expand Up @@ -83,7 +83,7 @@ def dehydrate_related(self, bundle, related_resource):
return related_resource.full_dehydrate(new_bundle)

parent_info = getattr(bundle, 'related_info', False)
if parent_info and parent_info.depth > 0 and (not related_resource.instance.__class__ in parent_info.ancestors or parent_info.full):
if parent_info and parent_info.depth > 0 and (related_resource.instance.__class__ not in parent_info.ancestors or parent_info.full):
new_bundle = related_resource.build_bundle(obj=related_resource.instance, request=bundle.request)
new_bundle.related_info = bundle_related_data_info_factory(parent_bundle=bundle)
return related_resource.full_dehydrate(new_bundle)
Expand Down Expand Up @@ -126,7 +126,7 @@ def dehydrate(self, bundle, for_list=False):
the_m2ms = None

if isinstance(self.attribute, basestring):
the_m2ms = getattr(bundle.obj, self.attribute)
the_m2ms = getattr(bundle.obj, self.attribute)
elif callable(self.attribute):
the_m2ms = self.attribute(bundle)

Expand Down Expand Up @@ -203,8 +203,7 @@ def dehydrate(self, bundle, for_list=False, return_url=True, url=""):
pass
return url
else:
if (not self.instance_name in bundle.data
and hasattr(bundle.obj, self.instance_name)):
if self.instance_name not in bundle.data and hasattr(bundle.obj, self.instance_name):
file_field = getattr(bundle.obj, self.instance_name)
if file_field:
try:
Expand Down
8 changes: 4 additions & 4 deletions akvo/api/resources/partnership.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ def create_organisation(bundle, bundle_field_to_use):
if bundle_field_to_use == FIELD_IATI_ORG_ID:
kwargs[FIELD_IATI_ORG_ID] = bundle.data[FIELD_IATI_ORG_ID]
try:
logger.debug("Trying to create an org with kwargs: {kwargs}".format(kwargs=kwargs))
logger.debug("Trying to create an org")
organisation = Organisation.objects.create(**kwargs)
except Exception, e:
logger.exception('{message} Locals:\n {locals}\n\n'.format(message=e.message, locals=locals(), ))
logger.exception('%s' % e.message)
# otherwise fall back to using the reporting_org's internal ID
elif bundle_field_to_use == FIELD_INTERNAL_ORG_ID:
try:
logger.debug("Trying to create an org with kwargs: {kwargs}".format(kwargs=kwargs))
logger.debug("Trying to create an org")
organisation = Organisation.objects.create(**kwargs)
our_organisation = Organisation.objects.get(iati_org_id=bundle.data[FIELD_REPORTING_ORG])
InternalOrganisationID.objects.create(
Expand All @@ -114,7 +114,7 @@ def create_organisation(bundle, bundle_field_to_use):
identifier=bundle.data[FIELD_INTERNAL_ORG_ID],
)
except Exception, e:
logger.exception('{message} Locals:\n {locals}\n\n'.format(message=e.message, locals=locals(), ))
logger.exception('%s' % e.message)
return organisation


Expand Down
31 changes: 16 additions & 15 deletions akvo/api/resources/project_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ class Meta:
)

def dehydrate(self, bundle):
""" Revert the field names "created_at" and "last_modified_at" to the old "time" and "time_last_updated"
respectively to keep the API signature stable
"""
# TODO: remove this for v2 of API
bundle = super(ProjectUpdateResource, self).dehydrate(bundle)
bundle.data['time'] = bundle.data['created_at']
bundle.data['time_last_updated'] = bundle.data['last_modified_at']
del bundle.data['created_at']
del bundle.data['last_modified_at']
if isinstance(bundle.data['time'], bool): bundle.data['time'] = None
if isinstance(bundle.data['time_last_updated'], bool): bundle.data['time_last_updated'] = None
return bundle
""" Revert the field names "created_at" and "last_modified_at" to the old "time" and "time_last_updated"
respectively to keep the API signature stable
"""
# TODO: remove this for v2 of API
bundle = super(ProjectUpdateResource, self).dehydrate(bundle)
bundle.data['time'] = bundle.data['created_at']
bundle.data['time_last_updated'] = bundle.data['last_modified_at']
del bundle.data['created_at']
del bundle.data['last_modified_at']
if isinstance(bundle.data['time'], bool): bundle.data['time'] = None
if isinstance(bundle.data['time_last_updated'], bool): bundle.data['time_last_updated'] = None
return bundle


class ProjectUpdateResourceExtra(ProjectUpdateResource):
Expand Down Expand Up @@ -145,9 +145,10 @@ def build_schema(self):
'nullable': False,
'blank': False,
'readonly': True,
'help_text': "A custom related resource with parts of data from user and the organisation the user belongs to. "
"Includes the fields full_name, organisation and resource_uri of user and absolute_url, long_name, name and resource_uri "
"of organisation.",
'help_text': "A custom related resource with parts of data from user and the "
"organisation the user belongs to. Includes the fields full_name, "
"organisation and resource_uri of user and absolute_url, long_name, name "
"and resource_uri of organisation.",
'unique': False,
}
return data
8 changes: 4 additions & 4 deletions akvo/codelists/scripts/iati_codelist_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def codelist_to_tuples(xml_string, codelist, version):
for codelist_field in list(codelist_tree):
for codelist_field_item in codelist_field.findall('*'):
unicode_field_tag = "u'" + codelist_field_item.tag + "'"
if not unicode_field_tag in fields:
if unicode_field_tag not in fields:
fields.append(unicode_field_tag)
fields_string = ', '.join(fields)

Expand All @@ -82,7 +82,7 @@ def codelist_to_tuples(xml_string, codelist, version):
codelist_field_content = ["" for _field in fields]
codelist_tags = []
for codelist_field_item in codelist_field.findall('*'):
if not codelist_field_item.tag in codelist_tags:
if codelist_field_item.tag not in codelist_tags:
codelist_tags.append(codelist_field_item.tag)
list_index = fields.index("u'" + codelist_field_item.tag + "'")
if codelist_field_item.text:
Expand Down Expand Up @@ -141,7 +141,7 @@ def generate_code_lists(version):
result = requests.get(codelist_url % codelist)
if result.status_code == 200 and len(result.text) > 0:
print "Generating python for %s..." % codelist
if not codelist in ["IATIOrganisationIdentifier", ]:
if codelist not in ["IATIOrganisationIdentifier", ]:
# IATIOrganisationIdentifier is not a codelist
python_code.append('# From %s' % codelist_url % codelist)
python_code.append('\n')
Expand All @@ -159,7 +159,7 @@ def generate_code_lists(version):
args = parser.parse_args()

# Version has to be one of the allowed versions
if not args.version in VERSIONS.keys():
if args.version not in VERSIONS.keys():
print "Error; Version should be one of the following:"
for version in VERSIONS.keys():
print "- %s" % version
Expand Down
3 changes: 1 addition & 2 deletions akvo/cordaid_org_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def import_orgs(xml_file):
action = "failed"
internal_org_id.delete()
referenced_org.delete()
print(u"*** UNABLE TO CREATE NEW ORGANISATION! "
"Reason: {message}.".format(e.message))
print(u"*** UNABLE TO CREATE NEW ORGANISATION! ")
name = element.findtext("name")
referenced_org.name, referenced_org.long_name = name[:25], name
referenced_org.description = element.findtext("description") or "N/A"
Expand Down
119 changes: 73 additions & 46 deletions akvo/iati/elements/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,76 @@
from lxml import etree


def _provider_organisation(element, trans):
"""
Helper function for transaction()
"""
org = trans.provider_organisation
provider_org_element = etree.SubElement(element, "provider-org")

if trans.provider_organisation_activity:
provider_org_element.attrib['provider-activity-id'] = trans.provider_organisation_activity

if org.iati_org_id:
provider_org_element.attrib['ref'] = org.iati_org_id

if org.long_name:
narrative_element = etree.SubElement(provider_org_element, "narrative")
narrative_element.text = org.long_name
elif org.name:
narrative_element = etree.SubElement(provider_org_element, "narrative")
narrative_element.text = org.name

return element


def _receiver_organisation(element, trans):
"""
Helper function for transaction()
"""
org = trans.receiver_organisation
receiver_org_element = etree.SubElement(element, "receiver-org")

if trans.receiver_organisation_activity:
receiver_org_element.attrib['receiver-activity-id'] = trans.receiver_organisation_activity

if org.iati_org_id:
receiver_org_element.attrib['ref'] = org.iati_org_id

if org.long_name:
narrative_element = etree.SubElement(receiver_org_element, "narrative")
narrative_element.text = org.long_name
elif org.name:
narrative_element = etree.SubElement(receiver_org_element, "narrative")
narrative_element.text = org.name

return element


def _sector(element, sector):
"""
Helper function for transaction()
"""
if sector.code:
sector_element = etree.SubElement(element, "sector")
sector_element.attrib['code'] = sector.code

if not sector.vocabulary or sector.vocabulary == 'DAC':
sector_element.attrib['vocabulary'] = '1'
elif sector.vocabulary == 'DAC-3':
sector_element.attrib['vocabulary'] = '2'
else:
sector_element.attrib['vocabulary'] = sector.vocabulary

if sector.percentage:
sector_element.attrib['percentage'] = str(sector.percentage)

if sector.text:
sector_element.text = sector.text

return element


def transaction(project):
"""
Generate the transaction elements.
Expand Down Expand Up @@ -47,60 +117,17 @@ def transaction(project):
narrative_element.text = trans.description

if trans.provider_organisation:
org = trans.provider_organisation
provider_org_element = etree.SubElement(element, "provider-org")

if trans.provider_organisation_activity:
provider_org_element.attrib['provider-activity-id'] = trans.provider_organisation_activity

if org.iati_org_id:
provider_org_element.attrib['ref'] = org.iati_org_id

if org.long_name:
narrative_element = etree.SubElement(provider_org_element, "narrative")
narrative_element.text = org.long_name
elif org.name:
narrative_element = etree.SubElement(provider_org_element, "narrative")
narrative_element.text = org.name
element = _provider_organisation(element, trans)

if trans.receiver_organisation:
org = trans.receiver_organisation
receiver_org_element = etree.SubElement(element, "receiver-org")

if trans.receiver_organisation_activity:
receiver_org_element.attrib['receiver-activity-id'] = trans.receiver_organisation_activity

if org.iati_org_id:
receiver_org_element.attrib['ref'] = org.iati_org_id

if org.long_name:
narrative_element = etree.SubElement(receiver_org_element, "narrative")
narrative_element.text = org.long_name
elif org.name:
narrative_element = etree.SubElement(receiver_org_element, "narrative")
narrative_element.text = org.name
element = _receiver_organisation(element, trans)

if trans.disbursement_channel:
disbursement_channel_element = etree.SubElement(element, "disbursement-channel")
disbursement_channel_element.attrib['code'] = trans.disbursement_channel

for sector in trans.sectors.all():
if sector.code:
sector_element = etree.SubElement(element, "sector")
sector_element.attrib['code'] = sector.code

if not sector.vocabulary or sector.vocabulary == 'DAC':
sector_element.attrib['vocabulary'] = '1'
elif sector.vocabulary == 'DAC-3':
sector_element.attrib['vocabulary'] = '2'
else:
sector_element.attrib['vocabulary'] = sector.vocabulary

if sector.percentage:
sector_element.attrib['percentage'] = str(sector.percentage)

if sector.text:
sector_element.text = sector.text
element = _sector(element, sector)

if trans.recipient_country:
recipient_country_element = etree.SubElement(element, "recipient-country")
Expand Down
Loading

0 comments on commit bb6fdbf

Please sign in to comment.