Exemple #1
0
def test_processor_get_legend_entries():
    theme1 = ThemeRecord(u'TEST', {'de': 'Theme 1'})
    theme2 = ThemeRecord(u'TEST', {'de': 'Theme 2'})
    office = OfficeRecord({'de': 'Test Office'})
    law_status = LawStatusRecord.from_config(u'inForce')
    geometries = [
        GeometryRecord(law_status, datetime.date.today(), Point(1, 1))
    ]
    legend1 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend1'},
                                'CodeA',
                                'bla',
                                theme1,
                                view_service_id=1)
    legend2 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend2'},
                                'CodeB',
                                'bla',
                                theme1,
                                view_service_id=1)
    legend3 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend3'},
                                'CodeC',
                                'bla',
                                theme2,
                                view_service_id=1)
    legend4 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend4'},
                                'CodeD',
                                'bla',
                                theme2,
                                view_service_id=1)
    view_service1 = ViewServiceRecord(
        'http://www.test1.url.ch',
        1,
        1.0,
        legend_at_web={'de': 'http://www.test1.url.ch'},
        legends=[legend1, legend2])
    view_service2 = ViewServiceRecord(
        'http://www.test2.url.ch',
        1,
        1.0,
        legend_at_web={'de': 'http://www.test2.url.ch'},
        legends=[legend3, legend4])
    image = ImageRecord('1'.encode('utf-8'))
    plr1 = PlrRecord(
        theme1,
        {'de': 'CONTENT'},
        law_status,
        datetime.datetime.now(),
        office,
        image,
        view_service1,
        geometries,
        type_code='CodeA',
    )
    plr2 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service1,
                     geometries,
                     type_code='CodeB')
    plr3 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service2,
                     geometries,
                     type_code='CodeB')
    plr4 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service2,
                     geometries,
                     type_code='CodeB')

    inside_plrs = [plr1]
    outside_plrs = [plr2, plr3]
    after_process = Processor.get_legend_entries(inside_plrs, outside_plrs)
    assert len(inside_plrs) == len(after_process)
    inside_plrs = [plr3]
    outside_plrs = [plr4]
    after_process = Processor.get_legend_entries(inside_plrs, outside_plrs)
    assert len(after_process) == 1
def test_invalid_document_type():
    office_record = OfficeRecord({'en': 'name'})
    with pytest.raises(AttributeError):
        DocumentRecord('invalid', 'runningModifications',
                       datetime.date(1985, 8, 29), {'en': 'title'},
                       office_record)
