Пример #1
0
def iati_outcome(activity, benchmarks, benchmark_names, categories):
    """Collects all RSR benchmark information and adds them to the activity as results."""

    bm_categories = benchmarks.values_list('category_id', flat=True)
    categories_distinct = list()
    map(lambda x: not x in categories_distinct and categories_distinct.append(x), bm_categories)

    for category in categories_distinct:
        sector_id, sector_name = sector_mapping(category)
        if check_value(sector_id):
            sector_node = schema.sector(valueOf_=sector_name)
            sector_node.set_code(sector_id)
            activity.add_sector(sector_node)

        result_title = schema.textType(valueOf_=xml_enc(categories.get(id=category).name))
        result = schema.result(type_="2")
        result.add_title(result_title)

        for benchmark in benchmarks.filter(category_id=category):
            result_text = str(benchmark.value) + " " + benchmark_names.get(id=benchmark.name_id).name
            result_description = schema.textType(valueOf_=xml_enc(result_text))
            result.add_description(result_description)

        activity.add_result(result)

    return activity
Пример #2
0
def iati_outcome(activity, benchmarks, benchmark_names, categories):
    """Collects all RSR benchmark information and adds them to the activity as results."""

    bm_categories = benchmarks.values_list('category_id', flat=True)
    categories_distinct = list()
    map(
        lambda x: not x in categories_distinct and categories_distinct.append(
            x), bm_categories)

    for category in categories_distinct:
        sector_id, sector_name = sector_mapping(category)
        if check_value(sector_id):
            sector_node = schema.sector(valueOf_=sector_name)
            sector_node.set_code(sector_id)
            activity.add_sector(sector_node)

        result_title = schema.textType(
            valueOf_=xml_enc(categories.get(id=category).name))
        result = schema.result(type_="2")
        result.add_title(result_title)

        for benchmark in benchmarks.filter(category_id=category):
            result_text = str(benchmark.value) + " " + benchmark_names.get(
                id=benchmark.name_id).name
            result_description = schema.textType(valueOf_=xml_enc(result_text))
            result.add_description(result_description)

        activity.add_result(result)

    return activity
Пример #3
0
def iati_location(activity, location, country):
    """Collects location of the RSR project and adds it to the activity."""

    location_node = schema.location()

    # Set value of location node. "<city>, <state>" if both are available.
    if check_value(location.city):
        if check_value(location.state):
            location_name = schema.textType(
                valueOf_=xml_enc(location.city + ", " + location.state))
            location_node.add_name(location_name)

        else:
            location_name = schema.textType(valueOf_=xml_enc(location.city))
            location_node.add_name(location_name)

        location_type = schema.textType(valueOf_="populated place")
        location_type.set_anyAttributes_({"code": "PPL"})
        location_node.add_location_type(location_type)

    elif check_value(location.state):
        location_name = schema.textType(valueOf_=xml_enc(location.state))
        location_node.add_name(location_name)

        location_type = schema.textType(
            valueOf_="first-order administrative division")
        location_type.set_anyAttributes_({"code": "ADM1"})
        location_node.add_location_type(location_type)

    coordinates = schema.coordinatesType(latitude=location.latitude,
                                         longitude=location.longitude)
    location_node.add_coordinates(coordinates)

    if check_value(country.name) and check_value(country.iso_code):
        administrative = schema.administrativeType(
            country=country.iso_code.upper(), valueOf_=xml_enc(country.name))

        location_node.add_administrative(administrative)

        # Add recipient-country node
        recipient_country_node = schema.recipient_country(
            code=country.iso_code.upper(), valueOf_=xml_enc(country.name))
        activity.add_recipient_country(recipient_country_node)

    if check_value(location.address_1):
        location_node.set_anyAttributes_(
            {"akvo:address-1": xml_enc(location.address_1)})

    if check_value(location.address_2):
        location_node.set_anyAttributes_(
            {"akvo:address-2": xml_enc(location.address_2)})

    if check_value(location.postcode):
        location_node.set_anyAttributes_(
            {"akvo:post-code": xml_enc(location.postcode)})

    activity.add_location(location_node)

    return activity
