Example #1
0
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)
Example #2
0
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)
Example #3
0
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'
Example #4
0
def process_housenumber(row):
    data = dict(version=1)
    keys = [('numero', 'number'), 'ordinal', 'ign', 'laposte', 'cia']
    populate(keys, row, data)
    fantoir = row.get('group:fantoir')
    cia = row.get('cia')
    insee = row.get('municipality:insee')
    computed_cia = None
    number = row.get('number')
    ordinal = row.get('ordinal')
    source = row.get('source')
    attributes = row.get('attributes', {})
    if source:
        attributes['source'] = source
    data['attributes'] = attributes
    # Only override if key is present (even if value is null).
    if 'postcode:code' in row:
        code = row.get('postcode:code')
        complement = row.get('postcode:complement')
        postcode = PostCode.select().join(Municipality).where(
            PostCode.code == code, Municipality.insee == insee,
            PostCode.complement == complement).first()
        if not postcode:
            reporter.error('HouseNumber postcode not found', (cia, code))
        else:
            data['postcode'] = postcode

    group_ign = row.get('group:ign')
    group_laposte = row.get('group:laposte')
    parent = None
    if fantoir:
        parent = 'fantoir:{}'.format(fantoir)
    elif group_ign:
        parent = 'ign:{}'.format(group_ign)
    elif group_laposte:
        parent = 'laposte:{}'.format(group_laposte)
    if parent:
        try:
            parent = Group.coerce(parent)
        except Group.DoesNotExist:
            reporter.error('Parent given but not found', parent)
            parent = None
        else:
            data['parent'] = parent

    update = False
    instance = None
    ign = row.get('ign')
    laposte = row.get('laposte')
    if cia:
        instance = HouseNumber.first(HouseNumber.cia == cia)
    elif ign:
        instance = HouseNumber.first(HouseNumber.ign == ign)
    elif laposte:
        instance = HouseNumber.first(HouseNumber.laposte == laposte)
    if parent and not instance:
        # Data is not coerced yet, we want None for empty strings.
        ordinal = row.get('ordinal') or None
        instance = HouseNumber.first(HouseNumber.parent == parent,
                                     HouseNumber.number == data['number'],
                                     HouseNumber.ordinal == ordinal)
    if instance:
        attributes = getattr(instance, 'attributes') or {}
        if attributes.get('source') == source:
            # Reimporting same data?
            reporter.warning('HouseNumber already exists',
                             (instance.cia, instance.ign, instance.laposte))
            return
        data['version'] = instance.version + 1
        update = True

    if not instance and not parent:
        reporter.error('No matching instance and missing parent reference',
                       row)
        return

    validator = HouseNumber.validator(instance=instance, update=update, **data)
    if validator.errors:
        reporter.error('HouseNumber errors', (validator.errors, data))
        return
    with HouseNumber._meta.database.atomic():
        try:
            validator.save()
        except peewee.IntegrityError as e:
            reporter.warning('HouseNumber DB error', (data, str(e)))
        else:
            msg = 'HouseNumber Updated' if instance else 'HouseNumber created'
            reporter.notice(msg, data)
