Esempio n. 1
0
def sector(project):
    """
    Generate the sector elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    sector_elements = []

    for sec in project.sectors.all():
        if has_data(sec, ['sector_code', 'vocabulary', 'vocabulary_uri', 'percentage', 'text', ]):
            element = etree.Element("sector")

            if sec.sector_code:
                element.attrib['code'] = sec.sector_code

            if sec.vocabulary:
                element.attrib['vocabulary'] = sec.vocabulary

            if sec.vocabulary_uri:
                element.attrib['vocabulary-uri'] = sec.vocabulary_uri

            if sec.percentage:
                element.attrib['percentage'] = str(sec.percentage)

            if sec.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = sec.text

            sector_elements.append(element)

    return sector_elements
Esempio n. 2
0
def _sector(element, sector):
    """
    Helper function for transaction()
    """
    if has_data(sector, [
            'code',
            'vocabulary',
            'vocabulary_uri',
            'text',
    ]):
        sector_element = etree.SubElement(element, "sector")

        if sector.code:
            sector_element.attrib['code'] = sector.code

        if sector.vocabulary:
            sector_element.attrib['vocabulary'] = sector.vocabulary

        if sector.vocabulary_uri:
            sector_element.attrib['vocabulary-uri'] = sector.vocabulary_uri

        if sector.text:
            narrative_element = etree.SubElement(sector_element, "narrative")
            narrative_element.text = sector.text

    return element
Esempio n. 3
0
def add_target_element(is_dgis_project, period, period_element):
    if (is_dgis_project or has_data(period, ['target_value', 'target_comment', ]) or
            has_qs_data(period, ['target_locations', 'target_dimensions', ])):
        target_element = etree.SubElement(period_element, "target")

        if period.target_value:
            target_element.attrib['value'] = period.target_value
        elif is_dgis_project:
            target_element.attrib['value'] = NOT_AVAILABLE

        for target_location in period.target_locations.all():
            target_location_element = etree.SubElement(target_element,
                                                       "location")
            target_location_element.attrib['ref'] = target_location.location

        for target_dimension in period.target_dimensions.all():
            if target_dimension.name or target_dimension.value:
                target_dimension_element = etree.SubElement(target_element,
                                                            "dimension")

                if target_dimension.name:
                    target_dimension_element.attrib['name'] = target_dimension.name

                if target_dimension.value:
                    target_dimension_element.attrib['value'] = target_dimension.value

        if period.target_comment:
            comment_element = etree.SubElement(target_element, "comment")
            narrative_element = etree.SubElement(comment_element,
                                                 "narrative")
            narrative_element.text = period.target_comment
Esempio n. 4
0
def recipient_region(project):
    """
    Generate the recipient-region element.

    :param project: Project object
    :return: A list of Etree elements
    """
    recipient_region_elements = []

    for region in project.recipient_regions.all():
        if has_data(region, ['region', 'percentage', 'region_vocabulary', 'region_vocabulary_uri',
                             'text', ]):
            element = etree.Element("recipient-region")

            if region.region:
                element.attrib['code'] = region.region

            if region.percentage:
                element.attrib['percentage'] = str(region.percentage)

            if region.region_vocabulary:
                element.attrib['vocabulary'] = str(region.region_vocabulary)

            if region.region_vocabulary_uri:
                element.attrib['vocabulary-uri'] = region.region_vocabulary_uri

            if region.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = region.text

            recipient_region_elements.append(element)

    return recipient_region_elements
Esempio n. 5
0
def policy_marker(project):
    """
    Generate the policy-marker element.

    :param project: Project object
    :return: A list of Etree elements
    """
    policy_marker_elements = []

    for policy in project.policy_markers.all():
        if has_data(policy, ['policy_marker', 'significance', 'vocabulary', 'vocabulary_uri',
                             'description', ]):
            element = etree.Element("policy-marker")

            if policy.policy_marker:
                element.attrib['code'] = policy.policy_marker

            if policy.significance:
                element.attrib['significance'] = policy.significance

            if policy.vocabulary:
                element.attrib['vocabulary'] = policy.vocabulary

            if policy.vocabulary_uri:
                element.attrib['vocabulary-uri'] = policy.vocabulary_uri

            if policy.description:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = policy.description

            policy_marker_elements.append(element)

    return policy_marker_elements
Esempio n. 6
0
def humanitarian_scope(project):
    """
    Generate the humanitarian-scope element.

    :param project: Project object
    :return: A list of Etree elements
    """
    humanitarian_scope_elements = []

    for scope in project.humanitarian_scopes.all():
        if has_data(scope, ['code', 'type', 'vocabulary', 'vocabulary_uri', 'text', ]):
            element = etree.Element("humanitarian-scope")

            if scope.code:
                element.attrib['code'] = scope.code

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

            if scope.vocabulary:
                element.attrib['vocabulary'] = scope.vocabulary

            if scope.vocabulary_uri:
                element.attrib['vocabulary-uri'] = scope.vocabulary_uri

            if scope.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = scope.text

            humanitarian_scope_elements.append(element)

    return humanitarian_scope_elements
Esempio n. 7
0
def add_actual_element(is_dgis_project, period, period_element):
    if (is_dgis_project or has_data(period, [
            'actual_value',
            'narrative',
            'actual_comment',
    ]) or has_qs_data(period, ['actual_locations'])):
        actual_element = etree.SubElement(period_element, "actual")

        if period.indicator.type == QUANTITATIVE:
            if period.actual_value:
                actual_element.attrib['value'] = period.actual_value
            elif is_dgis_project:
                actual_element.attrib['value'] = NOT_AVAILABLE

        else:
            if period.narrative:
                actual_element.attrib['value'] = period.narrative

        for actual_location in period.actual_locations.all():
            actual_location_element = etree.SubElement(actual_element,
                                                       "location")
            actual_location_element.attrib['ref'] = actual_location.location

        if period.actual_comment:
            comment_element = etree.SubElement(actual_element, "comment")
            narrative_element = etree.SubElement(comment_element, "narrative")
            narrative_element.text = period.actual_comment
Esempio n. 8
0
def contact_info(project):
    """
    Generate the contact-info elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    contact_info_elements = []

    for contact in project.contacts.all():
        if has_data(contact, ['type', 'organisation', 'department', 'person_name', 'job_title',
                              'telephone', 'email', 'website', 'mailing_address', ]):
            element = etree.Element("contact-info")

            if contact.type:
                element.attrib['type'] = str(contact.type)

            if contact.organisation:
                organisation_element = etree.SubElement(element, "organisation")
                narrative_element = etree.SubElement(organisation_element, "narrative")
                narrative_element.text = contact.organisation

            if contact.department:
                department_element = etree.SubElement(element, "department")
                narrative_element = etree.SubElement(department_element, "narrative")
                narrative_element.text = contact.department

            if contact.person_name:
                person_name_element = etree.SubElement(element, "person-name")
                narrative_element = etree.SubElement(person_name_element, "narrative")
                narrative_element.text = contact.person_name

            if contact.job_title:
                job_title_element = etree.SubElement(element, "job-title")
                narrative_element = etree.SubElement(job_title_element, "narrative")
                narrative_element.text = contact.job_title

            if contact.telephone:
                telephone_element = etree.SubElement(element, "telephone")
                telephone_element.text = contact.telephone

            if contact.email:
                email_element = etree.SubElement(element, "email")
                email_element.text = contact.email

            if contact.website:
                website_element = etree.SubElement(element, "website")
                website_element.text = contact.website

            if contact.mailing_address:
                mailing_address_element = etree.SubElement(element, "mailing-address")
                narrative_element = etree.SubElement(mailing_address_element, "narrative")
                narrative_element.text = contact.mailing_address

            contact_info_elements.append(element)

    return contact_info_elements