def test_render(parameter, glossaries_input, glossaries_expected):
    date = datetime.datetime.now()
    with pyramid_oereb_test_config():
        view_service = ViewServiceRecord(u'http://geowms.bl.ch', 1, 1.0,
                                         {'de': u'http://geowms.bl.ch'}, None)
        real_estate = RealEstateRecord(
            u'RealEstate', u'BL', u'Liestal', 2829, 11395,
            MultiPolygon([Polygon([(0, 0), (1, 1),
                                   (1, 0)])]), u'http://www.geocat.ch',
            u'1000', u'BL0200002829', u'CH775979211712')
        real_estate.set_view_service(view_service)
        real_estate.set_main_page_view_service(view_service)
        office_record = OfficeRecord({'de': u'AGI'})
        resolver = DottedNameResolver()
        date_method_string = Config.get('extract').get('base_data').get(
            'methods').get('date')
        date_method = resolver.resolve(date_method_string)
        av_update_date = date_method(real_estate)
        base_data = Config.get_base_data(av_update_date)

        av_provider_method_string = Config.get('extract').get('base_data').get(
            'methods').get('provider')
        av_provider_method = resolver.resolve(av_provider_method_string)
        cadaster_state = date
        theme = ThemeRecord(u'TEST', {'de': u'TEST TEXT'})
        datasources = [DatasourceRecord(theme, date, office_record)]
        plr_cadastre_authority = Config.get_plr_cadastre_authority()
        embeddable = EmbeddableRecord(cadaster_state, plr_cadastre_authority,
                                      av_provider_method(real_estate),
                                      av_update_date, datasources)
        extract = ExtractRecord(
            real_estate,
            ImageRecord('1'.encode('utf-8')),
            ImageRecord('2'.encode('utf-8')),
            ImageRecord('3'.encode('utf-8')),
            ImageRecord('4'.encode('utf-8')),
            office_record,
            base_data,
            embeddable,
            exclusions_of_liability=[
                ExclusionOfLiabilityRecord({'de': u'Haftungsausschluss'},
                                           {'de': u'Test'})
            ],
            glossaries=glossaries_input,
            general_information={'de': u'Allgemeine Informationen'},
            certification={'de': u'certification'},
            certification_at_web={'de': u'certification_at_web'},
        )
        extract.qr_code = '1'.encode('utf-8')
        extract.electronic_signature = 'Signature'
        renderer = Renderer(DummyRenderInfo())
        renderer._language = u'de'
        renderer._request = MockRequest()
        if parameter is None:
            with pytest.raises(TypeError):
                renderer._render(extract, None)
        else:
            result = renderer._render(extract, parameter)
            assert isinstance(result, dict)
            expected = {
                'ExtractIdentifier':
                extract.extract_identifier,
                'CreationDate':
                Base.date_time(extract.creation_date),
                'ConcernedTheme': [],
                'NotConcernedTheme': [],
                'ThemeWithoutData': [],
                'isReduced':
                True,
                'PLRCadastreAuthority':
                renderer.format_office(office_record),
                'BaseData':
                renderer.get_multilingual_text(
                    Config.get_base_data(av_update_date)),
                'RealEstate':
                renderer.format_real_estate(real_estate),
                'Certification': [{
                    'Language': 'de',
                    'Text': 'certification'
                }],
                'CertificationAtWeb': [{
                    'Language': 'de',
                    'Text': 'certification_at_web'
                }],
                'GeneralInformation': [{
                    'Language': 'de',
                    'Text': 'Allgemeine Informationen'
                }],
                'QRCode':
                '1'.encode('utf-8'),
                'ExclusionOfLiability': [{
                    'Title': [{
                        'Language': 'de',
                        'Text': 'Haftungsausschluss'
                    }],
                    'Content': [{
                        'Language': 'de',
                        'Text': 'Test'
                    }]
                }],
                'ElectronicSignature':
                'Signature'
            }
            if glossaries_expected:
                expected['Glossary'] = glossaries_expected
            if parameter.images:
                expected.update({
                    'LogoPLRCadastre':
                    base64.b64encode('1'.encode('utf-8')).decode('ascii'),
                    'FederalLogo':
                    base64.b64encode('2'.encode('utf-8')).decode('ascii'),
                    'CantonalLogo':
                    base64.b64encode('3'.encode('utf-8')).decode('ascii'),
                    'MunicipalityLogo':
                    base64.b64encode('4'.encode('utf-8')).decode('ascii'),
                })
            else:
                expected.update({
                    'LogoPLRCadastreRef':
                    u'http://example.com/image/logo/oereb/de',
                    'FederalLogoRef':
                    u'http://example.com/image/logo/confederation/de',
                    'CantonalLogoRef':
                    u'http://example.com/image/logo/canton/de',
                    'MunicipalityLogoRef':
                    u'http://example.com/image/municipality/2829'
                })
            assert result == expected
