199 lines
5.9 KiB
Python
199 lines
5.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
import sys
|
|
|
|
import psycopg2
|
|
|
|
from zoo.models import *
|
|
|
|
Relation.objects.all().delete()
|
|
Entity.objects.all().delete()
|
|
|
|
dbname = sys.argv[1]
|
|
|
|
connection = psycopg2.connect(dbname=dbname)
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
|
|
|
|
cursor = connection.cursor()
|
|
|
|
tr = Transaction.objects.create(meta="initial import")
|
|
|
|
cursor.execute('''SELECT id, gender, firstname, lastname, nameofuse, email, phones::json,
|
|
legalstatus, birthdate, mappings::json
|
|
FROM individual''')
|
|
|
|
individu_batch = []
|
|
|
|
individu_schema = EntitySchema.objects.get(slug='individu')
|
|
adresse_schema = EntitySchema.objects.get(slug='adresse')
|
|
habite_schema = RelationSchema.objects.get(slug='habite')
|
|
responsabilite_legale_schema = RelationSchema.objects.get(slug='responsabilite-legale')
|
|
union_schema = RelationSchema.objects.get(slug='union')
|
|
|
|
|
|
individu_mapping = {}
|
|
|
|
for (individualid, gender, firstname, lastname, nameofuse, email, phones, legalstatus, birthdate,
|
|
mappings) in cursor.fetchall():
|
|
if gender == 'Female':
|
|
genre = 'femme'
|
|
elif gender == 'Male':
|
|
genre = 'homme'
|
|
elif gender == 'Other':
|
|
genre = 'autre'
|
|
else:
|
|
raise NotImplementedError('unknown gender: %s' % gender)
|
|
|
|
if legalstatus == 'Adulte':
|
|
statut_legal = 'majeur'
|
|
elif legalstatus == 'Enfant':
|
|
statut_legal = 'mineur'
|
|
elif legalstatus == 'Emancipe':
|
|
status_legal = 'emancipe'
|
|
else:
|
|
raise NotImplementedError('unknown legalstatus: %s' % legalstatus)
|
|
|
|
telephones = []
|
|
for phone in phones:
|
|
if phone['phoneType'] == 'OtherPhone':
|
|
kind = 'autre'
|
|
elif phone['phoneType'] == 'Pro':
|
|
kind = 'pro'
|
|
elif phone['phoneType'] == 'Mobile':
|
|
kind = 'mobile'
|
|
elif phone['phoneType'] == 'Home':
|
|
kind = 'autre'
|
|
else:
|
|
raise NotImplementedError('unknown phoneType: %s' % phone['phoneType'])
|
|
telephones.append({'type': kind, 'numero': phone['number']})
|
|
|
|
content = {
|
|
'genre': genre,
|
|
'prenoms': firstname.upper(),
|
|
'nom_de_naissance': lastname.upper() if lastname else '',
|
|
'nom_d_usage': nameofuse.upper(),
|
|
'statut_legal': statut_legal,
|
|
'date_de_naissance': birthdate.isoformat(),
|
|
'cles_de_federation': mappings,
|
|
}
|
|
if telephones:
|
|
content['telephones'] = telephones
|
|
if email:
|
|
content['email'] = email
|
|
e = Entity(created=tr, schema=individu_schema, content=content)
|
|
individu_mapping[individualid] = e
|
|
individu_batch.append(e)
|
|
|
|
Entity.objects.bulk_create(individu_batch)
|
|
|
|
adresse_batch = []
|
|
adresse_mapping = {}
|
|
|
|
individu_adresse_mapping = {}
|
|
|
|
# Création des adresses
|
|
cursor.execute('''
|
|
SELECT
|
|
ia.individualid,
|
|
a.id,
|
|
ia.isprimary,
|
|
a.streetnumber,
|
|
a.streetnumberext,
|
|
a.streetname,
|
|
a.streetmatriculation,
|
|
a.ext1,
|
|
a.ext2,
|
|
a.at,
|
|
a.city,
|
|
a.zipcode,
|
|
a.country,
|
|
a.inseecode
|
|
FROM individualaddress as ia, address as a
|
|
WHERE
|
|
ia.addressid = a.id''')
|
|
for individualid, addressid, is_primary, streetnumber, streetnumberext, streetname, streetmatriculation, \
|
|
ext1, ext2, at, city, zipcode, country, inseecode in cursor.fetchall():
|
|
if individualid not in individu_mapping:
|
|
continue
|
|
if addressid not in adresse_mapping:
|
|
content = {
|
|
'streetnumber': streetnumber,
|
|
'streetnumberext': streetnumberext,
|
|
'streetname': streetname,
|
|
'streetmatriculation': streetmatriculation,
|
|
'ext1': ext1,
|
|
'ext2': ext2,
|
|
'at': at,
|
|
'city': city,
|
|
'zipcode': zipcode,
|
|
'country': country,
|
|
'inseecode': inseecode
|
|
}
|
|
e = Entity(created=tr, schema=adresse_schema, content=content)
|
|
adresse_batch.append(e)
|
|
adresse_mapping[addressid] = e
|
|
individu_adresse_mapping[(individualid, addressid)] = is_primary
|
|
|
|
Entity.objects.bulk_create(adresse_batch)
|
|
|
|
relation_batch = []
|
|
for (a, b), is_primary in individu_adresse_mapping.iteritems():
|
|
content = {
|
|
'facturation': is_primary,
|
|
}
|
|
e = Relation(created=tr,
|
|
left=individu_mapping[a],
|
|
right=adresse_mapping[b],
|
|
schema=habite_schema,
|
|
content=content)
|
|
relation_batch.append(e)
|
|
|
|
Relation.objects.bulk_create(relation_batch)
|
|
|
|
# Création des relations entre individus
|
|
cursor.execute('''
|
|
SELECT
|
|
label,
|
|
relationtype,
|
|
responsibleid,
|
|
subjectid
|
|
FROM individualrelation''')
|
|
|
|
relation_batch = []
|
|
|
|
for label, relationtype, responsibleid, subjectid in cursor.fetchall():
|
|
if relationtype == 'SituationFamiliale':
|
|
schema = union_schema
|
|
if label == 'Marie':
|
|
kind = 'mariage'
|
|
elif label == 'Pacse':
|
|
kind = 'pacs'
|
|
elif label == 'UnionLibre':
|
|
kind = 'unionlibre'
|
|
else:
|
|
raise NotImplementedError('unknown label for relationtype: %s, %s' % (label,
|
|
relationtype))
|
|
content = {'statut': kind}
|
|
elif relationtype == 'ResponsabiliteLegale':
|
|
schema = responsabilite_legale_schema
|
|
if label == 'Parent':
|
|
kind = 'parent'
|
|
elif label == 'TiersDeConfiance':
|
|
kind = 'tiers_de_confiance'
|
|
elif label == 'RepresentantPersonneMoraleQualifiee':
|
|
kind = 'representant_personne_morale_qualifiee'
|
|
elif label == 'Tuteur':
|
|
kind = 'tuteur'
|
|
else:
|
|
raise NotImplementedError('unknown label for relationtype: %s, %s' % (label,
|
|
relationtype))
|
|
content = {'statut': kind}
|
|
e = Relation(created=tr, schema=schema, left=individu_mapping[responsibleid],
|
|
right=individu_mapping[subjectid], content=content)
|
|
relation_batch.append(e)
|
|
|
|
Relation.objects.bulk_create(relation_batch)
|
|
|
|
|