Esempio n. 9
0
def result(project):
    """
    Generate the result elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    result_elements = []

    DGIS_PROJECT = project.validations.filter(name=DGIS_VALIDATION_SET_NAME).count() == 1

    for res in project.results.all():
        if (has_data(res, ['type', 'title', 'description', ]) or
                res.aggregation_status is not None or
                res.indicators.all()):
            element = etree.Element("result")

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

            if res.aggregation_status is not None:
                element.attrib['aggregation-status'] = '1' if res.aggregation_status else '0'

            if res.title:
                title_element = etree.SubElement(element, "title")
                narrative_element = etree.SubElement(title_element, "narrative")
                narrative_element.text = res.title

            if res.description:
                description_element = etree.SubElement(element, "description")
                narrative_element = etree.SubElement(description_element, "narrative")
                narrative_element.text = res.description

            for indicator in res.indicators.all():
                if (has_data(indicator, ['measure', 'title', 'description', 'baseline_year',
                                         'baseline_value', 'baseline_comment', ]) or
                        indicator.ascending is not None or
                        has_qs_data(indicator, ['references', 'periods', ])):
                    add_indicator_element(element, indicator, DGIS_PROJECT)

            result_elements.append(element)

    return result_elements
Esempio n. 10
0
def add_reference_element(indicator_element, reference):
    if has_data(reference, ['vocabulary', 'reference', 'vocabulary_uri', ]):
        reference_element = etree.SubElement(indicator_element, "reference")

        if reference.vocabulary:
            reference_element.attrib['vocabulary'] = reference.vocabulary

        if reference.reference:
            reference_element.attrib['code'] = reference.reference

        if reference.vocabulary_uri:
            reference_element.attrib['indicator-uri'] = reference.vocabulary_uri
Esempio n. 11
0
def planned_disbursement(project):
    """
    Generate the planned-disbursement elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    planned_disbursement_elements = []

    for pd in project.planned_disbursements.all():
        if has_data(pd, [
                'value',
                'type',
                'period_start',
                'period_end',
                'value_date',
                'currency',
                'provider_organisation',
                'receiver_organisation',
        ]):
            element = etree.Element("planned-disbursement")

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

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

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

            if pd.value == 0 or pd.value:
                value_element = etree.SubElement(element, "value")
                value_element.text = str(pd.value)

            if (pd.value == 0 or pd.value) and pd.value_date:
                value_element.attrib['value-date'] = str(pd.value_date)

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

            if pd.provider_organisation:
                element = _provider_organisation(element, pd)

            if pd.receiver_organisation:
                element = _receiver_organisation(element, pd)

            planned_disbursement_elements.append(element)

    return planned_disbursement_elements
Esempio n. 12
0
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 has_data(budget_item, [
                'amount', 'period_start', 'period_end', 'type', 'status',
                'value_date', 'currency', 'other_extra', 'label'
        ]):
            element = etree.Element("budget")

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

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

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

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

            if budget_item.amount == 0 or budget_item.amount:
                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

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

            budget_elements.append(element)

    return budget_elements
