コード例 #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 __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 = {}
コード例 #4
0
def test_get_mapped_value(key, language, result):
    file_ = File('Test', '/api/attachments/1', 'main')
    document = Document(id='test',
                        title='Test',
                        category='main',
                        doctype='decree',
                        files=[file_],
                        enactment_date=datetime.date.today(),
                        subtype='Liestal',
                        authority='Office')
    source = OEREBlexSource(host='http://oereblex.example.com',
                            language='de',
                            canton='BL',
                            mapping={'municipality': 'subtype'})
    assert source._get_mapped_value(document, key, language=language) == result
コード例 #5
0
def test_authentication():
    auth = {'username': '******', 'password': '******'}
    source = OEREBlexSource(host='http://oereblex.example.com',
                            language='de',
                            canton='BL',
                            auth=auth,
                            code='ch.Waldabstandslinien')
    assert isinstance(source._auth, HTTPBasicAuth)
コード例 #6
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
コード例 #7
0
def test_get_document_records(oereblex_test_config, i, document):
    del oereblex_test_config

    language = 'de'
    source = OEREBlexSource(host='http://oereblex.example.com',
                            language='de',
                            canton='BL',
                            code='ch.Waldabstandslinien')

    if i == 3:
        with pytest.raises(ConfigurationError):
            source._get_document_records(document, language)
    elif i == 4:
        assert source._get_document_records(document, language) == []
    else:
        records = source._get_document_records(document, language)
        assert len(records) == i
        for idx, record in enumerate(records):
            assert isinstance(record, DocumentRecord)
            if i == 1:
                record.document_type == 'GesetzlicheGrundlage'
                assert record.index == 1
            elif i == 2:
                record.document_type == 'Rechtsvorschrift'
                assert record.index == 2
            else:
                assert record.index is None
            assert record.title == {'de': 'Document {0}'.format(i)}
            assert record.published_from == datetime.date.today()
            assert record.text_at_web == {
                'de': '/api/attachments/{fid}'.format(fid=i + idx)
            }
コード例 #8
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
コード例 #9
0
def test_get_multilingual():
    document = Document([], id='1', title='Test')
    result = {'de': 'Test'}
    assert OEREBlexSource._get_multilingual(document.title, 'de') == result
    assert OEREBlexSource._get_multilingual(None, 'de') is None
コード例 #10
0
def test_init(valid, cfg):
    if valid:
        assert isinstance(OEREBlexSource(**cfg), OEREBlexSource)
    else:
        with pytest.raises(AssertionError):
            OEREBlexSource(**cfg)
コード例 #11
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]