Exemple #4
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 'inForce' and 'runningModifications'.
        """
        models_path = kwargs.get('source').get('params').get('models')
        bds_kwargs = {
            'model':
            DottedNameResolver().maybe_resolve(
                '{models_path}.Geometry'.format(models_path=models_path)),
            'db_connection':
            kwargs.get('source').get('params').get('db_connection')
        }

        BaseDatabaseSource.__init__(self, **bds_kwargs)
        PlrBaseSource.__init__(self, **kwargs)

        self.legend_entry_model = DottedNameResolver().maybe_resolve(
            '{models_path}.LegendEntry'.format(models_path=models_path))
        availability_model = DottedNameResolver().maybe_resolve(
            '{models_path}.Availability'.format(models_path=models_path))
        data_integration_model = DottedNameResolver().maybe_resolve(
            '{models_path}.DataIntegration'.format(models_path=models_path))

        self._theme_record = ThemeRecord(self._plr_info.get('code'),
                                         self._plr_info.get('text'))

        self.availabilities = []
        self.datasource = []

        session = self._adapter_.get_session(self._key_)

        try:

            availabilities_from_db = session.query(availability_model).all()
            for availability in availabilities_from_db:
                self.availabilities.append(
                    AvailabilityRecord(availability.fosnr,
                                       available=availability.available))

            data_integration = session.query(data_integration_model).all()
            for source in data_integration:
                self.datasource.append(
                    DatasourceRecord(self._theme_record, source.date,
                                     OfficeRecord(source.office.name)))

        finally:
            session.close()
                })
            else:
                expected.update({
                    'SymbolRef':
                    'http://example.com/image/symbol/{theme}/{view_service_id}/{code}'
                    .format(theme='ContaminatedSites',
                            view_service_id=1,
                            code='CodeA')
                })
            assert result[0] == expected


@pytest.mark.parametrize('document,result_dict', [
    (LegalProvisionRecord(
        law_status(), datetime.date.today(), {'de': 'Test Rechtsvorschrift'},
        OfficeRecord({'de': 'AGI'
                      }), {'de': 'http://meine.rechtsvorschrift.ch'},
        {'de': 'Test'}, 'rv.test.1', {'de': 'Rechtsvorschrift Test'}, 'BL',
        'Liestal', ['Art.1', 'Art.2', 'Art.3'], '1'.encode('utf-8'),
        [ArticleRecord(law_status(), datetime.date.today(), 'art.1')], [
            DocumentRecord('Law', law_status(), datetime.date.today(),
                           {'de': 'Test Dokument'}, OfficeRecord({
                               'de': 'BUD'
                           }), {'de': 'http://mein.dokument.ch'})
        ]), {
            'DocumentType':
            'LegalProvision',
            'Lawstatus': {
                'Code': 'inForce',
                'Text': {
                    'Language': 'de',
                    'Text': 'In Kraft'
def test_format_plr(parameter):
    with pyramid_oereb_test_config():
        renderer = Renderer(DummyRenderInfo())
        renderer._language = 'de'
        renderer._params = parameter
        renderer._request = MockRequest()
        document = DocumentRecord('Law', law_status(), datetime.date.today(),
                                  {u'de': u'Test Dokument'},
                                  OfficeRecord({u'de': u'BUD'}),
                                  {'de': 'http://mein.dokument.ch'})
        if parameter.flavour == 'reduced':
            documents = [document]
        else:
            documents = None
        theme = ThemeRecord(u'ContaminatedSites', {u'de': u'Test theme'})
        office = OfficeRecord({'de': 'Test Office'})
        legend_entry = LegendEntryRecord(ImageRecord(
            base64.b64encode('1'.encode('utf-8'))), {'de': 'Test'},
                                         'CodeA',
                                         'TypeCodeList',
                                         theme,
                                         view_service_id=1)
        view_service = ViewServiceRecord('http://geowms.bl.ch', 1, 1.0,
                                         {'de': u'http://geowms.bl.ch'},
                                         [legend_entry])
        geometry = GeometryRecord(law_status(), datetime.date.today(),
                                  Point(1, 1))
        plr = PlrRecord(theme, {'de': 'Test'},
                        law_status(),
                        datetime.date.today(),
                        office,
                        ImageRecord('1'.encode('utf-8')),
                        view_service, [geometry],
                        sub_theme={'de': 'Subtopic'},
                        other_theme='Additional topic',
                        type_code='CodeA',
                        type_code_list='TypeCodeList',
                        documents=documents,
                        view_service_id=1)
        plr.part_in_percent = 0.5
        if parameter.flavour == 'full':
            with pytest.raises(ValueError):
                renderer.format_plr([plr])
        else:
            result = renderer.format_plr([plr])
            assert isinstance(result, list)
            assert len(result) == 1
            assert isinstance(result[0], dict)
            expected = {
                'Information': renderer.get_multilingual_text(plr.information),
                'Theme': renderer.format_theme(plr.theme),
                'Lawstatus': {
                    'Code': 'inForce',
                    'Text': {
                        'Language': 'de',
                        'Text': 'In Kraft'
                    }
                },
                'ResponsibleOffice':
                renderer.format_office(plr.responsible_office),
                'Map': renderer.format_map(plr.view_service),
                'SubTheme': 'Subtopic',
                'OtherTheme': 'Additional topic',
                'TypeCode': 'CodeA',
                'TypeCodelist': 'TypeCodeList',
                'LegalProvisions': [renderer.format_document(document)],
                'PartInPercent': 0.5
            }
            if parameter.images:
                expected.update({
                    'Symbol':
                    base64.b64encode('1'.encode('utf-8')).decode('ascii')
                })
            else:
                expected.update({
                    'SymbolRef':
                    'http://example.com/image/symbol/{theme}/{view_service_id}/{code}'
                    .format(theme='ContaminatedSites',
                            view_service_id=1,
                            code='CodeA')
                })
            assert result[0] == expected
Exemple #7
0
    def _get_document_records(self, document, language, references=None):
        """
        Converts the received documents into records.

        Args:
            document (geolink_formatter.entity.Document): The geoLink document to be returned as document
                record.
            language (str): The language of the returned documents.
            references (list of geolink_formatter.entity.Document): Referenced geoLink documents.

        Returns:
            list of pyramid_oereb.lib.records.documents.DocumentRecord: The converted record.
        """

        references = references or list()

        # Cancel if document contains no files
        if len(document.files) == 0:
            log.warning('Document with OEREBlex ID {0} has been skipped because of missing file.'.format(
                document.id
            ))
            return []

        enactment_date = document.enactment_date
        authority = document.authority
        if document.doctype == 'notice':
            # Oereblex notices documents can have no enactment_date while it is require by pyramid_oereb to
            # have one. Add a fake default one that is identifiable and always older than now (01.0.1.1970).
            if enactment_date is None:
                enactment_date = datetime.date(1970, 1, 1)
            # Oereblex notices documents can have no `authority` while it is require by pyramid_oereb to
            # have one. Replace None by '-' in this case.
            if authority is None:
                authority = '-'

        # Cancel if enactment_date is not set
        if enactment_date is None:
            log.warning('Document with OEREBlex ID {0} has been skipped because of missing enactment_date.'
                        .format(document.id))
            return []

        # Check mandatory attributes
        if document.title is None:
            raise AssertionError('Missing title for document #{0}'.format(document.id))
        if authority is None:
            raise AssertionError('Missing authority for document #{0}'.format(document.id))

        # Get document type
        if document.doctype == 'decree':
            document_class = LegalProvisionRecord
        elif document.doctype == 'edict':
            document_class = LawRecord
        elif document.doctype == 'notice':
            document_class = HintRecord
        else:
            raise TypeError('Wrong doctype: expected decree, edict or notice, got {0}'.format(
                document.doctype
            ))

        # Convert referenced documents
        referenced_records = []
        for reference in references:
            referenced_records.extend(self._get_document_records(reference, language))

        # Create related office record
        office = OfficeRecord({language: authority}, office_at_web=document.authority_url)

        # Check for available abbreviation
        abbreviation = {language: document.abbreviation} if document.abbreviation else None

        # Get files
        records = []
        for f in document.files:
            arguments = {'law_status': LawStatusRecord.from_config(u'inForce'),
                         'published_from': enactment_date,
                         'title': self._get_document_title(document, f, language),
                         'responsible_office': office,
                         'text_at_web': {language: f.href},
                         'abbreviation': abbreviation,
                         'official_number': document.number,
                         'official_title': self._get_mapped_value(
                             document,
                             'official_title',
                             language=language
                         ),
                         'canton': self._canton,
                         'municipality': self._get_mapped_value(document, 'municipality'),
                         'references': referenced_records if len(referenced_records) > 0 else None}
            records.append(document_class(**arguments))

        return records
Exemple #8
0
    def _get_document_records(self, document, references=list()):
        """
        Converts the received documents into records.

        Args:
            document (geolink_formatter.entity.Document): The geoLink document to be returned as document
                record.
            references (list of geolink_formatter.entity.Document): Referenced geoLink documents.

        Returns:
            list of pyramid_oereb.lib.records.documents.DocumentRecord: The converted record.
        """

        # Cancel if document contains no files
        if len(document.files) == 0:
            log.warning(
                'Document with OEREBlex ID {0} has been skipped because of missing file.'
                .format(document.id))
            return []

        # Check mandatory attributes
        assert document.title is not None
        assert document.enactment_date is not None
        assert document.authority is not None

        # Get document type
        if document.doctype == 'decree':
            document_class = LegalProvisionRecord
        elif document.doctype == 'edict':
            document_class = LawRecord
        else:
            raise TypeError(
                'Wrong doctype: expected decree or edict, got {0}'.format(
                    document.doctype))

        # Convert referenced documents
        referenced_records = []
        for reference in references:
            referenced_records.extend(self._get_document_records(reference))

        # Create related office record
        office = OfficeRecord({self._language: document.authority},
                              office_at_web=document.authority_url)

        # Check for available abbreviation
        abbreviation = {
            self._language: document.abbreviation
        } if document.abbreviation else None

        # Get files
        records = []
        for f in document.files:
            arguments = {
                'law_status':
                LawStatusRecord.from_config(u'inForce'),
                'published_from':
                document.enactment_date,
                'title':
                self._get_document_title(document, f),
                'responsible_office':
                office,
                'text_at_web': {
                    self._language: f.href
                },
                'abbreviation':
                abbreviation,
                'official_number':
                document.number,
                'official_title':
                self._get_mapped_value(document, 'official_title', True),
                'canton':
                self._canton,
                'municipality':
                self._get_mapped_value(document, 'municipality'),
                'references':
                referenced_records if len(referenced_records) > 0 else None
            }
            records.append(document_class(**arguments))

        return records
Exemple #9
0
def test_future_document(law_status):
    office_record = OfficeRecord({'en': 'name'})
    record = DocumentRecord('Hint', law_status,
                            (datetime.datetime.now().date() + datetime.timedelta(days=7)), {'en': 'title'},
                            office_record, {'en': 'http://my.document.com'})
    assert not record.published
                        theme='ContaminatedSites',
                        text=base64.b64encode(
                            json.dumps(legend_entry.legend_text).encode('utf-8')
                        ).decode('ascii').replace('=', '%3D'),
                        code='test'
                    )
                })
            assert result[0] == expected


@pytest.mark.parametrize('document,result_dict', [
    (LegalProvisionRecord(
        law_status(),
        datetime.date.today(),
        {'de': 'Test Rechtsvorschrift'},
        OfficeRecord({'de': 'AGI'}),
        {'de': 'http://meine.rechtsvorschrift.ch'},
        {'de': 'Test'},
        'rv.test.1',
        {'de': 'Rechtsvorschrift Test'},
        'BL', 'Liestal',
        ['Art.1', 'Art.2', 'Art.3'],
        '1'.encode('utf-8'), [
            ArticleRecord(law_status(), datetime.date.today(), 'art.1')
        ], [
            DocumentRecord(law_status(), datetime.date.today(), {'de': 'Test Dokument'},
                           OfficeRecord({'de': 'BUD'}), {'de': 'http://mein.dokument.ch'})
        ]), {
            'Lawstatus': {
                'Code': 'inForce',
                'Text': {'Language': 'de', 'Text': 'In Kraft'}
def test_processor_get_legend_entries(law_status):
    theme1 = ThemeRecord(u'TEST', {'de': 'Theme 1'})
    theme2 = ThemeRecord(u'TEST', {'de': 'Theme 2'})
    office = OfficeRecord({'de': 'Test Office'})
    geometries = [
        GeometryRecord(law_status, datetime.date.today(), Point(1, 1))
    ]
    legend1 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend1'}, u'type1', u'bla', theme1)
    legend2 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend2'}, u'type2', u'bla', theme1)
    legend3 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend3'}, u'type3', u'bla', theme2)
    legend4 = LegendEntryRecord(ImageRecord('1'.encode('utf-8')),
                                {'de': 'legend4'}, u'type4', u'bla', theme2)
    view_service1 = ViewServiceRecord('http://www.test1.url.ch',
                                      'http://www.test1.url.ch',
                                      legends=[legend1, legend2])
    view_service2 = ViewServiceRecord('http://www.test2.url.ch',
                                      'http://www.test2.url.ch',
                                      legends=[legend3, legend4])
    image = ImageRecord('1'.encode('utf-8'))
    plr1 = PlrRecord(
        theme1,
        {'de': 'CONTENT'},
        law_status,
        datetime.datetime.now(),
        office,
        image,
        view_service1,
        geometries,
        type_code=u'type1',
    )
    plr2 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service1,
                     geometries,
                     type_code=u'type2')
    plr3 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service2,
                     geometries,
                     type_code=u'type2')
    plr4 = PlrRecord(theme1, {'de': 'CONTENT'},
                     law_status,
                     datetime.datetime.now(),
                     office,
                     image,
                     view_service2,
                     geometries,
                     type_code=u'type2')

    inside_plrs = [plr1]
    outside_plrs = [plr2, plr3]
    after_process = Processor.get_legend_entries(inside_plrs, outside_plrs)
    assert len(inside_plrs) == len(after_process)
    inside_plrs = [plr3]
    outside_plrs = [plr4]
    after_process = Processor.get_legend_entries(inside_plrs, outside_plrs)
    assert len(after_process) == 1
def test_mandatory_fields():
    with pytest.raises(TypeError):
        OfficeRecord()
def test_init():
    record = OfficeRecord({'de': 'Test'}, uid='ch99', postal_code=4123)
    assert record.name.get('de') == 'Test'
    assert record.line1 is None
    assert isinstance(record.postal_code, int)