Esempio n. 13
0
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 has_data(budget_item, ['amount', 'period_start', 'period_end', 'type', 'status',
                                  'value_date', 'currency', 'other_extra', 'label']):
            element = etree.Element("budget")

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

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

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

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

            if budget_item.amount == 0 or budget_item.amount:
                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

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

            budget_elements.append(element)

    return budget_elements
Esempio n. 14
0
def planned_disbursement(project):
    """
    Generate the planned-disbursement elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    planned_disbursement_elements = []

    for pd in project.planned_disbursements.all():
        if has_data(pd, ['value', 'type', 'period_start', 'period_end', 'value_date', 'currency',
                         'provider_organisation', 'receiver_organisation', ]):
            element = etree.Element("planned-disbursement")

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

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

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

            if pd.value == 0 or pd.value:
                value_element = etree.SubElement(element, "value")
                value_element.text = str(pd.value)

            if (pd.value == 0 or pd.value) and pd.value_date:
                value_element.attrib['value-date'] = str(pd.value_date)

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

            if pd.provider_organisation:
                element = _provider_organisation(element, pd)

            if pd.receiver_organisation:
                element = _receiver_organisation(element, pd)

            planned_disbursement_elements.append(element)

    return planned_disbursement_elements
Esempio n. 15
0
def add_baseline_element(is_dgis_project, indicator_element, indicator):
    if is_dgis_project or has_data(indicator,
                                   ['baseline_year', 'baseline_value', 'baseline_comment', ]):
        baseline_element = etree.SubElement(indicator_element, "baseline")

        if indicator.baseline_year:
            baseline_element.attrib['year'] = str(indicator.baseline_year)
        elif is_dgis_project:
            baseline_element.attrib['year'] = NOT_AVAILABLE_YEAR

        if indicator.baseline_value:
            baseline_element.attrib['value'] = indicator.baseline_value
        elif is_dgis_project:
            baseline_element.attrib['value'] = NOT_AVAILABLE

        if indicator.baseline_comment:
            comment_element = etree.SubElement(baseline_element, "comment")
            narrative_element = etree.SubElement(comment_element, "narrative")
            narrative_element.text = indicator.baseline_comment
Esempio n. 16
0
def add_period_element(is_dgis_project, indicator_element, period):
    if (has_data(period, ['period_start', 'period_end', 'target_value', 'target_comment',
                          'actual_value', 'narrative', 'actual_comment']) or
            has_qs_data(period, ['target_locations', 'target_dimensions', 'actual_locations',
                                 'actual_dimensions', ])):
        period_element = etree.SubElement(indicator_element, "period")

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

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

        add_target_element(is_dgis_project, period, period_element)

        add_actual_element(is_dgis_project, period, period_element)
Esempio n. 17
0
def _sector(element, sector):
    """
    Helper function for transaction()
    """
    if has_data(sector, ['code', 'vocabulary', 'vocabulary_uri', 'text', ]):
        sector_element = etree.SubElement(element, "sector")

        if sector.code:
            sector_element.attrib['code'] = sector.code

        if sector.vocabulary:
            sector_element.attrib['vocabulary'] = sector.vocabulary

        if sector.vocabulary_uri:
            sector_element.attrib['vocabulary-uri'] = sector.vocabulary_uri

        if sector.text:
            narrative_element = etree.SubElement(sector_element, "narrative")
            narrative_element.text = sector.text

    return element
Esempio n. 18
0
def sector(project):
    """
    Generate the sector elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    sector_elements = []

    for sec in project.sectors.all():
        if has_data(sec, [
                'sector_code',
                'vocabulary',
                'vocabulary_uri',
                'percentage',
                'text',
        ]):
            element = etree.Element("sector")

            if sec.sector_code:
                element.attrib['code'] = sec.sector_code

            if sec.vocabulary:
                element.attrib['vocabulary'] = sec.vocabulary

            if sec.vocabulary_uri:
                element.attrib['vocabulary-uri'] = sec.vocabulary_uri

            if sec.percentage:
                element.attrib['percentage'] = str(sec.percentage)

            if sec.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = sec.text

            sector_elements.append(element)

    return sector_elements
Esempio n. 19
0
def policy_marker(project):
    """
    Generate the policy-marker element.

    :param project: Project object
    :return: A list of Etree elements
    """
    policy_marker_elements = []

    for policy in project.policy_markers.all():
        if has_data(policy, [
                'policy_marker',
                'significance',
                'vocabulary',
                'vocabulary_uri',
                'description',
        ]):
            element = etree.Element("policy-marker")

            if policy.policy_marker:
                element.attrib['code'] = policy.policy_marker

            if policy.significance:
                element.attrib['significance'] = policy.significance

            if policy.vocabulary:
                element.attrib['vocabulary'] = policy.vocabulary

            if policy.vocabulary_uri:
                element.attrib['vocabulary-uri'] = policy.vocabulary_uri

            if policy.description:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = policy.description

            policy_marker_elements.append(element)

    return policy_marker_elements
Esempio n. 20
0
def humanitarian_scope(project):
    """
    Generate the humanitarian-scope element.

    :param project: Project object
    :return: A list of Etree elements
    """
    humanitarian_scope_elements = []

    for scope in project.humanitarian_scopes.all():
        if has_data(scope, [
                'code',
                'type',
                'vocabulary',
                'vocabulary_uri',
                'text',
        ]):
            element = etree.Element("humanitarian-scope")

            if scope.code:
                element.attrib['code'] = scope.code

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

            if scope.vocabulary:
                element.attrib['vocabulary'] = scope.vocabulary

            if scope.vocabulary_uri:
                element.attrib['vocabulary-uri'] = scope.vocabulary_uri

            if scope.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = scope.text

            humanitarian_scope_elements.append(element)

    return humanitarian_scope_elements
