Exemplo n.º 1
0
    def test_get_country(self):
        """
        Test for getting or creating a country.
        """
        country = custom_get_or_create_country('NL')
        self.assertIsInstance(country, Country)

        # Try again for existing country
        country = custom_get_or_create_country('NL')
        self.assertIsInstance(country, Country)
Exemplo n.º 2
0
 def hydrate_country(self, bundle):
     country = custom_get_or_create_country(bundle.data['country'])
     bundle.data['country'] = reverse('api_dispatch_detail',
                                      kwargs={
                                          'resource_name': 'country',
                                          'api_name': 'v1',
                                          'pk': country.pk
                                      })
     return bundle
Exemplo n.º 3
0
 def set_location_for_org(org_etree, internal_id, org):
     if not org.primary_location:
         iso_code = text_from_xpath(org_etree, 'location/object/iso_code').lower()
         if not iso_code == "ww!":
             country = custom_get_or_create_country(iso_code)
             location = OrganisationLocation.objects.create(
                 country=country,
                 location_target=org
             )
             org.locations.add(location)
             org.primary_location = location
             org.save()
             log(
                 u"  Added location to org {pk}",
                 dict(
                     log_type=LOG_ORGANISATIONS,
                     internal_id=internal_id,
                     pk=org.pk,
                     label=org.name,
                     event=ACTION_LOCATION_SET,
                 )
             )
         else:
             log(
                 u"Couldn't create location for org {pk}, no proper country code",
                 dict(
                     log_type=LOG_ORGANISATIONS,
                     internal_id=internal_id,
                     pk=org.pk,
                     label=org.name,
                     event=ERROR_COUNTRY_CODE,
                 )
             )
     else:
         log(
             u"  Org {pk} already has a location.",
             dict(
                 log_type=LOG_ORGANISATIONS,
                 internal_id=internal_id,
                 pk=org.pk,
                 label=org.name,
                 event=ACTION_LOCATION_FOUND,
             )
         )
def import_countries(xml_file):
    with open(xml_file, "rb") as f:
        cordaid = Organisation.objects.get(id=CORDAID_ORG_ID)
        root = etree.fromstring(f.read())
        for element in root:
            identifier = element.findtext("org_id")
            internal_org_id = InternalOrganisationID.objects.get(recording_org=cordaid, identifier=identifier)
            org = internal_org_id.referenced_org
            for location in element.find("location"):
                iso_code = location.findtext("iso_code").strip().upper()
                if not iso_code == "WW!":
                    country = custom_get_or_create_country(iso_code)
                    primary_location = OrganisationLocation(country=country)
                    org.primary_location = primary_location
                    org.save()
                    print(
                        u"Added country {country_name} to Organisation {org_id}.".format(
                            country_name=country.name.lower().capitalize(), org_id=org.id
                        )
                    )
def import_countries(xml_file):
    with open(xml_file, "rb") as f:
        cordaid = Organisation.objects.get(id=CORDAID_ORG_ID)
        root = etree.fromstring(f.read())
        for element in root:
            identifier = element.findtext("org_id")
            internal_org_id = InternalOrganisationID.objects.get(
                recording_org=cordaid, identifier=identifier)
            org = internal_org_id.referenced_org
            for location in element.find("location"):
                iso_code = location.findtext("iso_code").strip().upper()
                if not iso_code == "WW!":
                    country = custom_get_or_create_country(iso_code)
                    primary_location = OrganisationLocation(country=country)
                    org.primary_location = primary_location
                    org.save()
                    print(
                        u"Added country {country_name} to Organisation {org_id}."
                        .format(country_name=country.name.lower().capitalize(),
                                org_id=org.id))
Exemplo n.º 6
0
 def save_model(self, request, obj, form, change):
     if obj.iso_code:
         custom_get_or_create_country(obj.iso_code, obj)
Exemplo n.º 7
0
 def save_model(self, request, obj, form, change):
     if obj.iso_code:
         custom_get_or_create_country(obj.iso_code, obj)
Exemplo n.º 8
0
 def hydrate_country(self, bundle):
     country = custom_get_or_create_country(bundle.data['country'])
     bundle.data['country'] = reverse(
         'api_dispatch_detail', kwargs={'resource_name':'country', 'api_name': 'v1', 'pk': country.pk}
     )
     return bundle
