def process_postcode(row): insee = row['municipality:insee'] municipality = 'insee:{}'.format(insee) source = row.get('source') attributes = {} if source: attributes = {'source': row.pop('source')} name = row.get('name') code = row.get('postcode') complement = row.get('complement') data = dict(name=name, code=code, municipality=municipality, version=1, attributes=attributes, complement=complement) instance = PostCode.select().join(Municipality).where( PostCode.complement == complement, PostCode.code == code, Municipality.insee == insee).first() if instance: return reporter.notice('PostCode already exists', code) validator = PostCode.validator(**data) if validator.errors: return reporter.error('PostCode errors', (validator.errors, code, insee)) validator.save() reporter.notice('Imported PostCode', code)
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_postcode(row): name = row.get('libelle') municipality = 'insee:{}'.format(row.get('code_insee')) code = row.get('code_post') validator = PostCode.validator(name=name, municipality=municipality, code=code) if validator.errors: reporter.error('Postcode error', validator.errors) else: validator.save() reporter.notice('Postcode saved', code)
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) municipality = 'insee:{}'.format(line[6:11]) code = line[89:94] name = line[90:] validator = PostCode.validator(code=code, name=name, municipality=municipality) if validator.errors: return report('PostCode Error', validator.errors, report.ERROR) else: with PostCode._meta.database.atomic(): try: validator.save() except peewee.IntegrityError: report('Already created', (code, municipality), report.WARNING) else: report('PostCode created', code, report.NOTICE)
def process_postcode(row): insee = row['municipality:insee'] municipality = 'insee:{}'.format(insee) attributes = {'source': row.pop('source')} name = row.get('name') code = row.get('postcode') data = dict(name=name, code=code, municipality=municipality, version=1, attributes=attributes) instance = PostCode.select().join(Municipality).where( PostCode.code == code, Municipality.insee == insee).first() if instance: return reporter.notice('PostCode already exists', code) validator = PostCode.validator(**data) if validator.errors: return reporter.error('PostCode errors', (validator.errors, code, insee)) validator.save() reporter.notice('Imported PostCode', code)