remove some magic strings
This commit is contained in:
parent
69d42f2bf4
commit
cb07c0a6f6
|
@ -52,8 +52,8 @@ def individu_to_response(individu, add_text=False):
|
|||
d['enfants'] = [individu_to_response(enfant) for enfant in individu.enfants]
|
||||
if hasattr(individu, 'parents'):
|
||||
d['parents'] = [individu_to_response(parent) for parent in individu.parents]
|
||||
if hasattr(individu, 'union'):
|
||||
d['union'] = individu_to_response(individu.union)
|
||||
if hasattr(individu, utils.UNION_REL):
|
||||
d[utils.UNION_REL] = individu_to_response(individu.union)
|
||||
d['union_statut'] = individu.union_statut
|
||||
if add_text:
|
||||
d['text'] = d['nom_de_naissance'] + ' '
|
||||
|
@ -128,7 +128,7 @@ class ReseauView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
utils.PersonSearch.decorate_individu(individu)
|
||||
return Response({
|
||||
'err': 0,
|
||||
|
@ -145,7 +145,7 @@ class ReseauView(APIView):
|
|||
'left_relations__right__right_relations__schema',
|
||||
'left_relations__right__right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
utils.PersonSearch.decorate_individu(individu)
|
||||
serializer = CreateIndividuSerializer(data=request.data, partial=True)
|
||||
if not serializer.is_valid():
|
||||
|
@ -153,12 +153,12 @@ class ReseauView(APIView):
|
|||
'err': 1,
|
||||
'errors': serializer.errors,
|
||||
}, status=400)
|
||||
if (serializer.validated_data.get('adresse')
|
||||
if (serializer.validated_data.get(utils.ADRESSE_ENT)
|
||||
and individu.content['statut_legal'] == 'mineur'):
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'adresse': 'adresse is forbidden for non adult persons',
|
||||
utils.ADRESSE_ENT: 'adresse is forbidden for non adult persons',
|
||||
},
|
||||
}, status=400)
|
||||
transaction = Transaction.get_transaction()
|
||||
|
@ -185,9 +185,9 @@ class ReseauView(APIView):
|
|||
if 'telephones' in v:
|
||||
individu.content['telephones'] = v['telephones']
|
||||
individu.save()
|
||||
if individu.content['statut_legal'] == 'majeur' and 'adresse' in v:
|
||||
if individu.content['statut_legal'] == 'majeur' and utils.ADRESSE_ENT in v:
|
||||
for adresse, rel in utils.adresses(individu):
|
||||
adresse.content = v['adresse']
|
||||
adresse.content = v[utils.ADRESSE_ENT]
|
||||
rel.modified = individu.modified
|
||||
adresse.save()
|
||||
rel.save()
|
||||
|
@ -257,9 +257,9 @@ class CreateIndividu(APIView):
|
|||
'err': 1,
|
||||
'errors': serializer.errors,
|
||||
}, status=400)
|
||||
schema = EntitySchema.objects.get(slug='individu')
|
||||
schema_adresse = EntitySchema.objects.get(slug='adresse')
|
||||
habite_schema = RelationSchema.objects.get(slug='habite')
|
||||
schema = EntitySchema.objects.get(slug=utils.INDIVIDU_ENT)
|
||||
schema_adresse = EntitySchema.objects.get(slug=utils.ADRESSE_ENT)
|
||||
habite_schema = RelationSchema.objects.get(slug=utils.HABITE_REL)
|
||||
v = serializer.validated_data
|
||||
v['adresse_inconnnue'] = False # new individual always have known adresses
|
||||
cles_de_federation = {}
|
||||
|
@ -280,7 +280,7 @@ class CreateIndividu(APIView):
|
|||
'cles_de_federation': cles_de_federation,
|
||||
'email': v['email'],
|
||||
})
|
||||
content = v['adresse'].copy()
|
||||
content = v[utils.ADRESSE_ENT].copy()
|
||||
content['adresse_inconnnue'] = False
|
||||
individu.save()
|
||||
adresse = Entity.objects.create(
|
||||
|
@ -340,7 +340,7 @@ class DeclarationResponsabiliteLegale(APIView):
|
|||
)
|
||||
individu = get_object_or_404(
|
||||
qs,
|
||||
schema__slug='individu',
|
||||
schema__slug=utils.INDIVIDU_ENT,
|
||||
content__statut_legal='majeur',
|
||||
id=identifier)
|
||||
serializer = DeclarationResponsabiliteLegaleSerializer(data=request.data)
|
||||
|
@ -366,9 +366,10 @@ class DeclarationResponsabiliteLegale(APIView):
|
|||
}, status=400)
|
||||
|
||||
transaction = Transaction.get_transaction()
|
||||
individu_schema = EntitySchema.objects.get(slug='individu')
|
||||
responsabilite_legale_schema = RelationSchema.objects.get(slug='responsabilite-legale')
|
||||
habite_schema = RelationSchema.objects.get(slug='habite')
|
||||
individu_schema = EntitySchema.objects.get(slug=utils.INDIVIDU_ENT)
|
||||
responsabilite_legale_schema = RelationSchema.objects.get(
|
||||
slug=utils.RESPONSABILITE_LEGALE_REL)
|
||||
habite_schema = RelationSchema.objects.get(slug=utils.HABITE_REL)
|
||||
adresse = list(utils.adresses(individu))[0][0]
|
||||
|
||||
if v.get('enfant_id'): # existing child
|
||||
|
@ -400,7 +401,7 @@ class DeclarationResponsabiliteLegale(APIView):
|
|||
'cles_de_federation': {},
|
||||
})
|
||||
# verify child has not already two parents
|
||||
if enfant.right_relations.filter(schema__slug='responsabilite-legale').count() > 1:
|
||||
if enfant.right_relations.filter(schema__slug=utils.RESPONSABILITE_LEGALE_REL).count() > 1:
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
|
@ -409,7 +410,7 @@ class DeclarationResponsabiliteLegale(APIView):
|
|||
}, status=400)
|
||||
|
||||
# verify child is not already linked to this parent
|
||||
if enfant.right_relations.filter(schema__slug='responsabilite-legale',
|
||||
if enfant.right_relations.filter(schema__slug=utils.RESPONSABILITE_LEGALE_REL,
|
||||
left=individu).exists():
|
||||
return Response({
|
||||
'err': 1,
|
||||
|
@ -464,13 +465,13 @@ class DeclarationUnion(APIView):
|
|||
|
||||
try:
|
||||
individu1 = Entity.objects.get(id=v['individu_id_1'],
|
||||
schema__slug='individu')
|
||||
schema__slug=utils.INDIVIDU_ENT)
|
||||
except Entity.DoesNotExist:
|
||||
individu1 = None
|
||||
|
||||
try:
|
||||
individu2 = Entity.objects.get(id=v['individu_id_2'],
|
||||
schema__slug='individu')
|
||||
schema__slug=utils.INDIVIDU_ENT)
|
||||
except Entity.DoesNotExist:
|
||||
individu2 = None
|
||||
|
||||
|
@ -483,7 +484,7 @@ class DeclarationUnion(APIView):
|
|||
'err': 1,
|
||||
'errors': errors,
|
||||
})
|
||||
qs = Relation.objects.filter(schema__slug='union')
|
||||
qs = Relation.objects.filter(schema__slug=utils.UNION_REL)
|
||||
already1 = qs.filter(Q(left=individu1) | Q(right=individu1)).exists()
|
||||
already2 = qs.filter(Q(left=individu2) | Q(right=individu2)).exists()
|
||||
|
||||
|
@ -497,7 +498,7 @@ class DeclarationUnion(APIView):
|
|||
'errors': errors,
|
||||
})
|
||||
|
||||
union_schema = RelationSchema.objects.get(slug='union')
|
||||
union_schema = RelationSchema.objects.get(slug=utils.UNION_REL)
|
||||
Relation.objects.create(
|
||||
schema=union_schema,
|
||||
left=individu1,
|
||||
|
@ -509,18 +510,18 @@ class DeclarationUnion(APIView):
|
|||
adresse1 = list(utils.adresses(individu1))[0][0]
|
||||
adresse2 = list(utils.adresses(individu2))[0][0]
|
||||
if v['adresse_commune'] == 1:
|
||||
qs = Relation.objects.filter(schema__slug='habite', right=adresse1)
|
||||
qs = Relation.objects.filter(schema__slug=utils.HABITE_REL, right=adresse1)
|
||||
entities = Entity.objects.filter(left_relations__in=qs)
|
||||
|
||||
qs = Relation.objects.filter(schema__slug='habite')
|
||||
qs = Relation.objects.filter(schema__slug=utils.HABITE_REL)
|
||||
qs = qs.exclude(left__in=entities)
|
||||
qs.filter(right=adresse2).update(right=adresse1)
|
||||
adresse2.delete()
|
||||
else:
|
||||
qs = Relation.objects.filter(schema__slug='habite', right=adresse2)
|
||||
qs = Relation.objects.filter(schema__slug=utils.HABITE_REL, right=adresse2)
|
||||
entities = Entity.objects.filter(left_relations__in=qs)
|
||||
|
||||
qs = Relation.objects.filter(schema__slug='habite')
|
||||
qs = Relation.objects.filter(schema__slug=utils.HABITE_REL)
|
||||
qs = qs.exclude(left__in=entities)
|
||||
qs.filter(right=adresse1).update(right=adresse2)
|
||||
adresse1.delete()
|
||||
|
@ -538,7 +539,7 @@ class JournalView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
try:
|
||||
limit = int(request.GET.get('limit', ''))
|
||||
except ValueError:
|
||||
|
@ -571,7 +572,7 @@ class JournalView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
Log.objects.create(
|
||||
entity=individu,
|
||||
content=request.data)
|
||||
|
@ -592,7 +593,7 @@ class DeclarationAdressePrincipaleView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
serializer = DeclarationAdressePrincipaleSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
|
@ -644,7 +645,7 @@ class ChangementDeSituationMaritaleView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
if individu.content['statut_legal'] != 'majeur':
|
||||
return Response({
|
||||
|
@ -693,7 +694,7 @@ class SeparationView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
if individu.content['statut_legal'] != 'majeur':
|
||||
return Response({
|
||||
|
@ -714,7 +715,7 @@ class SeparationView(APIView):
|
|||
return Response({
|
||||
'err': 0,
|
||||
'data': {
|
||||
'union': individu_to_response(conjoint),
|
||||
utils.UNION_REL: individu_to_response(conjoint),
|
||||
'union_statut': conjoint_rel.content['statut'],
|
||||
'enfants': [individu_to_response(enfant, add_text=True) for enfant in
|
||||
self.enfants_communs(individu, conjoint)],
|
||||
|
@ -726,7 +727,7 @@ class SeparationView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
utils.PersonSearch.decorate_individu(individu)
|
||||
|
||||
|
@ -758,7 +759,7 @@ class SeparationView(APIView):
|
|||
assert adresse_2.id and adresse_1.id != adresse_2.id
|
||||
|
||||
# lie le conjoint à la nouvelle adresse
|
||||
c = (Relation.objects.filter(left=conjoint, right=adresse_1, schema__slug='habite')
|
||||
c = (Relation.objects.filter(left=conjoint, right=adresse_1, schema__slug=utils.HABITE_REL)
|
||||
.update(right=adresse_2))
|
||||
assert c == 1
|
||||
|
||||
|
@ -794,11 +795,11 @@ class SeparationView(APIView):
|
|||
# mise à jour de l'adresse des enfants non communs
|
||||
c = Relation.objects.exclude(left__in=enfants_communs).filter(
|
||||
left__right_relations__left=conjoint,
|
||||
right=adresse_1, schema__slug='habite').update(right=adresse_2)
|
||||
right=adresse_1, schema__slug=utils.HABITE_REL).update(right=adresse_2)
|
||||
assert c == 1
|
||||
|
||||
# crée la nouvelle relation 'habite' et pose le flag principale si demandé
|
||||
habite_schema = RelationSchema.objects.get(slug='habite')
|
||||
# crée la nouvelle relation utils.HABITE_REL et pose le flag principale si demandé
|
||||
habite_schema = RelationSchema.objects.get(slug=utils.HABITE_REL)
|
||||
for enfant in enfants_communs:
|
||||
rel_adresse_1 = Relation.objects.get(left=enfant, right=adresse_1)
|
||||
|
||||
|
@ -820,7 +821,7 @@ class SeparationView(APIView):
|
|||
c, c_by_type = Relation.objects.filter(
|
||||
left__in=[individu, conjoint],
|
||||
right__in=[individu, conjoint],
|
||||
schema__slug='union').delete()
|
||||
schema__slug=utils.UNION_REL).delete()
|
||||
assert c == 1
|
||||
|
||||
return Response({
|
||||
|
@ -840,7 +841,7 @@ class DeclarationDeDecesView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
if individu.content.get('date_de_deces'):
|
||||
return Response({
|
||||
|
@ -862,9 +863,10 @@ class DeclarationDeDecesView(APIView):
|
|||
individu.save()
|
||||
|
||||
# on supprime les relations
|
||||
Relation.objects.filter(schema__slug='responsabilite-legale', left=individu).delete()
|
||||
Relation.objects.filter(schema__slug='union', left=individu).delete()
|
||||
Relation.objects.filter(schema__slug='union', right=individu).delete()
|
||||
Relation.objects.filter(schema__slug=utils.RESPONSABILITE_LEGALE_REL,
|
||||
left=individu).delete()
|
||||
Relation.objects.filter(schema__slug=utils.UNION_REL, left=individu).delete()
|
||||
Relation.objects.filter(schema__slug=utils.UNION_REL, right=individu).delete()
|
||||
|
||||
# si l'adresse est partagée avec un autre adulte on l'individualise
|
||||
adresse = utils.adresse(individu)
|
||||
|
@ -873,13 +875,13 @@ class DeclarationDeDecesView(APIView):
|
|||
new_adresse.id = None
|
||||
new_adresse.save()
|
||||
assert new_adresse.id and new_adresse.id != adresse.id
|
||||
c = Relation.objects.filter(schema__slug='habite', left=individu,
|
||||
c = Relation.objects.filter(schema__slug=utils.HABITE_REL, left=individu,
|
||||
right=adresse).update(right=new_adresse)
|
||||
assert c == 1
|
||||
# si l'adresse n'est partagée qu'avec des enfants, on les débranche
|
||||
else:
|
||||
Relation.objects.filter(
|
||||
schema__slug='habite', right=adresse).exclude(
|
||||
schema__slug=utils.HABITE_REL, right=adresse).exclude(
|
||||
left=individu).delete()
|
||||
return Response({
|
||||
'err': 0,
|
||||
|
@ -894,12 +896,12 @@ class SuppressionLienDeResponsabiliteView(APIView):
|
|||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
individu = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier)
|
||||
|
||||
enfant = get_object_or_404(qs, schema__slug='individu', id=identifier_enfant)
|
||||
enfant = get_object_or_404(qs, schema__slug=utils.INDIVIDU_ENT, id=identifier_enfant)
|
||||
|
||||
c = Relation.objects.filter(left=individu, right=enfant,
|
||||
schema__slug='responsabilite-legale').count()
|
||||
schema__slug=utils.RESPONSABILITE_LEGALE_REL).count()
|
||||
if not c:
|
||||
return Response({
|
||||
'err': 1,
|
||||
|
@ -910,7 +912,7 @@ class SuppressionLienDeResponsabiliteView(APIView):
|
|||
|
||||
c = Relation.objects.filter(
|
||||
right=enfant,
|
||||
schema__slug='responsabilite-legale').count()
|
||||
schema__slug=utils.RESPONSABILITE_LEGALE_REL).count()
|
||||
if c < 2:
|
||||
return Relation({
|
||||
'err': 1,
|
||||
|
@ -922,7 +924,7 @@ class SuppressionLienDeResponsabiliteView(APIView):
|
|||
assert c == 2, 'only two links should exist'
|
||||
|
||||
Relation.objects.filter(left=individu, right=enfant,
|
||||
schema__slug='responsabilite-legale').delete()
|
||||
schema__slug=utils.RESPONSABILITE_LEGALE_REL).delete()
|
||||
|
||||
# suppression éventuelle de l'adresse commune entre l'enfant et l'adulte
|
||||
# si elle n'appartient pas aussi à un autre adulte ayant des responsabilités
|
||||
|
@ -931,10 +933,10 @@ class SuppressionLienDeResponsabiliteView(APIView):
|
|||
other_individu_ids = [rel.left_id for rel in
|
||||
Relation.objects.filter(left__content__statut_legal='majeur',
|
||||
right=adresse,
|
||||
schema__slug='habite')]
|
||||
schema__slug=utils.HABITE_REL)]
|
||||
if not Relation.objects.filter(left_id__in=other_individu_ids,
|
||||
right=enfant,
|
||||
schema__slug='responsabilite-legale').exists():
|
||||
schema__slug=utils.RESPONSABILITE_LEGALE_REL).exists():
|
||||
Relation.objects.filter(left=enfant, right=adresse).delete()
|
||||
|
||||
return Response({
|
||||
|
|
|
@ -4,6 +4,7 @@ import sys
|
|||
import psycopg2
|
||||
|
||||
from zoo.models import Entity, Relation, EntitySchema, RelationSchema, Transaction
|
||||
from zoo.zoo_nanterre.utils import UNION_REL, RESPONSABILITE_LEGALE_REL, HABITE_REL
|
||||
|
||||
Relation.objects.all().delete()
|
||||
Entity.objects.all().delete()
|
||||
|
@ -27,9 +28,9 @@ 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')
|
||||
habite_schema = RelationSchema.objects.get(slug=HABITE_REL)
|
||||
responsabilite_legale_schema = RelationSchema.objects.get(slug=RESPONSABILITE_LEGALE_REL)
|
||||
union_schema = RelationSchema.objects.get(slug=UNION_REL)
|
||||
|
||||
|
||||
individu_mapping = {}
|
||||
|
|
|
@ -19,6 +19,14 @@ from zoo.zoo_data.search import JSONTextRef, Normalize
|
|||
today = datetime.date.today
|
||||
|
||||
|
||||
INDIVIDU_ENT = 'individu'
|
||||
ADRESSE_ENT = 'adresse'
|
||||
|
||||
HABITE_REL = 'habite'
|
||||
UNION_REL = 'union'
|
||||
RESPONSABILITE_LEGALE_REL = 'responsabilite-legale'
|
||||
|
||||
|
||||
def make_date(date_var):
|
||||
'''Extract a date from a datetime, a date, a struct_time or a string'''
|
||||
if isinstance(date_var, datetime.datetime):
|
||||
|
@ -58,12 +66,12 @@ def age_in_years(born, today=None):
|
|||
|
||||
def conjoint(individu):
|
||||
for relation in individu.left_relations.all():
|
||||
if relation.schema.slug != 'union':
|
||||
if relation.schema.slug != UNION_REL:
|
||||
continue
|
||||
return relation.right, relation
|
||||
|
||||
for relation in individu.right_relations.all():
|
||||
if relation.schema.slug != 'union':
|
||||
if relation.schema.slug != UNION_REL:
|
||||
continue
|
||||
return relation.left, relation
|
||||
|
||||
|
@ -72,14 +80,14 @@ def conjoint(individu):
|
|||
|
||||
def enfants(individu):
|
||||
for relation in individu.left_relations.all():
|
||||
if relation.schema.slug != 'responsabilite-legale':
|
||||
if relation.schema.slug != RESPONSABILITE_LEGALE_REL:
|
||||
continue
|
||||
yield relation.right, relation
|
||||
|
||||
|
||||
def parents(individu):
|
||||
for relation in individu.right_relations.all():
|
||||
if relation.schema.slug != 'responsabilite-legale':
|
||||
if relation.schema.slug != RESPONSABILITE_LEGALE_REL:
|
||||
continue
|
||||
yield relation.left
|
||||
|
||||
|
@ -87,7 +95,7 @@ def parents(individu):
|
|||
def adresses(individu):
|
||||
adresses = []
|
||||
for relation in individu.left_relations.all():
|
||||
if relation.schema.slug != 'habite':
|
||||
if relation.schema.slug != HABITE_REL:
|
||||
continue
|
||||
adresses.append((relation.right, relation))
|
||||
adresses.sort(key=lambda (a, r): (not r.content.get('principale', False), a.id))
|
||||
|
@ -132,7 +140,7 @@ class PersonSearch(object):
|
|||
self.email_similarities = []
|
||||
self.key_filters = []
|
||||
self.email_filters = []
|
||||
self.schema = EntitySchema.objects.get(slug='individu')
|
||||
self.schema = EntitySchema.objects.get(slug=INDIVIDU_ENT)
|
||||
self.limit = limit
|
||||
|
||||
def search_query(self, query):
|
||||
|
|
Loading…
Reference in New Issue