Пример #4
0
def iati_location(activity, location, country):
    """Collects location of the RSR project and adds it to the activity."""

    location_node = schema.location()

    # Set value of location node. "<city>, <state>" if both are available.
    if check_value(location.city):
        if check_value(location.state):
            location_name = schema.textType(valueOf_=xml_enc(location.city + ", " + location.state))
            location_node.add_name(location_name)

        else:
            location_name = schema.textType(valueOf_=xml_enc(location.city))
            location_node.add_name(location_name)

        location_type = schema.textType(valueOf_="populated place")
        location_type.set_anyAttributes_({"code": "PPL"})
        location_node.add_location_type(location_type)

    elif check_value(location.state):
        location_name = schema.textType(valueOf_=xml_enc(location.state))
        location_node.add_name(location_name)

        location_type = schema.textType(valueOf_="first-order administrative division")
        location_type.set_anyAttributes_({"code": "ADM1"})
        location_node.add_location_type(location_type)

    coordinates = schema.coordinatesType(latitude=location.latitude, longitude=location.longitude)
    location_node.add_coordinates(coordinates)

    if check_value(country.name) and check_value(country.iso_code):
        administrative = schema.administrativeType(country=country.iso_code.upper(), valueOf_=xml_enc(country.name))

        location_node.add_administrative(administrative)

        # Add recipient-country node
        recipient_country_node = schema.recipient_country(code=country.iso_code.upper(), valueOf_=xml_enc(country.name))
        activity.add_recipient_country(recipient_country_node)

    if check_value(location.address_1):
        location_node.set_anyAttributes_({"akvo:address-1": xml_enc(location.address_1)})

    if check_value(location.address_2):
        location_node.set_anyAttributes_({"akvo:address-2": xml_enc(location.address_2)})

    if check_value(location.postcode):
        location_node.set_anyAttributes_({"akvo:post-code": xml_enc(location.postcode)})

    activity.add_location(location_node)

    return activity
Пример #5
0
def iati_photo(activity, project):
    """Collects the actual photo of the RSR project and adds it to the activity."""

    photo_url = "http://rsr.akvo.org/media/" + str(project.current_image)

    allowed_extensions = ["jpg", "jpeg", "png", "gif", "tiff", "bmp"]
    try:
        extension = str(project.current_image).rsplit('.',1)[1].lower()
    except:
        extension = ""

    document_link = schema.document_link(url=xml_enc(photo_url))

    if check_value(extension) and extension in allowed_extensions:
        document_link.set_format(xml_enc("image/" + extension))

    if check_value(project.current_image_caption):
        caption = schema.textType(valueOf_=xml_enc(project.current_image_caption))
        document_link.add_title(caption)

    if check_value(project.current_image_credit):
        document_link.set_anyAttributes_({"akvo:photo-credit": xml_enc(project.current_image_credit)})

    activity.add_document_link(document_link)

    return activity
Пример #6
0
def iati_budget(activity, budgets, project):
    """Collects budget of the RSR project and adds it to the activity."""

    # Ignore total budgets -- label 13 or 14 -- if not all budgets are a total budget
    if not all(budget.get_label() == 13 or 14 for budget in budgets):
        budgets = filter(lambda x: x.get_label() == 13 or 14, budgets)

    for budget in budgets:
        budget_node = schema.budget()

        budget_label = budget.get_label()

        if check_value(budget.amount):
            budget_value = schema.textType(valueOf_=budget.amount)

            if check_value(project.date_start_actual):
                budget_value.set_anyAttributes_({"value-date": project.date_start_actual})
            elif check_value(project.date_start_planned):
                budget_value.set_anyAttributes_({"value-date": project.date_start_planned})

            budget_node.add_value(budget_value)

        if check_value(budget_label):
            budget_node.set_anyAttributes_({"akvo:type": xml_enc(budget_label)})

        if check_value(budget.other_extra):
            budget_node.set_anyAttributes_({"akvo:description": xml_enc(budget.other_extra)})

        activity.add_budget(budget_node)

    return activity