Esempio n. 21
0
def recipient_region(project):
    """
    Generate the recipient-region element.

    :param project: Project object
    :return: A list of Etree elements
    """
    recipient_region_elements = []

    for region in project.recipient_regions.all():
        if has_data(region, [
                'region',
                'percentage',
                'region_vocabulary',
                'region_vocabulary_uri',
                'text',
        ]):
            element = etree.Element("recipient-region")

            if region.region:
                element.attrib['code'] = region.region

            if region.percentage:
                element.attrib['percentage'] = str(region.percentage)

            if region.region_vocabulary:
                element.attrib['vocabulary'] = str(region.region_vocabulary)

            if region.region_vocabulary_uri:
                element.attrib['vocabulary-uri'] = region.region_vocabulary_uri

            if region.text:
                narrative_element = etree.SubElement(element, "narrative")
                narrative_element.text = region.text

            recipient_region_elements.append(element)

    return recipient_region_elements
Esempio n. 22
0
def add_actual_dimension_elements(is_dgis_project, period, period_element):
    if (is_dgis_project or has_data(period, [
            'actual_value',
            'narrative',
            'actual_comment',
    ]) or has_qs_data(period, ['actual_locations'])):

        qs = period.disaggregations.select_related('dimension_value',
                                                   'dimension_value__name')
        for period_disaggregation in qs.all():
            actual_element = etree.SubElement(period_element, "actual")

            if period_disaggregation.value:
                actual_element.attrib['value'] = str(
                    period_disaggregation.value)

            elif is_dgis_project:
                actual_element.attrib['value'] = NOT_AVAILABLE

            dimension_element = etree.SubElement(actual_element, 'dimension')
            dimension_element.attrib[
                'value'] = period_disaggregation.dimension_value.value
            dimension_element.attrib[
                'name'] = period_disaggregation.dimension_value.name.name
