def process_municipality(row): row['attributes'] = {'source': row.pop('source')} validator = Municipality.validator(**row) if validator.errors: return reporter.error('Municipality errors', validator.errors) validator.save() reporter.notice('Imported Municipality', row['insee'])
def process_row(metadata): name = metadata.get('name') id = metadata.get('id') insee = metadata.get('citycode') code = metadata.get('postcode') fantoir = ''.join(id.split('_')[:2])[:9] kind = metadata['type'] klass = Street if kind == 'street' else Locality instance = klass.select().where(klass.fantoir == fantoir).first() if instance: return report('Existing {}'.format(klass.__name__), { name: name, fantoir: fantoir }, report.WARNING) try: municipality = Municipality.get(Municipality.insee == insee) except Municipality.DoesNotExist: return report('Municipality does not exist', insee, report.ERROR) validator = PostCode.validator(code=code, version=1, name=municipality.name, municipality=municipality) if validator.errors: report('Invalid postcode', code, report.ERROR) postcode = None else: with PostCode._meta.database.atomic(): try: postcode = validator.save() except peewee.IntegrityError: # Another thread created it? postcode = PostCode.get(PostCode.code == code) else: report('Created postcode', code, report.NOTICE) data = dict( name=name, fantoir=fantoir, municipality=municipality.id, version=1, ) validator = klass.validator(**data) if not validator.errors: item = validator.save() report(kind, item, report.NOTICE) housenumbers = metadata.get('housenumbers') if housenumbers: for id, metadata in housenumbers.items(): add_housenumber(item, id, metadata, postcode) else: report('Street error', validator.errors, report.ERROR)
def process_row(metadata): name = metadata.get('name') id = metadata.get('id') insee = metadata.get('citycode') code = metadata.get('postcode') fantoir = ''.join(id.split('_')[:2])[:9] kind = 'area' if metadata['type'] == 'locality' else 'way' instance = Group.select().where(Group.fantoir == fantoir).first() if instance: return report('Existing {}'.format(Group.__name__), {name: name, fantoir: fantoir}, report.WARNING) try: municipality = Municipality.get(Municipality.insee == insee) except Municipality.DoesNotExist: return report('Municipality does not exist', insee, report.ERROR) validator = PostCode.validator(code=code, version=1, name=municipality.name, municipality=municipality) if validator.errors: report('Invalid postcode', code, report.ERROR) postcode = None else: with PostCode._meta.database.atomic(): try: postcode = validator.save() except peewee.IntegrityError: # Another thread created it? postcode = PostCode.get(PostCode.code == code) else: report('Created postcode', code, report.NOTICE) validator = Group.validator(name=name, fantoir=fantoir, kind=kind, municipality=municipality.pk, version=1) if not validator.errors: try: item = validator.save() except peewee.IntegrityError: return report('Duplicate group', fantoir, report.ERROR) report(kind, item, report.NOTICE) housenumbers = metadata.get('housenumbers') if housenumbers: for id, metadata in housenumbers.items(): add_housenumber(item, id, metadata, postcode) else: report('Street error', validator.errors, report.ERROR)
def process_postcode(line): if line[50] != 'M': return report('Cedex postcode', line, report.WARNING) insee = line[6:11] code = line[89:94] name = line[90:] try: municipality = Municipality.get(Municipality.insee == insee) except Municipality.DoesNotExist: return report('Municipality Not Existing', insee, report.WARNING) postcode, created = PostCode.get_or_create(code=code, name=name, municipality=municipality, defaults={'version': 1}) if created: report('PostCode Added', postcode, report.NOTICE)
def process_postcode(line): if line[50] != 'M': return report('Cedex postcode', line, report.WARNING) insee = line[6:11] code = line[89:94] try: municipality = Municipality.get(Municipality.insee == insee) except Municipality.DoesNotExist: return report('Municipality Not Existing', insee, report.WARNING) postcode, created = PostCode.get_or_create(code=code, version=1) if created: report('PostCode Added', postcode, report.NOTICE) try: postcode.municipalities.add(municipality) except peewee.IntegrityError: report('Association Already Exists', postcode, report.WARNING) else: report('Association Added', postcode, report.NOTICE)
def process_row(metadata): name = metadata.get('name') id = metadata.get('id') insee = metadata.get('citycode') fantoir = ''.join(id.split('_')[:2])[:9] kind = metadata['type'] klass = Street if kind == 'street' else Locality instance = klass.select().where(klass.fantoir == fantoir).first() if instance: return report('Existing {}'.format(klass.__name__), {name: name, fantoir: fantoir}, report.WARNING) try: municipality = Municipality.get(Municipality.insee == insee) except Municipality.DoesNotExist: return report('Municipality does not exist', insee, report.ERROR) data = dict( name=name, fantoir=fantoir, municipality=municipality.id, version=1, ) validator = klass.validator(**data) if not validator.errors: item = validator.save() report(kind, item, report.NOTICE) housenumbers = metadata.get('housenumbers') if housenumbers: for id, metadata in housenumbers.items(): add_housenumber(item, id, metadata) else: report('Street error', validator.errors, report.ERROR)
from pathlib import Path import os import peewee from ban.commands import command from ban.core.encoder import dumps from ban.core.models import (Group, HouseNumber, Municipality, Position, PostCode) from ban.db import database from . import helpers QUERIES = { 'PostCode': PostCode.select(), 'Municipality': Municipality.select(), 'Group': Group.select(), 'HouseNumber': HouseNumber.select(), 'Position': Position.select() } @command def resources(resource, path, **kwargs): """Export database as resources in json stream format. path path of file where to write resources resource Municipality, PostCode, Group, HouseNumber or Position """ resources = [ 'Municipality', 'PostCode', 'Group', 'HouseNumber', 'Position'