Exemplo n.º 9
0
def create_project(project, answers):
    project_id = project["id"]
    form_id = project["form_id"]
    if form_id == OPTIMY_FORM_IDS["response-facility"]:
        lead_project_id = PROGRAM_IDS["Response Facility"]
    else:
        program_name = get_answer(form_id, answers, "program", ans_key="answer_name")
        lead_project_id = PROGRAM_IDS.get(program_name)
    if lead_project_id is None:
        print(f"Skipping {project_id} since it has no associated program")
        return None

    optimy_project_id_field = "Optimy Project ID"
    custom_field = ProjectCustomField.objects.filter(
        name=optimy_project_id_field, value=project_id
    ).first()
    title = get_answer(form_id, answers, "title")[:200]
    project_created = False
    if custom_field is not None:
        project = custom_field.project

    else:
        project = Project.objects.create(title=title)
        project_created = True
        ProjectCustomField.objects.get_or_create(
            project=project,
            name="Optimy Project ID",
            defaults=dict(value=project_id, section="1", order="1"),
        )

        validation_set = ProjectEditorValidationSet.objects.filter(name=VALIDATION_SET_NAME).first()
        if validation_set:
            project.validations.add(validation_set)

    program = Project.objects.get(pk=lead_project_id)
    project.add_to_program(program)
    # Add Aqua for All as financing partner
    Partnership.objects.get_or_create(
        project=project,
        organisation=A4A,
        iati_organisation_role=Partnership.IATI_FUNDING_PARTNER,
    )

    # Add implementing partner
    grantee = get_answer(form_id, answers, "grantee")
    if grantee and project_created:
        grantee_org = Organisation.objects.filter(Q(name=grantee) | Q(long_name=grantee)).first()
        if not grantee_org:
            grantee_org = Organisation.objects.create(
                name=textwrap.wrap(grantee, 40)[0],
                long_name=grantee
            )
        Partnership.objects.get_or_create(
            project=project,
            organisation=grantee_org,
            iati_organisation_role=Partnership.IATI_IMPLEMENTING_PARTNER,
        )

    # Add Aqua for All project Number
    project_number_question = get_answer(
        form_id, answers, "project-number", "question_name"
    )
    project_number_value = get_answer(form_id, answers, "project-number")
    if project_number_value:
        ProjectCustomField.objects.get_or_create(
            project=project,
            name=project_number_question,
            defaults=dict(value=project_number_value, section="1", order="1"),
        )

    start_date = get_answer(form_id, answers, "start-date")
    end_date = get_answer(form_id, answers, "end-date")

    iati_id = f"{A4A.iati_org_id}-{project.pk}"

    # Update project attributes
    data = dict(
        title=title,
        date_start_planned=start_date,
        date_end_planned=end_date,
        is_public=False,
        project_plan_summary=get_answer(form_id, answers, "summary"),
        iati_status="2",  # Implementation status
        iati_activity_id=iati_id,
    )
    # NOTE: Don't update Title, description and is_public for existing projects
    if not project_created:
        data.pop('title')
        data.pop('project_plan_summary')
        data.pop('is_public')

    data.update(DEFAULT_PROJECT_INFO)
    for key, value in data.items():
        if value is not None:
            setattr(project, key, value)
    project.save(update_fields=data.keys())

    # Create budget objects
    BudgetItem.objects.filter(project=project).delete()
    # Co-financing budget
    other = BudgetItemLabel.objects.get(label="Other")
    budget = get_answer(form_id, answers, "cofinancing-budget")
    extra = get_answer(form_id, answers, "cofinancing-budget", "answer_name")
    if budget:
        if extra:
            extra = " ".join(extra.split()[1:-1]).title()
        BudgetItem.objects.create(
            project=project,
            label=other,
            amount=budget,
            other_extra=extra,
            value_date=start_date,
            period_start=start_date,
            period_end=end_date,
        )
    # A4A budget
    budget = get_answer(form_id, answers, "a4a-budget")
    extra = get_answer(form_id, answers, "a4a-budget", "answer_name")
    if budget:
        if extra:
            extra = " ".join(extra.split()[1:-1]).title()
        BudgetItem.objects.create(
            project=project,
            label=other,
            amount=budget,
            other_extra=extra,
            value_date=start_date,
            period_start=start_date,
            period_end=end_date,
        )

    # Create location objects
    if project_created:
        project.primary_location = None
        if form_id == OPTIMY_FORM_IDS["response-facility"]:
            iso_code = get_answer(form_id, answers, "country").lower()
        else:
            name = get_answer(form_id, answers, "country", ans_key="answer_name")
            iso_code = COUNTRY_NAME_TO_ISO_MAP.get(name)
        if iso_code:
            country = custom_get_or_create_country(iso_code)
            ProjectLocation.objects.create(location_target=project, country=country)
        else:
            print(f"Could not find iso code for {name}")

    return project