Esempio n. 23
0
def location(project):
    """
    Generate the location elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    location_elements = []

    for loc in project.locations.all():
        if (has_data(loc, [
                'reference',
                'location_reach',
                'location_code',
                'vocabulary',
                'name',
                'description',
                'activity_description',
                'exactness',
                'location_class',
                'feature_designation',
        ]) or (loc.latitude and loc.longitude)
                or loc.administratives.exists()):
            element = etree.Element("location")
            if loc.reference:
                element.attrib['ref'] = loc.reference

            if loc.location_reach:
                reach_element = etree.SubElement(element, "location-reach")
                reach_element.attrib['code'] = loc.location_reach

            if loc.location_code or loc.vocabulary:
                id_element = etree.SubElement(element, "location-id")

                if loc.vocabulary:
                    id_element.attrib['vocabulary'] = loc.vocabulary

                if loc.location_code:
                    id_element.attrib['code'] = loc.location_code

            if loc.name:
                name_element = etree.SubElement(element, "name")
                narrative_element = etree.SubElement(name_element, "narrative")
                narrative_element.text = loc.name

            if loc.description:
                description_element = etree.SubElement(element, "description")
                narrative_element = etree.SubElement(description_element,
                                                     "narrative")
                narrative_element.text = loc.description

            if loc.activity_description:
                activity_description_element = etree.SubElement(
                    element, "activity-description")
                narrative_element = etree.SubElement(
                    activity_description_element, "narrative")
                narrative_element.text = loc.activity_description

            for administrative in loc.administratives.all():
                if administrative.code or administrative.vocabulary or administrative.level:
                    administrative_element = etree.SubElement(
                        element, "administrative")

                    if administrative.vocabulary:
                        administrative_element.attrib[
                            'vocabulary'] = administrative.vocabulary

                    if administrative.code:
                        administrative_element.attrib[
                            'code'] = administrative.code

                    if administrative.level:
                        administrative_element.attrib['level'] = str(
                            administrative.level)

            if (loc.latitude == 0 or loc.latitude) and (loc.longitude == 0
                                                        or loc.longitude):
                point_element = etree.SubElement(element, "point")
                point_element.attrib[
                    'srsName'] = 'http://www.opengis.net/def/crs/EPSG/0/4326'
                pos_element = etree.SubElement(point_element, "pos")
                pos_element.text = "%s %s" % (str(
                    loc.latitude), str(loc.longitude))

            if loc.exactness:
                exactness_element = etree.SubElement(element, "exactness")
                exactness_element.attrib['code'] = str(loc.exactness)

            if loc.location_class:
                class_element = etree.SubElement(element, "location-class")
                class_element.attrib['code'] = str(loc.location_class)

            if loc.feature_designation:
                feature_element = etree.SubElement(element,
                                                   "feature-designation")
                feature_element.attrib['code'] = str(loc.feature_designation)

            location_elements.append(element)

    return location_elements
Esempio n. 24
0
def crs_add(project):
    """
    Generate the crs-add element.

    :param project: Project object
    :return: A list of Etree elements
    """
    crs = getattr(project, 'crsadd', None)

    if crs:
        element = etree.Element("crs-add")

        for flag in crs.other_flags.all():
            if flag.code or flag.significance is not None:
                other_flag_element = etree.SubElement(element, "other-flags")

                if flag.code:
                    other_flag_element.attrib['code'] = flag.code

                if flag.significance is not None:
                    other_flag_element.attrib['significance'] = '1' if flag.significance else '0'

                element.append(other_flag_element)

        if has_data(crs, ['loan_terms_rate1', 'loan_terms_rate2', 'repayment_type',
                          'repayment_plan', 'commitment_date', 'repayment_first_date',
                          'repayment_final_date', ]):
            loan_terms_element = etree.SubElement(element, "loan-terms")

            if crs.loan_terms_rate1 == 0 or crs.loan_terms_rate1:
                loan_terms_element.attrib['rate-1'] = str(crs.loan_terms_rate1)

            if crs.loan_terms_rate2 == 0 or crs.loan_terms_rate2:
                loan_terms_element.attrib['rate-2'] = str(crs.loan_terms_rate2)

            if crs.repayment_type:
                repayment_type_element = etree.SubElement(loan_terms_element, "repayment-type")
                repayment_type_element.attrib['code'] = crs.repayment_type

            if crs.repayment_plan:
                repayment_plan_element = etree.SubElement(loan_terms_element, "repayment-plan")
                repayment_plan_element.attrib['code'] = crs.repayment_plan

            if crs.commitment_date:
                commitment_date_element = etree.SubElement(loan_terms_element, "commitment-date")
                commitment_date_element.attrib['iso-date'] = str(crs.commitment_date)

            if crs.repayment_first_date:
                repayment_first_date_element = etree.SubElement(loan_terms_element, "repayment-first-date")
                repayment_first_date_element.attrib['iso-date'] = str(crs.repayment_first_date)

            if crs.repayment_final_date:
                repayment_final_date_element = etree.SubElement(loan_terms_element, "repayment-final-date")
                repayment_final_date_element.attrib['iso-date'] = str(crs.repayment_final_date)

            element.append(loan_terms_element)

            if has_data(crs, ['loan_status_year', 'loan_status_currency', 'loan_status_value_date',
                              'interest_received', 'principal_outstanding', 'principal_arrears',
                              'interest_arrears']):
                loan_status_element = etree.SubElement(element, "loan-status")

            if crs.loan_status_year:
                loan_status_element.attrib['year'] = str(crs.loan_status_year)

            if crs.loan_status_currency:
                loan_status_element.attrib['currency'] = crs.loan_status_currency

            if crs.loan_status_value_date:
                loan_status_element.attrib['value-date'] = str(crs.loan_status_value_date)

            if crs.interest_received == 0 or crs.interest_received:
                interest_received_element = etree.SubElement(loan_status_element,
                                                             "interest-received")
                interest_received_element.text = str(crs.interest_received)

            if crs.principal_outstanding == 0 or crs.principal_outstanding:
                principal_outstanding_element = etree.SubElement(loan_status_element,
                                                                 "principal-outstanding")
                principal_outstanding_element.text = str(crs.principal_outstanding)

            if crs.principal_arrears == 0 or crs.principal_arrears:
                principal_arrears_element = etree.SubElement(loan_status_element,
                                                             "principal-arrears")
                principal_arrears_element.text = str(crs.principal_arrears)

            if crs.interest_arrears == 0 or crs.interest_arrears:
                interest_arrears_element = etree.SubElement(loan_status_element, "interest-arrears")
                interest_arrears_element.text = str(crs.interest_arrears)

            element.append(loan_status_element)

        if crs.channel_code:
            channel_code_element = etree.SubElement(element, "channel-code")
            channel_code_element.text = str(crs.channel_code)

        return [element]

    else:
        return []
Esempio n. 25
0
 def test_model_instance_has_data(self):
     indicator = Indicator()
     self.assertFalse(
         has_data(indicator, ['baseline_year', 'baseline_value']))
Esempio n. 26
0
def transaction(project):
    """
    Generate the transaction elements.

    :param project: Project object
    :return: A list of Etree elements
    """

    transaction_elements = []

    for trans in project.transactions.all():
        if (has_data(trans, ['reference', 'transaction_type', 'transaction_date', 'value',
                             'currency', 'value_date', 'description', 'provider_organisation',
                             'provider_organisation_activity', 'receiver_organisation',
                             'receiver_organisation_activity', 'disbursement_channel',
                             'recipient_country', 'recipient_region', 'recipient_region_vocabulary',
                             'recipient_region_vocabulary_uri', 'flow_type', 'finance_type',
                             'aid_type', 'tied_status', ]) or
                trans.humanitarian is not None or
                trans.sectors.exists()):
            element = etree.Element("transaction")

            if trans.reference:
                element.attrib['ref'] = trans.reference

            if trans.humanitarian is not None:
                element.attrib['humanitarian'] = '1' if trans.humanitarian else '0'

            if trans.transaction_type:
                type_element = etree.SubElement(element, "transaction-type")
                type_element.attrib['code'] = trans.transaction_type

            if trans.transaction_date:
                date_element = etree.SubElement(element, "transaction-date")
                date_element.attrib['iso-date'] = str(trans.transaction_date)

            if trans.value or trans.currency or trans.value_date:
                value_element = etree.SubElement(element, "value")

                if trans.value == 0 or trans.value:
                    value_element.text = str(trans.value)

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

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

            if trans.description:
                description_element = etree.SubElement(element, "description")
                narrative_element = etree.SubElement(description_element, "narrative")
                narrative_element.text = trans.description

            if trans.provider_organisation:
                element = _provider_organisation(element, trans)

            if trans.receiver_organisation:
                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():
                element = _sector(element, sector)

            if trans.recipient_country:
                recipient_country_element = etree.SubElement(element, "recipient-country")
                recipient_country_element.attrib['code'] = trans.recipient_country

            if trans.recipient_region or trans.recipient_region_vocabulary or \
                    trans.recipient_region_vocabulary_uri:
                recipient_region_element = etree.SubElement(element, "recipient-region")

                if trans.recipient_region_vocabulary_uri:
                    recipient_region_element.attrib['code'] = trans.recipient_region

                if trans.recipient_region_vocabulary:
                    recipient_region_element.attrib['vocabulary'] = trans.recipient_region_vocabulary

                if trans.recipient_region_vocabulary_uri:
                    recipient_region_element.attrib['vocabulary-uri'] = trans.\
                        recipient_region_vocabulary_uri

            if trans.flow_type:
                flow_type_element = etree.SubElement(element, "flow-type")
                flow_type_element.attrib['code'] = trans.flow_type

            if trans.finance_type:
                finance_type_element = etree.SubElement(element, "finance-type")
                finance_type_element.attrib['code'] = trans.finance_type

            if trans.aid_type:
                aid_type_element = etree.SubElement(element, "aid-type")
                aid_type_element.attrib['code'] = trans.aid_type
                aid_type_element.attrib['vocabulary'] = trans.aid_type_vocabulary

            if trans.tied_status:
                tied_status_element = etree.SubElement(element, "tied-status")
                tied_status_element.attrib['code'] = trans.tied_status

            transaction_elements.append(element)

    return transaction_elements
Esempio n. 27
0
def document_link(project):
    """
    Generate the document-link elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    document_link_elements = []

    if project.current_image:
        current_image_element = etree.Element("document-link")
        current_image_element.attrib['url'] = "http://rsr.akvo.org" + project.current_image.url
        current_image_element.attrib['format'] = "image/jpeg"

        title_element = etree.SubElement(current_image_element, "title")
        narrative_element = etree.SubElement(title_element, "narrative")

        if project.current_image_caption or project.current_image_credit:
            if project.current_image_caption and project.current_image_credit:
                narrative_element.text = "%s, credit: %s" % (
                    project.current_image_caption,
                    project.current_image_credit
                )
            elif project.current_image_caption:
                narrative_element.text = project.current_image_caption
            elif project.current_image_credit:
                narrative_element.text = "Credit: %s" % (project.current_image_credit,)
        else:
            narrative_element.text = "Project photo"

        category_element = etree.SubElement(current_image_element, "category")
        category_element.attrib['code'] = "A12"

        document_link_elements.append(current_image_element)

    for link in project.links.all():
        if link.url:
            link_element = etree.Element("document-link")
            link_element.attrib['url'] = link.url
            link_element.attrib['format'] = "application/http"

            title_element = etree.SubElement(link_element, "title")
            narrative_element = etree.SubElement(title_element, "narrative")
            narrative_element.text = link.caption if link.caption else "Project link"

            category_element = etree.SubElement(link_element, "category")
            category_element.attrib['code'] = "A12"

            document_link_elements.append(link_element)

    for document in project.documents.all():

        if has_data(document, ['url', 'document', 'format', 'title', 'language', 'document_date'])\
           or document.categories.exists():

            document_element = etree.Element("document-link")

            if document.url:
                document_element.attrib['url'] = document.url
            elif document.document:
                document_element.attrib['url'] = "http://rsr.akvo.org" + document.document.url

            if document.format:
                document_element.attrib['format'] = document.format

            if document.title:
                title_element = etree.SubElement(document_element, "title")
                narrative_element = etree.SubElement(title_element, "narrative")
                narrative_element.text = document.title

                if document.title_language:
                    narrative_element.attrib['{http://www.w3.org/XML/1998/namespace}lang'] = \
                        document.title_language

            for category in document.categories.all():
                category_element = etree.SubElement(document_element, "category")
                category_element.attrib['code'] = category.category

            if document.language:
                language_element = etree.SubElement(document_element, "language")
                language_element.attrib['code'] = document.language

            if document.document_date:
                date_element = etree.SubElement(document_element, "document-date")
                date_element.attrib['iso-date'] = str(document.document_date)

            document_link_elements.append(document_element)

    for update in project.project_updates.all():
        update_element = etree.Element("document-link")
        update_element.attrib['url'] = "http://rsr.akvo.org/project/%s/update/%s/" % \
                                       (str(project.pk), str(update.pk))
        update_element.attrib['format'] = "application/http"

        title_element = etree.SubElement(update_element, "title")
        narrative_element = etree.SubElement(title_element, "narrative")
        narrative_element.text = update.title if update.title else "Project update"

        category_element = etree.SubElement(update_element, "category")
        category_element.attrib['code'] = "A12"

        if update.language:
            language_element = etree.SubElement(update_element, "language")
            language_element.attrib['code'] = update.language

        document_link_elements.append(update_element)

    return document_link_elements