Пример #7
0
def iati_photo(activity, project):
    """Collects the actual photo of the RSR project and adds it to the activity."""

    photo_url = "http://rsr.akvo.org/media/" + str(project.current_image)

    allowed_extensions = ["jpg", "jpeg", "png", "gif", "tiff", "bmp"]
    try:
        extension = str(project.current_image).rsplit('.', 1)[1].lower()
    except:
        extension = ""

    document_link = schema.document_link(url=xml_enc(photo_url))

    if check_value(extension) and extension in allowed_extensions:
        document_link.set_format(xml_enc("image/" + extension))

    if check_value(project.current_image_caption):
        caption = schema.textType(
            valueOf_=xml_enc(project.current_image_caption))
        document_link.add_title(caption)

    if check_value(project.current_image_credit):
        document_link.set_anyAttributes_(
            {"akvo:photo-credit": xml_enc(project.current_image_credit)})

    activity.add_document_link(document_link)

    return activity
Пример #8
0
def iati_budget(activity, budgets):
    """Collects budget of the RSR project and adds it to the activity."""

    # Ignore total budgets -- label 13 or 14 -- if not all budgets are a total budget
    if not all(budget.get_label() == 13 or 14 for budget in budgets):
        budgets = filter(lambda x: x.get_label() == 13 or 14, budgets)

    for budget in budgets:
        budget_node = schema.budget()

        budget_label = budget.get_label()

        if check_value(budget.amount):
            budget_value = schema.textType(valueOf_=budget.amount)
            budget_node.add_value(budget_value)

        if check_value(budget_label):
            budget_node.set_anyAttributes_(
                {"akvo:type": xml_enc(budget_label)})

        if check_value(budget.other_extra):
            budget_node.set_anyAttributes_(
                {"akvo:description": xml_enc(budget.other_extra)})

        activity.add_budget(budget_node)

    return activity
Пример #9
0
def iati_goals(activity, goals):
    """Collects all goals as specified in the RSR project and adds them to the activity."""

    for goal in goals:
        if check_value(goal.text):
            goal_text = schema.textType(valueOf_=xml_enc(goal.text))
            result = schema.result(type_="1")
            result.add_title(goal_text)
            activity.add_result(result)

    return activity
Пример #10
0
def iati_goals(activity, goals):
    """Collects all goals as specified in the RSR project and adds them to the activity."""

    for goal in goals:
        if check_value(goal.text):
            goal_text = schema.textType(valueOf_=xml_enc(goal.text))
            result = schema.result(type_="1")
            result.add_title(goal_text)
            activity.add_result(result)

    return activity
Пример #11
0
def iati_contact(activity, organisation):
    """Collects contact information of the RSR organisation and adds it to the activity."""

    contact_node = schema.contact_info()

    if check_value(organisation.long_name):
        long_name = schema.textType(valueOf_=xml_enc(organisation.long_name))
        contact_node.add_organisation(long_name)
    elif check_value(organisation.name):
        org_name = schema.textType(valueOf_=xml_enc(organisation.name))
        contact_node.add_organisation(org_name)

    if check_value(organisation.url):
        contact_node.add_website(xml_enc(organisation.url))

    if check_value(organisation.phone):
        phone = schema.textType(valueOf_=xml_enc(organisation.phone))
        contact_node.add_telephone(phone)
    elif check_value(organisation.mobile):
        mobile = schema.textType(valueOf_=xml_enc(organisation.mobile))
        contact_node.add_telephone(mobile)

    if check_value(organisation.contact_person):
        person = schema.textType(valueOf_=xml_enc(organisation.contact_person))
        contact_node.add_person_name(person)

    if check_value(organisation.contact_email):
        email = schema.textType(valueOf_=xml_enc(organisation.contact_email))
        contact_node.add_email(email)

    activity.add_contact_info(contact_node)

    return activity
Пример #12
0
def iati_contact(activity, organisation):
    """Collects contact information of the RSR organisation and adds it to the activity."""

    contact_node = schema.contact_info()

    if check_value(organisation.long_name):
        long_name = schema.textType(valueOf_=xml_enc(organisation.long_name))
        contact_node.add_organisation(long_name)
    elif check_value(organisation.name):
        org_name = schema.textType(valueOf_=xml_enc(organisation.name))
        contact_node.add_organisation(org_name)

    if check_value(organisation.url):
        contact_node.add_website(xml_enc(organisation.url))

    if check_value(organisation.phone):
        phone = schema.textType(valueOf_=xml_enc(organisation.phone))
        contact_node.add_telephone(phone)
    elif check_value(organisation.mobile):
        mobile = schema.textType(valueOf_=xml_enc(organisation.mobile))
        contact_node.add_telephone(mobile)

    if check_value(organisation.contact_person):
        person = schema.textType(valueOf_=xml_enc(organisation.contact_person))
        contact_node.add_person_name(person)

    if check_value(organisation.contact_email):
        email = schema.textType(valueOf_=xml_enc(organisation.contact_email))
        contact_node.add_email(email)

    activity.add_contact_info(contact_node)

    return activity