Example #5
0
def process_housenumber(row):
    data = dict(version=1)
    keys = [('numero', 'number'), 'ordinal', 'ign', 'laposte', 'cia']
    populate(keys, row, data)
    fantoir = row.get('group:fantoir')
    cia = row.get('cia')
    insee = row.get('municipality:insee')
    computed_cia = None
    if fantoir:
        if not insee:
            insee = fantoir[:5]
        number = data.get('number')
        ordinal = data.get('ordinal')
        computed_cia = compute_cia(insee, fantoir[5:], number, ordinal)
        if data.get('cia'):
            data['cia'] = computed_cia
    source = row.get('source')
    data['attributes'] = {'source': source}
    # Only override if key is present (even if value is null).
    if 'postcode:code' in row:
        code = row.get('postcode:code')
        postcode = PostCode.select().join(Municipality).where(
            PostCode.code == code,
            Municipality.insee == insee).first()
        if not postcode:
            reporter.error('HouseNumber postcode not found', (cia, code))
        else:
            data['postcode'] = postcode

    group_ign = row.get('group:ign')
    group_laposte = row.get('group:laposte')
    parent = None
    if fantoir:
        parent = 'fantoir:{}'.format(fantoir)
    elif group_ign:
        parent = 'ign:{}'.format(group_ign)
    elif group_laposte:
        parent = 'laposte:{}'.format(group_laposte)
    if parent:
        try:
            parent = Group.coerce(parent)
        except Group.DoesNotExist:
            reporter.error('Parent given but not found', parent)
            parent = None
        else:
            data['parent'] = parent

    update = False
    instance = None
    ign = data.get('ign')
    laposte = data.get('laposte')
    if cia:
        instance = HouseNumber.first(HouseNumber.cia == cia)
        if instance and compute_cia:
            if cia != computed_cia:
                # Means new values are changing one of the four values of the
                # cia (insee, fantoir, number, ordinal). Make sure we are not
                # creating a duplicate.
                duplicate = HouseNumber.first(HouseNumber.cia == computed_cia)
                if duplicate:
                    msg = 'Duplicate CIA'
                    reporter.error(msg, (cia, computed_cia))
                    return
    elif ign:
        instance = HouseNumber.first(HouseNumber.ign == ign)
    elif laposte:
        instance = HouseNumber.first(HouseNumber.laposte == laposte)
    if parent and not instance:
        # Data is not coerced yet, we want None for empty strings.
        ordinal = data.get('ordinal') or None
        instance = HouseNumber.first(HouseNumber.parent == parent,
                                     HouseNumber.number == data['number'],
                                     HouseNumber.ordinal == ordinal)
    if instance:
        attributes = getattr(instance, 'attributes') or {}
        if attributes.get('source') == source:
            # Reimporting same data?
            reporter.warning('HouseNumber already exists', instance.cia)
            return
        data['version'] = instance.version + 1
        update = True

    if not instance and not parent:
        reporter.error('No matching instance and missing parent reference',
                       row)
        return

    validator = HouseNumber.validator(instance=instance, update=update, **data)
    if validator.errors:
        reporter.error('HouseNumber errors', (validator.errors, data))
        return
    with HouseNumber._meta.database.atomic():
        try:
            validator.save()
        except peewee.IntegrityError as e:
            reporter.warning('HouseNumber DB error', (data, str(e)))
        else:
            msg = 'HouseNumber Updated' if instance else 'HouseNumber created'
            reporter.notice(msg, data)
Example #6
0
File: init.py Project: odorie/ban
def process_housenumber(row):
    number = row.get('numero')
    ordinal = row.get('ordinal') or None
    fantoir = row.get('group:fantoir')
    cia = row.get('cia')
    if not fantoir and cia:
        # 12xxx.json is missing group:fantoir.
        fantoir = '{}{}'.format(cia[:5], cia[6:10])
    insee = fantoir[:5]
    computed_cia = compute_cia(insee, fantoir[5:], number, ordinal)
    if not cia:
        cia = computed_cia
    parent = 'fantoir:{}'.format(fantoir)
    source = row.get('source')
    attributes = {'source': source}
    data = dict(number=number, ordinal=ordinal, version=1, parent=parent,
                attributes=attributes)
    # Only override if key is present (even if value is null).
    if 'ref:ign' in row:
        data['ign'] = row['ref:ign']
    if 'poste:cea' in row:
        data['laposte'] = row['poste:cea']
    if 'postcode' in row:
        code = row.get('postcode')
        postcode = PostCode.select().join(Municipality).where(
            PostCode.code == code,
            Municipality.insee == insee).first()
        if not postcode:
            reporter.error('HouseNumber postcode not found', (cia, code))
        else:
            data['postcode'] = postcode
    instance = HouseNumber.first(HouseNumber.cia == cia)
    update = False
    if instance:
        if cia != computed_cia:
            # Means new values are changing one of the four values of the cia
            # (insee, fantoir, number, ordinal). Make sure we are not creating
            # a duplicate.
            duplicate = HouseNumber.first(HouseNumber.cia == computed_cia)
            if duplicate:
                msg = 'Duplicate CIA'
                reporter.error(msg, (cia, computed_cia))
                return
        attributes = getattr(instance, 'attributes') or {}
        if attributes.get('source') == source:
            # Reimporting same data?
            reporter.warning('HouseNumber already exists', instance.cia)
            return
        data['version'] = instance.version + 1
        update = True

    validator = HouseNumber.validator(instance=instance, update=update, **data)
    if validator.errors:
        reporter.error('HouseNumber errors', (validator.errors, parent))
        return
    with HouseNumber._meta.database.atomic():
        try:
            validator.save()
        except peewee.IntegrityError:
            reporter.warning('HouseNumber DB error', cia)
        else:
            msg = 'HouseNumber Updated' if instance else 'HouseNumber created'
            reporter.notice(msg, (number, ordinal, parent))