Esempio n. 28
0
def transaction(project):
    """
    Generate the transaction elements.

    :param project: Project object
    :return: A list of Etree elements
    """

    transaction_elements = []

    for trans in project.transactions.all():
        if (has_data(trans, [
                'reference',
                'transaction_type',
                'transaction_date',
                'value',
                'currency',
                'value_date',
                'description',
                'provider_organisation',
                'provider_organisation_activity',
                'receiver_organisation',
                'receiver_organisation_activity',
                'disbursement_channel',
                'recipient_country',
                'recipient_region',
                'recipient_region_vocabulary',
                'recipient_region_vocabulary_uri',
                'flow_type',
                'finance_type',
                'aid_type',
                'tied_status',
        ]) or trans.humanitarian is not None or trans.sectors.exists()):
            element = etree.Element("transaction")

            if trans.reference:
                element.attrib['ref'] = trans.reference

            if trans.humanitarian is not None:
                element.attrib[
                    'humanitarian'] = '1' if trans.humanitarian else '0'

            if trans.transaction_type:
                type_element = etree.SubElement(element, "transaction-type")
                type_element.attrib['code'] = trans.transaction_type

            if trans.transaction_date:
                date_element = etree.SubElement(element, "transaction-date")
                date_element.attrib['iso-date'] = str(trans.transaction_date)

            if trans.value or trans.currency or trans.value_date:
                value_element = etree.SubElement(element, "value")

                if trans.value == 0 or trans.value:
                    value_element.text = str(trans.value)

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

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

            if trans.description:
                description_element = etree.SubElement(element, "description")
                narrative_element = etree.SubElement(description_element,
                                                     "narrative")
                narrative_element.text = trans.description

            if trans.provider_organisation:
                element = _provider_organisation(element, trans)

            if trans.receiver_organisation:
                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():
                element = _sector(element, sector)

            if trans.recipient_country:
                recipient_country_element = etree.SubElement(
                    element, "recipient-country")
                recipient_country_element.attrib[
                    'code'] = trans.recipient_country

            if trans.recipient_region or trans.recipient_region_vocabulary or \
                    trans.recipient_region_vocabulary_uri:
                recipient_region_element = etree.SubElement(
                    element, "recipient-region")

                if trans.recipient_region_vocabulary_uri:
                    recipient_region_element.attrib[
                        'code'] = trans.recipient_region

                if trans.recipient_region_vocabulary:
                    recipient_region_element.attrib[
                        'vocabulary'] = trans.recipient_region_vocabulary

                if trans.recipient_region_vocabulary_uri:
                    recipient_region_element.attrib['vocabulary-uri'] = trans.\
                        recipient_region_vocabulary_uri

            if trans.flow_type:
                flow_type_element = etree.SubElement(element, "flow-type")
                flow_type_element.attrib['code'] = trans.flow_type

            if trans.finance_type:
                finance_type_element = etree.SubElement(
                    element, "finance-type")
                finance_type_element.attrib['code'] = trans.finance_type

            if trans.aid_type:
                aid_type_element = etree.SubElement(element, "aid-type")
                aid_type_element.attrib['code'] = trans.aid_type
                aid_type_element.attrib[
                    'vocabulary'] = trans.aid_type_vocabulary

            if trans.tied_status:
                tied_status_element = etree.SubElement(element, "tied-status")
                tied_status_element.attrib['code'] = trans.tied_status

            transaction_elements.append(element)

    return transaction_elements