Exemplo n.º 10
0
def create_project(project_id, answers):
    question_mapping = {
        "title": "9900586f-3c4b-5e3e-a9e6-a209eb8cb8e3",
        # FIXME: subtitle?
        "cofinancing-budget": "6c05de7b-4031-5809-a692-a45beadf7cec",
        "a4a-budget": "b0268b0c-d7e9-513a-bb27-1de7c0ec593a",
        "total-budget": "322932f0-e294-5621-a37b-fd57fec9937a",
        "aqua-for-all-budget": "b0268b0c-d7e9-513a-bb27-1de7c0ec593a",
        "co-financing-budget": "6c05de7b-4031-5809-a692-a45beadf7cec",
        "start-date": "b785b97e-64f7-5149-a07b-7216497aa39f",
        "end-date": "d3c4132c-1e55-5177-943e-3afa25b092ab",
        "project-number": "683c31bc-d1d3-57f2-bf57-2e4c54894181",
        "country": "913bec17-7f11-540a-8cb5-c5803e32a98b",
        "summary": "02f1316c-4d5c-5989-8183-e392a634d23e",
        "program": "09c477bb-d887-5862-9b12-ea5ab566b363",
    }
    answers_by_id = {ans["question_id"]: ans for ans in answers}

    def get_answer(key, ans_key="value"):
        answer = answers_by_id.get(question_mapping[key], {}).get(ans_key)
        if not answer:
            print(f"Could not find answer for {key}")
        return answer

    program_name = get_answer("program", ans_key="answer_name")
    lead_project_id = PROGRAM_IDS.get(program_name)
    if lead_project_id is None:
        print(f"Skipping {project_id} since it has no associated program")
        return None

    optimy_project_id_field = "Optimy Project ID"
    custom_field = ProjectCustomField.objects.filter(
        name=optimy_project_id_field, value=project_id).first()
    if custom_field is not None:
        project = custom_field.project

    else:
        title = get_answer("title")
        project = Project.objects.create(title=title)
        ProjectCustomField.objects.get_or_create(
            project=project,
            name="Optimy Project ID",
            defaults=dict(value=project_id, section="1", order="1"),
        )

    # Add Aqua for All project Number
    answer_project_number = answers_by_id.get(
        question_mapping["project-number"])
    if answer_project_number:
        ProjectCustomField.objects.get_or_create(
            project=project,
            name=answer_project_number["question_name"],
            defaults=dict(value=answer_project_number["value"],
                          section="1",
                          order="1"),
        )

    start_date = get_answer("start-date")
    end_date = get_answer("end-date")

    # Update project attributes
    data = dict(
        date_start_planned=start_date,
        date_end_planned=end_date,
        is_public=False,
        project_plan_summary=get_answer("summary"),
        iati_status="2",  # Implementation status
    )
    for key, value in data.items():
        if value is not None:
            setattr(project, key, value)
    project.save(update_fields=data.keys())

    # Add reporting organisation
    a4a = Organisation.objects.get(name="Aqua for All")
    project.set_reporting_org(a4a)

    # Add Aqua for All as financing partner
    Partnership.objects.get_or_create(
        project=project,
        organisation=a4a,
        iati_organisation_role=Partnership.IATI_FUNDING_PARTNER,
    )

    # Set lead project
    if lead_project_id is not None and not project.parents_all().exists():
        RelatedProject.objects.create(
            project=project,
            related_project_id=lead_project_id,
            relation=RelatedProject.PROJECT_RELATION_PARENT,
        )

    # Import results
    project.import_results()

    # Create budget objects
    BudgetItem.objects.filter(project=project).delete()
    # Co-financing budget
    other = BudgetItemLabel.objects.get(label="Other")
    budget = get_answer("cofinancing-budget")
    extra = get_answer("cofinancing-budget", "answer_name")
    if budget:
        if extra:
            extra = " ".join(extra.split()[1:-1]).title()
        BudgetItem.objects.create(
            project=project,
            label=other,
            amount=budget,
            other_extra=extra,
            period_start=start_date,
            period_end=end_date,
        )
    # A4A budget
    budget = get_answer("a4a-budget")
    extra = get_answer("a4a-budget", "answer_name")
    if budget:
        if extra:
            extra = " ".join(extra.split()[1:-1]).title()
        BudgetItem.objects.create(
            project=project,
            label=other,
            amount=budget,
            other_extra=extra,
            period_start=start_date,
            period_end=end_date,
        )
    # Total budget
    total = BudgetItemLabel.objects.get(label="Total")
    budget = get_answer("total-budget")
    if budget:
        BudgetItem.objects.create(
            project=project,
            label=total,
            amount=budget,
            period_start=start_date,
            period_end=end_date,
        )

    # Create location objects
    ProjectLocation.objects.filter(location_target=project).delete()
    project.primary_location = None
    name = get_answer("country", ans_key="answer_name")
    iso_code = COUNTRY_NAME_TO_ISO_MAP.get(name)
    if iso_code:
        country = custom_get_or_create_country(iso_code)
        ProjectLocation.objects.create(location_target=project,
                                       country=country)
    else:
        print(f"Could not find iso code for {name}")

    # Publish the project
    project.publish()

    return project