Ejemplo n.º 1
0
    def iati_organisations__iati_organisation(self, element):
        id = self._normalize(
            element.xpath('organisation-identifier/text()')[0])
        last_updated_datetime = self.validate_date(
            element.attrib.get('last-updated-datetime'))
        default_lang = element.attrib.get(
            '{http://www.w3.org/XML/1998/namespace}lang')
        default_currency = self.get_or_none(
            codelist_models.Currency,
            code=element.attrib.get('default-currency'))

        if not id:
            raise self.RequiredFieldError(
                "", "id", "organisation: must contain organisation-identifier")

        old_organisation = self.get_or_none(Organisation, id=id)

        if old_organisation:
            old_organisation.delete()

        organisation = Organisation()
        organisation.id = id
        organisation.organisation_identifier = id
        organisation.last_updated_datetime = last_updated_datetime
        organisation.default_lang_id = default_lang
        organisation.iati_standard_version_id = self.VERSION
        organisation.default_currency = default_currency

        self.organisation_identifier = organisation.organisation_identifier
        self.default_currency = default_currency

        # for later reference
        self.default_lang = default_lang

        self.register_model('Organisation', organisation)

        return element
Ejemplo n.º 2
0
    def iati_organisations__iati_organisation(self, element):
        organisation_identifier = element.xpath('organisation-identifier')
        if len(organisation_identifier) is not 1:
            raise ParserError("Organisation", "organisation-identifier",
                              "must occur once and only once.")
        # Here organisation_identifier is a string.
        organisation_identifier = organisation_identifier[0].text

        if organisation_identifier is None:
            raise RequiredFieldError("Organisation",
                                     "organisation-identifier", "required "
                                                                "field "
                                                                "missing.")
        # Here normalized_organisation_identifier is a string.
        normalized_organisation_identifier = self._normalize(
            organisation_identifier)

        # Although name and reporting-org is saved in different table,
        # according to specifications it must occur once and only once in
        # organisation element. So we check if 'name' and 'reporting-org'
        # element occurs at least once here.
        name = element.xpath("name")
        reporting_org = element.xpath("reporting-org")
        if len(name) and len(reporting_org) is not 1:
            raise ParserError("Organisation", "name and reporting-org",
                              "must occur at least once.")

        last_updated_datetime = self.validate_date(element.attrib.get(
            "last-updated-datetime"))
        default_lang_code = element.attrib.get(
            '{http://www.w3.org/XML/1998/namespace}lang',
            settings.DEFAULT_LANG)
        if default_lang_code:
            default_lang_code = default_lang_code.lower()

        default_lang = self.get_or_none(
            codelist_models.Language,
            code=default_lang_code
        )
        default_currency = self.get_or_none(
            codelist_models.Currency,
            code=element.attrib.get('default-currency'))

        old_organisation = self.get_or_none(
            Organisation, organisation_identifier=organisation_identifier)
        if old_organisation:
            if old_organisation.last_updated_datetime < last_updated_datetime:

                OrganisationName.objects.filter(
                    organisation=old_organisation).delete()
                OrganisationReportingOrganisation.objects.filter(
                    organisation=old_organisation).delete()
                TotalBudget.objects.filter(
                    organisation=old_organisation).delete()
                RecipientOrgBudget.objects.filter(
                    organisation=old_organisation).delete()
                RecipientCountryBudget.objects.filter(
                    organisation=old_organisation).delete()
                RecipientRegionBudget.objects.filter(
                    organisation=old_organisation).delete()
                TotalExpenditure.objects.filter(
                    organisation=old_organisation).delete()
                OrganisationDocumentLink.objects.filter(
                    organisation=old_organisation).delete()

                organisation = old_organisation
                organisation.organisation_identifier = organisation_identifier
                organisation.normalized_organisation_identifier = \
                    normalized_organisation_identifier
                organisation.last_updated_datetime = last_updated_datetime
                organisation.default_lang = default_lang
                organisation.iati_standard_version_id = self.VERSION
                organisation.default_currency = default_currency

                organisation.published = True
                organisation.ready_to_publish = True
                organisation.modified = True

                self.organisation_identifier = \
                    organisation.organisation_identifier
                self.default_currency = default_currency

                # for later reference
                self.default_lang = default_lang

                self.register_model('Organisation', organisation)

                return element
            else:  # if the current element's date is later than or equal to
                # the existing element's date, raise parser error.
                raise ParserError("Organisation",
                                  None, msg="last-updated-datetime "
                                            "is earlier than old element's "
                                            "last_updated_datetime.")
        else:
            organisation = Organisation()

            organisation.organisation_identifier = organisation_identifier
            organisation.normalized_organisation_identifier = \
                normalized_organisation_identifier
            organisation.last_updated_datetime = last_updated_datetime
            organisation.default_lang = default_lang
            organisation.iati_standard_version_id = self.VERSION
            organisation.default_currency = default_currency

            organisation.published = True
            organisation.ready_to_publish = True
            organisation.modified = False

            self.organisation_identifier = organisation.organisation_identifier
            self.default_currency = default_currency

            # for later reference
            self.default_lang = default_lang

            self.register_model('Organisation', organisation)

            return element