Esempio n. 29
0
def document_link(project):
    """
    Generate the document-link elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    document_link_elements = []

    BASE_URL = "http://rsr.akvo.org"

    if project.current_image:
        current_image_element = etree.Element("document-link")
        current_image_element.attrib['url'] = urljoin(
            BASE_URL, project.current_image.url)
        current_image_element.attrib['format'] = "image/jpeg"

        title_element = etree.SubElement(current_image_element, "title")
        narrative_element = etree.SubElement(title_element, "narrative")

        if project.current_image_caption or project.current_image_credit:
            if project.current_image_caption and project.current_image_credit:
                narrative_element.text = "%s, credit: %s" % (
                    project.current_image_caption,
                    project.current_image_credit)
            elif project.current_image_caption:
                narrative_element.text = project.current_image_caption
            elif project.current_image_credit:
                narrative_element.text = "Credit: %s" % (
                    project.current_image_credit, )
        else:
            narrative_element.text = "Project photo"

        category_element = etree.SubElement(current_image_element, "category")
        category_element.attrib['code'] = "A12"

        document_link_elements.append(current_image_element)

    for link in project.links.all():
        if link.url:
            link_element = etree.Element("document-link")
            link_element.attrib['url'] = link.url
            link_element.attrib['format'] = "application/http"

            title_element = etree.SubElement(link_element, "title")
            narrative_element = etree.SubElement(title_element, "narrative")
            narrative_element.text = link.caption if link.caption else "Project link"

            category_element = etree.SubElement(link_element, "category")
            category_element.attrib['code'] = "A12"

            document_link_elements.append(link_element)

    for document in project.documents.all():

        if has_data(document, ['url', 'document', 'format', 'title', 'language', 'document_date'])\
           or document.categories.exists():

            document_element = etree.Element("document-link")

            if document.url:
                document_element.attrib['url'] = document.url
            elif document.document:
                document_element.attrib['url'] = urljoin(
                    BASE_URL, document.document.url)

            if document.format:
                document_element.attrib['format'] = document.format

            if document.title:
                title_element = etree.SubElement(document_element, "title")
                narrative_element = etree.SubElement(title_element,
                                                     "narrative")
                narrative_element.text = document.title

                if document.title_language:
                    narrative_element.attrib['{http://www.w3.org/XML/1998/namespace}lang'] = \
                        document.title_language

            for category in document.categories.all():
                category_element = etree.SubElement(document_element,
                                                    "category")
                category_element.attrib['code'] = category.category

            if document.language:
                language_element = etree.SubElement(document_element,
                                                    "language")
                language_element.attrib['code'] = document.language

            if document.document_date:
                date_element = etree.SubElement(document_element,
                                                "document-date")
                date_element.attrib['iso-date'] = str(document.document_date)

            document_link_elements.append(document_element)

    for update in project.project_updates.all():
        update_element = etree.Element("document-link")
        update_element.attrib['url'] = "http://rsr.akvo.org/project/%s/update/%s/" % \
                                       (str(project.pk), str(update.pk))
        update_element.attrib['format'] = "application/http"

        title_element = etree.SubElement(update_element, "title")
        narrative_element = etree.SubElement(title_element, "narrative")
        narrative_element.text = update.title if update.title else "Project update"

        category_element = etree.SubElement(update_element, "category")
        category_element.attrib['code'] = "A12"

        if update.language:
            language_element = etree.SubElement(update_element, "language")
            language_element.attrib['code'] = update.language

        document_link_elements.append(update_element)

    return document_link_elements
Esempio n. 30
0
def location(project):
    """
    Generate the location elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    location_elements = []

    for loc in project.locations.all():
        if (has_data(loc, ['reference', 'location_reach', 'location_code', 'vocabulary', 'name',
                           'description', 'activity_description', 'exactness', 'location_class',
                           'feature_designation', ]) or
                (loc.latitude and loc.longitude) or
                loc.administratives.exists()):
            element = etree.Element("location")
            if loc.reference:
                element.attrib['ref'] = loc.reference

            if loc.location_reach:
                reach_element = etree.SubElement(element, "location-reach")
                reach_element.attrib['code'] = loc.location_reach

            if loc.location_code or loc.vocabulary:
                id_element = etree.SubElement(element, "location-id")

                if loc.vocabulary:
                    id_element.attrib['vocabulary'] = loc.vocabulary

                if loc.location_code:
                    id_element.attrib['code'] = loc.location_code

            if loc.name:
                name_element = etree.SubElement(element, "name")
                narrative_element = etree.SubElement(name_element, "narrative")
                narrative_element.text = loc.name

            if loc.description:
                description_element = etree.SubElement(element, "description")
                narrative_element = etree.SubElement(description_element, "narrative")
                narrative_element.text = loc.description

            if loc.activity_description:
                activity_description_element = etree.SubElement(element, "activity-description")
                narrative_element = etree.SubElement(activity_description_element, "narrative")
                narrative_element.text = loc.activity_description

            for administrative in loc.administratives.all():
                if administrative.code or administrative.vocabulary or administrative.level:
                    administrative_element = etree.SubElement(element, "administrative")

                    if administrative.vocabulary:
                        administrative_element.attrib['vocabulary'] = administrative.vocabulary

                    if administrative.code:
                        administrative_element.attrib['code'] = administrative.code

                    if administrative.level:
                        administrative_element.attrib['level'] = str(administrative.level)

            if (loc.latitude == 0 or loc.latitude) and (loc.longitude == 0 or loc.longitude):
                point_element = etree.SubElement(element, "point")
                point_element.attrib['srsName'] = 'http://www.opengis.net/def/crs/EPSG/0/4326'
                pos_element = etree.SubElement(point_element, "pos")
                pos_element.text = "%s %s" % (str(loc.latitude), str(loc.longitude))

            if loc.exactness:
                exactness_element = etree.SubElement(element, "exactness")
                exactness_element.attrib['code'] = str(loc.exactness)

            if loc.location_class:
                class_element = etree.SubElement(element, "location-class")
                class_element.attrib['code'] = str(loc.location_class)

            if loc.feature_designation:
                feature_element = etree.SubElement(element, "feature-designation")
                feature_element.attrib['code'] = str(loc.feature_designation)

            location_elements.append(element)

    return location_elements