Пример #13
0
def iati_activity(activity, project):
    """Collects all underlying nodes of the <iati-activity> node and adds them to the activity."""

    def project_status():
        """Converts the status of the RSR project to IATI codes and description.

        Mapped to the RSR Project Status: Pipeline/identification => Needs Funding,
        Implementation => Active, Completion => Completed, Post-completion => Completed, Cancelled => Cancelled."""

        if project.status == Project.STATUS_NEEDS_FUNDING:
            return IATI_LIST_ACTIVITY_STATUS[0][0], IATI_LIST_ACTIVITY_STATUS[0][1]
        elif project.status == Project.STATUS_ACTIVE:
            return IATI_LIST_ACTIVITY_STATUS[1][0], IATI_LIST_ACTIVITY_STATUS[1][1]
        elif project.status == Project.STATUS_COMPLETE:
            return IATI_LIST_ACTIVITY_STATUS[2][0], IATI_LIST_ACTIVITY_STATUS[2][1]
        elif project.status == Project.STATUS_ARCHIVED:
            return IATI_LIST_ACTIVITY_STATUS[3][0], IATI_LIST_ACTIVITY_STATUS[3][1]
        elif project.status == Project.STATUS_CANCELLED:
            return IATI_LIST_ACTIVITY_STATUS[4][0], IATI_LIST_ACTIVITY_STATUS[4][1]
        else:
            # Impossible to have another status
            raise MandatoryError(project.title, project.pk, "status")

    # Title
    activity.add_title(schema.textType(valueOf_=xml_enc(project.title)))

    # Subtitle
    subtitle = schema.description(type_="1",valueOf_=xml_enc(project.subtitle))
    subtitle.set_anyAttributes_({"akvo:type":"4"})
    activity.add_description(subtitle)

    # Project plan summary
    pps = schema.description(type_="1",valueOf_=xml_enc(project.project_plan_summary))
    pps.set_anyAttributes_({"akvo:type":"5"})
    activity.add_description(pps)

    # Background
    if check_value(project.background):
        background = schema.description(type_="1",valueOf_=xml_enc(project.background))
        background.set_anyAttributes_({"akvo:type":"6"})
        activity.add_description(background)

    # Project plan
    if check_value(project.project_plan):
        project_plan = schema.description(type_="1",valueOf_=xml_enc(project.project_plan))
        project_plan.set_anyAttributes_({"akvo:type":"7"})
        activity.add_description(project_plan)

    # Current status
    if check_value(project.current_status):
        current_status = schema.description(type_="1",valueOf_=xml_enc(project.current_status))
        current_status.set_anyAttributes_({"akvo:type":"9"})
        activity.add_description(current_status)

    # Sustainability
    sustainability = schema.description(type_="1",valueOf_=xml_enc(project.sustainability))
    sustainability.set_anyAttributes_({"akvo:type":"10"})
    activity.add_description(sustainability)

    # Project status
    status_code, status_description = project_status()
    project_status = schema.textType(valueOf_=xml_enc(status_description))
    project_status.set_anyAttributes_({"code": status_code})
    activity.add_activity_status(project_status)

    # Goals overview
    goals_overview = schema.description(type_="2",valueOf_=xml_enc(project.goals_overview))
    goals_overview.set_anyAttributes_({"akvo:type":"8"})
    activity.add_description(goals_overview)

    # Date start (actual)
    if check_value(project.date_start_actual):
        start_actual = schema.activity_date(iso_date=project.date_start_actual, type_="start-actual",
                                            valueOf_=project.date_start_actual)
        activity.add_activity_date(start_actual)

    # Date start (planned)
    if check_value(project.date_start_planned):
        start_planned = schema.activity_date(iso_date=project.date_start_planned, type_="start-planned",
                                             valueOf_=project.date_start_planned)
        activity.add_activity_date(start_planned)

    # Date end (actual)
    if check_value(project.date_end_actual):
        end_actual = schema.activity_date(iso_date=project.date_end_actual, type_="end-actual",
                                          valueOf_=project.date_end_actual)
        activity.add_activity_date(end_actual)

    # Date end (planned)
    if check_value(project.date_end_planned):
        end_planned = schema.activity_date(iso_date=project.date_end_planned, type_="end-planned",
                                           valueOf_=project.date_end_planned)
        activity.add_activity_date(end_planned)

    return activity
