Ejemplo n.º 1
0
class DatabaseOEREBlexSource(DatabaseSource):
    """
    A source to get oereblex documents attached to public law restrictions in replacement
    of standards documents. Be sure to use a model with an OEREBlex "lexlink" 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 'inForce' and 'runningModifications'.
        """
        super(DatabaseOEREBlexSource, self).__init__(**kwargs)
        self._oereblex_source = OEREBlexSource(**Config.get_oereblex_config())
        self._queried_lexlinks = {}

    def get_document_records(self, params, public_law_restriction_from_db):
        """
        Override the parent's get_document_records method to obtain the oereblex document instead.
        """
        return self.document_records_from_oereblex(params, public_law_restriction_from_db.geolink)

    def document_records_from_oereblex(self, params, lexlink):
        """
        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.
            lexlink (int): The ID of the geoLink to request the documents for.

        Returns:
            list of pyramid_oereb.lib.records.documents.DocumentRecord:
                The documents created from the parsed OEREBlex response.
        """
        log.debug("document_records_from_oereblex() start")
        if lexlink in self._queried_lexlinks:
            log.debug('skip querying this lexlink "{}" because it was fetched already.'.format(lexlink))
            log.debug('use already queried instead')
            return self._queried_lexlinks[lexlink]
        else:
            self._oereblex_source.read(params, lexlink)
            log.debug("document_records_from_oereblex() returning {} records"
                      .format(len(self._oereblex_source.records)))
            self._queried_lexlinks[lexlink] = self._oereblex_source.records
            return self._queried_lexlinks[lexlink]
def test_read_with_specified_version():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.0.0.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/1.0.0/geolinks/100.xml', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL',
                                pass_version=True, version='1.0.0')
        source.read(100)
        assert len(source.records) == 2
def test_read_with_version_in_url():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.xml', 'rb') as f:
            m.get('http://oereblex.example.com/api/1.1.1/geolinks/100.xml', content=f.read())
        source = OEREBlexSource(host='http://oereblex.example.com', language='de', canton='BL',
                                pass_version=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 2
def test_read_with_specified_language():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.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')
        params = MockParameter()
        params.set_language('fr')
        source.read(params, 100)
        assert len(source.records) == 2
        document = source.records[0]
        assert document.responsible_office.name == {'fr': 'Landeskanzlei'}
        assert document.text_at_web == {
            'fr': 'http://oereblex.example.com/api/attachments/313'
        }
def test_read():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.1.1.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')
        source.read(100)
        assert len(source.records) == 2
        document = source.records[0]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {'de': 'Landeskanzlei'}
        assert document.canton == 'BL'
        assert document.text_at_web == {
            'de': 'http://oereblex.example.com/api/attachments/313'
        }
        assert len(document.references) == 5
def test_read_related_notice_as_main():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.0.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',
                                related_notice_as_main=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 6
        document = source.records[5]
        assert isinstance(document, HintRecord)
        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 len(document.references) == 3
def test_read_related_decree_as_main():
    with requests_mock.mock() as m:
        with open('./tests/resources/geolink_v1.2.0.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',
                                related_decree_as_main=True)
        source.read(MockParameter(), 100)
        assert len(source.records) == 5
        document = source.records[0]
        assert isinstance(document, DocumentRecord)
        assert isinstance(document.responsible_office, OfficeRecord)
        assert document.responsible_office.name == {
            'de': 'Bauverwaltung Gemeinde'
        }
        assert document.canton == 'BL'
        assert document.text_at_web == {
            'de': 'http://oereblex.example.com/api/attachments/4735'
        }
        assert len(document.references) == 4