Esempio n. 31
0
def contact_info(project):
    """
    Generate the contact-info elements.

    :param project: Project object
    :return: A list of Etree elements
    """
    contact_info_elements = []

    for contact in project.contacts.all():
        if has_data(contact, [
                'type',
                'organisation',
                'department',
                'person_name',
                'job_title',
                'telephone',
                'email',
                'website',
                'mailing_address',
        ]):
            element = etree.Element("contact-info")

            if contact.type:
                element.attrib['type'] = str(contact.type)

            if contact.organisation:
                organisation_element = etree.SubElement(
                    element, "organisation")
                narrative_element = etree.SubElement(organisation_element,
                                                     "narrative")
                narrative_element.text = contact.organisation

            if contact.department:
                department_element = etree.SubElement(element, "department")
                narrative_element = etree.SubElement(department_element,
                                                     "narrative")
                narrative_element.text = contact.department

            if contact.person_name:
                person_name_element = etree.SubElement(element, "person-name")
                narrative_element = etree.SubElement(person_name_element,
                                                     "narrative")
                narrative_element.text = contact.person_name

            if contact.job_title:
                job_title_element = etree.SubElement(element, "job-title")
                narrative_element = etree.SubElement(job_title_element,
                                                     "narrative")
                narrative_element.text = contact.job_title

            if contact.telephone:
                telephone_element = etree.SubElement(element, "telephone")
                telephone_element.text = contact.telephone

            if contact.email:
                email_element = etree.SubElement(element, "email")
                email_element.text = contact.email

            if contact.website:
                website_element = etree.SubElement(element, "website")
                website_element.text = contact.website

            if contact.mailing_address:
                mailing_address_element = etree.SubElement(
                    element, "mailing-address")
                narrative_element = etree.SubElement(mailing_address_element,
                                                     "narrative")
                narrative_element.text = contact.mailing_address

            contact_info_elements.append(element)

    return contact_info_elements