コード例 #1
0
def test_read_related_decree_as_main(oereblex_test_config):
    del oereblex_test_config

    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.2.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml',
                  content=f.read())
        source = OEREBlexSource(
            host='http://oereblex.example.com',
            language='de',
            canton='BL',
            version='1.2.2',
            related_decree_as_main=True,
            code='ch.Waldabstandslinien',
        )
        source.read(MockParameter(), 100)
        assert len(source.records) == 9
        document = source.records[0]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {
            'de': 'Bauverwaltung Gemeinde'
        }
        assert document.text_at_web == {
            'de': 'http://oereblex.example.com/api/attachments/4735'
        }
        assert document.index == 30
コード例 #2
0
def test_read_related_notice_as_main(oereblex_test_config):
    del oereblex_test_config

    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.2.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml',
                  content=f.read())
        source = OEREBlexSource(
            host='http://oereblex.example.com',
            language='de',
            canton='BL',
            version='1.2.2',
            related_notice_as_main=True,
            code='ch.Waldabstandslinien',
        )
        source.read(MockParameter(), 100)
        assert len(source.records) == 9
        document = source.records[8]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.document_type, DocumentTypeRecord)
        assert document.document_type.code == 'Hint'
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {'de': '-'}
        assert document.responsible_office.office_at_web is None
        assert document.published_from == datetime.date(1970, 1, 1)
        assert document.index == 40
コード例 #3
0
def test_read_with_specified_version(oereblex_test_config):
    del oereblex_test_config

    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.2.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/1.2.2/geolinks/100.xml',
                  content=f.read())
        source = OEREBlexSource(
            host='http://oereblex.example.com',
            language='de',
            canton='BL',
            version='1.2.2',
            pass_version=True,
            code='ch.Waldabstandslinien',
        )
        source.read(MockParameter(), 100)
        assert len(source.records) == 9
コード例 #4
0
def test_read_with_specified_language(oereblex_test_config):
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.2.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/geolinks/100.xml?locale=fr',
                  content=f.read())
        source = OEREBlexSource(
            host='http://oereblex.example.com',
            language='de',
            canton='BL',
            version='1.2.2',
            code='ch.Waldabstandslinien',
        )
        params = MockParameter()
        params.set_language('fr')
        source.read(params, 100)
        assert len(source.records) == 9
        document = source.records[0]
        assert document.responsible_office.name == {
            'fr': 'Bauverwaltung Gemeinde'
        }
        assert document.text_at_web == {
            'fr': 'http://oereblex.example.com/api/attachments/4735'
        }
        assert document.index == 30
コード例 #5
0
class DatabaseOEREBlexSource(DatabaseSource):
    """
    A source to get models documents attached to public law restrictions in replacement
    of standards documents. Be sure to use a model with an OEREBlex "geolink" integer
    column for plrs that use this source.
    """
    def __init__(self, **kwargs):
        """
        Keyword Arguments:
            name (str): The name. You are free to choose one.
            code (str): The official code. Regarding to the federal specifications.
            geometry_type (str): The geometry type. Possible are: POINT, POLYGON, LINESTRING,
                GEOMETRYCOLLECTION
            thresholds (dict): The configuration of limits and units used for processing.
            text (dict of str): The speaking title. It must be a dictionary containing language (as
                configured) as key and text as value.
            language (str): The language this public law restriction is originally shipped with.
            federal (bool): Switch if it is a federal topic. This will be taken into account in processing
                steps.
            source (dict): The configuration dictionary of the public law restriction
            hooks (dict of str): The hook methods: get_symbol, get_symbol_ref. They have to be provided as
                dotted string for further use with dotted name resolver of pyramid package.
            law_status (dict of str): The multiple match configuration to provide more flexible use of the
                federal specified classifiers "inKraft" or "AenderungMitVorwirkung"
                or "AenderungOhneVorwirkung".
        """
        super(DatabaseOEREBlexSource, self).__init__(**kwargs)
        config = Config.get_oereblex_config()
        config["code"] = self._plr_info.get('code')
        self._oereblex_source = OEREBlexSource(**config)
        self._queried_geolinks = {}

    @staticmethod
    def get_config_value_for_plr_code(url_param_config, plr_code):
        """
        Returns the appropriate configuration entry for a plr within a url_param_config section.
        """
        for url_param_entry in url_param_config:
            if url_param_entry['code'] == plr_code:
                if 'url_param' in url_param_entry:
                    return url_param_entry['url_param']
                else:
                    log.warning(
                        "Incorrect configuration: missing url_param for entry {}"
                        .format(plr_code))
                    return None
        return None

    def get_document_records(self, params, public_law_restriction_from_db):
        """
        Override the parent's get_document_records method to obtain the models document instead.
        """
        oereblex_params = None
        url_param_config = self._oereblex_source._url_param_config
        if url_param_config:
            plr_code = self._theme_record.code
            oereblex_params = DatabaseOEREBlexSource.get_config_value_for_plr_code(
                url_param_config, plr_code)
        return self.document_records_from_oereblex(
            params, public_law_restriction_from_db.geolink, oereblex_params)

    def document_records_from_oereblex(self, params, geolink, oereblex_params):
        """
        Create document records parsed from the OEREBlex response with the specified geoLink ID and appends
        them to the current public law restriction.

        Args:
            params (pyramid_oereb.views.webservice.Parameter): The parameters of the extract request.
            geolink (int): The ID of the GEO-Link to request the documents for.
            oereblex_params (string): URL parameter to add to the models request

        Returns:
            list of pyramid_oereb.core.records.documents.DocumentRecord:
                The documents created from the parsed OEREBlex response.
        """
        log.debug(
            "document_records_from_oereblex() start, GEO-Link {}, oereblex_params {}"
            .format(geolink, oereblex_params))
        identifier = '{}{}'.format(geolink, params.language)
        if identifier in self._queried_geolinks:
            log.debug(
                'skip querying this geolink "{}" because it was fetched already.'
                .format(identifier))
            log.debug('use already queried instead')
        else:
            self._oereblex_source.read(params, geolink, oereblex_params)
            log.debug(
                "document_records_from_oereblex() returning {} records".format(
                    len(self._oereblex_source.records)))
            self._queried_geolinks[identifier] = self._oereblex_source.records
        return self._queried_geolinks[identifier]