Ejemplo n.º 3
0
    def iati_organisations__iati_organisation(self, element):
        id = element.xpath('organisation-identifier/text()')[0]
        normalized_id = self._normalize(id)
        last_updated_datetime = self.validate_date(
            element.attrib.get('last-updated-datetime'))
        # default is here to make it default to settings 'DEFAULT_LANG' on no
        # language set (validation error we want to be flexible per instance)

        default_lang_code = element.attrib.get(
            '{http://www.w3.org/XML/1998/namespace}lang',
            settings.DEFAULT_LANG)
        if default_lang_code:
            default_lang_code = default_lang_code.lower()

        default_lang = self.get_or_none(codelist_models.Language,
                                        code=default_lang_code)

        default_currency = self.get_or_none(
            codelist_models.Currency,
            code=element.attrib.get('default-currency'))

        if not id:
            raise RequiredFieldError(
                "", "id", "organisation: must contain organisation-identifier")

        # TODO: check for last-updated-datetime - 2017-03-27
        old_organisation = self.get_or_none(Organisation,
                                            organisation_identifier=id)

        if old_organisation:
            OrganisationName.objects.filter(
                organisation=old_organisation).delete()
            OrganisationReportingOrganisation.objects.filter(
                organisation=old_organisation).delete()
            TotalBudget.objects.filter(organisation=old_organisation).delete()
            RecipientOrgBudget.objects.filter(
                organisation=old_organisation).delete()
            RecipientCountryBudget.objects.filter(
                organisation=old_organisation).delete()
            RecipientRegionBudget.objects.filter(
                organisation=old_organisation).delete()
            TotalExpenditure.objects.filter(
                organisation=old_organisation).delete()
            OrganisationDocumentLink.objects.filter(
                organisation=old_organisation).delete()

            organisation = old_organisation
        else:
            organisation = Organisation()

        organisation.organisation_identifier = id
        organisation.normalized_organisation_identifier = normalized_id
        organisation.last_updated_datetime = last_updated_datetime
        organisation.default_lang = default_lang
        organisation.iati_standard_version_id = self.VERSION
        organisation.default_currency = default_currency

        organisation.published = True
        organisation.ready_to_publish = True
        organisation.modified = False

        self.organisation_identifier = organisation.organisation_identifier
        self.default_currency = default_currency

        # for later reference
        self.default_lang = default_lang

        self.register_model('Organisation', organisation)

        return element
    def test_organisation_file_document_link_list_endpoint_result(self):
        # Version
        version = Version(code='2.03')
        version.save()

        # Language
        language = Language(code='en', name='English')
        language.save()

        # Organisation
        organisation_identifier = 'test-org'
        organisation = Organisation(
            organisation_identifier=organisation_identifier,
            normalized_organisation_identifier='test-org',
            iati_standard_version=version,
            primary_name='Test Primary Name')
        organisation.save()

        # Organisation Name
        organisation_name = OrganisationName(organisation=organisation)
        organisation_name.save()

        # Organisation Narrative
        OrganisationNarrative(
            organisation=organisation,
            content_type=ContentType.objects.get_for_model(organisation_name),
            object_id=organisation_name.id,
            language=language,
            content='Test Content').save()

        # File Format
        file_format = FileFormat(code='application/http', name='URL')
        file_format.save()

        # Organisation Document Link
        organisation_document_link_url = 'https://www.test.com'
        organisation_document_link = OrganisationDocumentLink(
            organisation=organisation,
            url=organisation_document_link_url,
            file_format=file_format,
            language=language,
            iso_date=datetime.datetime.strptime('2018-01-01', '%Y-%m-%d'))
        organisation_document_link.save()

        # DocumentCategoryCategory
        document_category_category = DocumentCategoryCategory(
            code='B', name='Organisation Level')
        document_category_category.save()

        # Document Category
        document_category = DocumentCategory(
            code='B01', name='Organisation Web URL',
            category=document_category_category)
        document_category.save()

        # Organisation Document Link Category
        organisation_document_link_category = \
            OrganisationDocumentLinkCategory(
                document_link=organisation_document_link,
                category=document_category)
        organisation_document_link_category.save()

        # API client
        client = APIClient()

        # URL endpoint organisation dopcument links
        url = reverse(
            'organisations:organisation-file-organisation-document-link-list',
            kwargs={'organisation_identifier': organisation_identifier})

        # Get response from client
        response = client.get(path=url)
        first_organisation_document_link = response.data.get('results')[0]

        # The first result of the organisation document link list
        # should be the same with above URL 'https://www.test.com'
        self.assertEqual(
            first_organisation_document_link.get('url'),
            organisation_document_link_url)
    def test_organisation_file_document_link_list_endpoint_result(self):
        # Version
        version = Version(code='2.03')
        version.save()

        # Language
        language = Language(code='en', name='English')
        language.save()

        # Organisation
        organisation_identifier = 'test-org'
        organisation = Organisation(
            organisation_identifier=organisation_identifier,
            normalized_organisation_identifier='test-org',
            iati_standard_version=version,
            primary_name='Test Primary Name')
        organisation.save()

        # Organisation Name
        organisation_name = OrganisationName(organisation=organisation)
        organisation_name.save()

        # Organisation Narrative
        OrganisationNarrative(
            organisation=organisation,
            content_type=ContentType.objects.get_for_model(organisation_name),
            object_id=organisation_name.id,
            language=language,
            content='Test Content').save()

        # File Format
        file_format = FileFormat(code='application/http', name='URL')
        file_format.save()

        # Organisation Document Link
        organisation_document_link_url = 'https://www.test.com'
        organisation_document_link = OrganisationDocumentLink(
            organisation=organisation,
            url=organisation_document_link_url,
            file_format=file_format,
            language=language,
            iso_date=datetime.datetime.strptime('2018-01-01', '%Y-%m-%d'))
        organisation_document_link.save()

        # DocumentCategoryCategory
        document_category_category = DocumentCategoryCategory(
            code='B', name='Organisation Level')
        document_category_category.save()

        # Document Category
        document_category = DocumentCategory(
            code='B01',
            name='Organisation Web URL',
            category=document_category_category)
        document_category.save()

        # Organisation Document Link Category
        organisation_document_link_category = \
            OrganisationDocumentLinkCategory(
                document_link=organisation_document_link,
                category=document_category)
        organisation_document_link_category.save()

        # API client
        client = APIClient()

        # URL endpoint organisation dopcument links
        url = reverse(
            'organisations:organisation-file-organisation-document-link-list',
            kwargs={'organisation_identifier': organisation_identifier})

        # Get response from client
        response = client.get(path=url)
        first_organisation_document_link = response.data.get('results')[0]

        # The first result of the organisation document link list
        # should be the same with above URL 'https://www.test.com'
        self.assertEqual(first_organisation_document_link.get('url'),
                         organisation_document_link_url)