Пример #14
0
def iati_activity(activity, project):
    """Collects all underlying nodes of the <iati-activity> node and adds them to the activity."""
    def project_status():
        """Converts the status of the RSR project to IATI codes and description.

        Mapped to the RSR Project Status: Pipeline/identification => Needs Funding,
        Implementation => Active, Completion => Completed, Post-completion => Completed, Cancelled => Cancelled."""

        if project.status == 'H':
            return IATI_LIST_ACTIVITY_STATUS[0][0], IATI_LIST_ACTIVITY_STATUS[
                0][1]
        elif project.status == 'A':
            return IATI_LIST_ACTIVITY_STATUS[1][0], IATI_LIST_ACTIVITY_STATUS[
                1][1]
        elif project.status == 'C':
            return IATI_LIST_ACTIVITY_STATUS[2][0], IATI_LIST_ACTIVITY_STATUS[
                2][1]
        elif project.status == 'R':
            return IATI_LIST_ACTIVITY_STATUS[3][0], IATI_LIST_ACTIVITY_STATUS[
                3][1]
        elif project.status == 'L':
            return IATI_LIST_ACTIVITY_STATUS[4][0], IATI_LIST_ACTIVITY_STATUS[
                4][1]
        else:
            # Impossible to have another status
            raise MandatoryError(project.title, project.pk, "status")

    # Title
    activity.add_title(schema.textType(valueOf_=xml_enc(project.title)))

    # Subtitle
    subtitle = schema.description(type_="1",
                                  valueOf_=xml_enc(project.subtitle))
    subtitle.set_anyAttributes_({"akvo:type": "4"})
    activity.add_description(subtitle)

    # Project plan summary
    pps = schema.description(type_="1",
                             valueOf_=xml_enc(project.project_plan_summary))
    pps.set_anyAttributes_({"akvo:type": "5"})
    activity.add_description(pps)

    # Background
    if check_value(project.background):
        background = schema.description(type_="1",
                                        valueOf_=xml_enc(project.background))
        background.set_anyAttributes_({"akvo:type": "6"})
        activity.add_description(background)

    # Project plan
    if check_value(project.project_plan):
        project_plan = schema.description(type_="1",
                                          valueOf_=xml_enc(
                                              project.project_plan))
        project_plan.set_anyAttributes_({"akvo:type": "7"})
        activity.add_description(project_plan)

    # Current status
    if check_value(project.current_status):
        current_status = schema.description(type_="1",
                                            valueOf_=xml_enc(
                                                project.current_status))
        current_status.set_anyAttributes_({"akvo:type": "9"})
        activity.add_description(current_status)

    # Sustainability
    sustainability = schema.description(type_="1",
                                        valueOf_=xml_enc(
                                            project.sustainability))
    sustainability.set_anyAttributes_({"akvo:type": "10"})
    activity.add_description(sustainability)

    # Project status
    status_code, status_description = project_status()
    project_status = schema.textType(valueOf_=xml_enc(status_description))
    project_status.set_anyAttributes_({"code": status_code})
    activity.add_activity_status(project_status)

    # Goals overview
    goals_overview = schema.description(type_="2",
                                        valueOf_=xml_enc(
                                            project.goals_overview))
    goals_overview.set_anyAttributes_({"akvo:type": "8"})
    activity.add_description(goals_overview)

    # Date request posted
    if check_value(project.date_request_posted):
        start_actual = schema.activity_date(
            iso_date=project.date_request_posted,
            type_="start-actual",
            valueOf_=project.date_request_posted)
        activity.add_activity_date(start_actual)

    # Date complete
    if check_value(project.date_complete):
        end_planned = schema.activity_date(iso_date=project.date_complete,
                                           type_="end-planned",
                                           valueOf_=project.date_complete)
        activity.add_activity_date(end_planned)

    return activity