ajout ws séparation
This commit is contained in:
parent
9a28a22785
commit
e18ac08c0d
|
@ -52,6 +52,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert response.json['data']
|
||||
return response.json['data']
|
||||
|
||||
# création du premier adulte
|
||||
create_url = reverse('rsu-api-create-individu')
|
||||
response = app.post_json(create_url, params={}, status=400)
|
||||
assert response.json['err'] == 1
|
||||
|
@ -100,6 +101,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert Entity.objects.filter(schema__slug='adresse').count() == 1
|
||||
assert Relation.objects.count() == 1
|
||||
|
||||
# mise à jour premier adulte
|
||||
update_url = reverse('rsu-api-reseau', kwargs={
|
||||
'identifier': first_id})
|
||||
|
||||
|
@ -116,6 +118,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert Entity.objects.filter(schema__slug='adresse').count() == 1
|
||||
assert Relation.objects.count() == 1
|
||||
|
||||
# création second adulte
|
||||
response = app.post_json(create_url, params={
|
||||
'prenoms': 'Micheline',
|
||||
'nom_de_naissance': 'Michalon-Gourde',
|
||||
|
@ -150,6 +153,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert Relation.objects.filter(schema__slug='habite').count() == 2
|
||||
second_id = response.json['data']['id']
|
||||
|
||||
# rattachement du premier enfant au premier adulte
|
||||
enfant_url = reverse('rsu-api-declaration-responsabilite-legale',
|
||||
kwargs={'identifier': first_id})
|
||||
|
||||
|
@ -170,6 +174,7 @@ def test_create_individu(app, rsu_schema):
|
|||
|
||||
enfant_id = response.json['data']['id']
|
||||
|
||||
# rattachement du premier enfant au deuxième adulte
|
||||
enfant_url = reverse('rsu-api-declaration-responsabilite-legale',
|
||||
kwargs={'identifier': second_id})
|
||||
|
||||
|
@ -181,13 +186,32 @@ def test_create_individu(app, rsu_schema):
|
|||
assert response.json['err'] == 0
|
||||
assert 'id' in response.json['data']
|
||||
|
||||
assert Entity.objects.count() == 5
|
||||
assert Entity.objects.filter(schema__slug='individu').count() == 3
|
||||
assert Entity.objects.filter(schema__slug='adresse').count() == 2
|
||||
assert Relation.objects.count() == 6
|
||||
assert Relation.objects.filter(schema__slug='habite').count() == 4
|
||||
assert Relation.objects.filter(schema__slug='responsabilite-legale').count() == 2
|
||||
# rattachement d'un deuxième enfant au deuxième adulte
|
||||
response = app.post_json(enfant_url, params={
|
||||
'statut': 'parent',
|
||||
'enfant': {
|
||||
'prenoms': u'huguette',
|
||||
'nom_de_naissance': 'Michalon-Gourde',
|
||||
'genre': 'femme',
|
||||
'date_de_naissance': '2016-03-04',
|
||||
'email': '',
|
||||
'telephones': [],
|
||||
}
|
||||
})
|
||||
|
||||
assert response.json['err'] == 0
|
||||
assert 'id' in response.json['data']
|
||||
|
||||
enfant_id_2 = response.json['data']['id']
|
||||
|
||||
assert Entity.objects.count() == 6
|
||||
assert Entity.objects.filter(schema__slug='individu').count() == 4
|
||||
assert Entity.objects.filter(schema__slug='adresse').count() == 2
|
||||
assert Relation.objects.count() == 8
|
||||
assert Relation.objects.filter(schema__slug='habite').count() == 5
|
||||
assert Relation.objects.filter(schema__slug='responsabilite-legale').count() == 3
|
||||
|
||||
# déclaration d'union entre les deux adultes
|
||||
union_url = reverse('rsu-api-declaration-union')
|
||||
|
||||
response = app.post_json(union_url, params={
|
||||
|
@ -199,15 +223,15 @@ def test_create_individu(app, rsu_schema):
|
|||
|
||||
assert response.json['err'] == 0
|
||||
|
||||
assert Entity.objects.count() == 4
|
||||
assert Entity.objects.filter(schema__slug='individu').count() == 3
|
||||
assert Entity.objects.count() == 5
|
||||
assert Entity.objects.filter(schema__slug='individu').count() == 4
|
||||
assert Entity.objects.filter(schema__slug='adresse').count() == 1
|
||||
assert Relation.objects.count() == 6
|
||||
assert Relation.objects.filter(schema__slug='habite').count() == 3
|
||||
assert Relation.objects.filter(schema__slug='responsabilite-legale').count() == 2
|
||||
assert Relation.objects.count() == 8
|
||||
assert Relation.objects.filter(schema__slug='habite').count() == 4
|
||||
assert Relation.objects.filter(schema__slug='responsabilite-legale').count() == 3
|
||||
assert Relation.objects.filter(schema__slug='union').count() == 1
|
||||
|
||||
# journal
|
||||
# écriture dans le journal du premier adulte
|
||||
journal_url = reverse('rsu-api-journal', kwargs={'identifier': first_id})
|
||||
for i in range(30):
|
||||
response = app.post_json(journal_url, params={
|
||||
|
@ -226,6 +250,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert [d['content']['id'] for d in response.json['data']] == range(9, -1, -1)
|
||||
assert 'more' not in response.json
|
||||
|
||||
# déclaration d'adresse principale pour le premier enfant
|
||||
declaration_adresse_principale_url = reverse('rsu-api-declaration-adresse-principale', kwargs={
|
||||
'identifier': enfant_id
|
||||
})
|
||||
|
@ -273,6 +298,7 @@ def test_create_individu(app, rsu_schema):
|
|||
assert data['union']
|
||||
assert data['union_statut'] == 'mariage'
|
||||
|
||||
# changement de situation maritale entre les deux adultes
|
||||
changement_de_situation_maritale_url = reverse('rsu-api-changement-de-situation-maritale',
|
||||
kwargs={
|
||||
'identifier': first_id
|
||||
|
@ -287,3 +313,43 @@ def test_create_individu(app, rsu_schema):
|
|||
data = get_reseau(first_id)
|
||||
assert data['union']
|
||||
assert data['union_statut'] == 'pacs'
|
||||
|
||||
# déclaration de séparation entre les deux adultes
|
||||
separation_url = reverse('rsu-api-separation', kwargs={
|
||||
'identifier': first_id
|
||||
})
|
||||
response = app.get(separation_url)
|
||||
assert response.json['err'] == 0
|
||||
data = response.json['data']
|
||||
assert set(data.keys()) == set(['union', 'union_statut', 'enfants'])
|
||||
assert data['union']['id'] == second_id
|
||||
assert data['union_statut'] == 'pacs'
|
||||
assert len(data['enfants']) == 1
|
||||
assert data['enfants'][0]['id'] == enfant_id
|
||||
|
||||
response = app.post_json(separation_url, params={
|
||||
'enfant_%s' % enfant_id: 1,
|
||||
})
|
||||
|
||||
assert Entity.objects.count() == 6
|
||||
assert Entity.objects.filter(schema__slug='individu').count() == 4
|
||||
assert Entity.objects.filter(schema__slug='adresse').count() == 2
|
||||
assert Relation.objects.count() == 8
|
||||
# tout le monde habite quelque part, un enfant a deux logements
|
||||
assert Relation.objects.filter(schema__slug='habite').count() == 5
|
||||
# le premier adulte et le premier enfant partagent un logement
|
||||
assert Relation.objects.filter(
|
||||
left=first_id, right__right_relations__left=enfant_id).count() == 1
|
||||
# le deuxième adulte et le premier enfant partagent un logement
|
||||
assert Relation.objects.filter(
|
||||
left=second_id, right__right_relations__left=enfant_id).count() == 1
|
||||
# le deuxième adulte et le deuxième enfant partagent un logement
|
||||
assert Relation.objects.filter(
|
||||
left=second_id, right__right_relations__left=enfant_id_2).count() == 1
|
||||
# le premier et le deuxième adulte ne partagent plus de logement
|
||||
assert Relation.objects.filter(
|
||||
left=first_id,
|
||||
right__right_relations__schema__slug='habite',
|
||||
right__right_relations__left=second_id).count() == 0
|
||||
assert Relation.objects.filter(schema__slug='responsabilite-legale').count() == 3
|
||||
assert Relation.objects.filter(schema__slug='union').count() == 0
|
||||
|
|
|
@ -1,4 +1,22 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# zoo - data management system
|
||||
# Copyright (C) 2017 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import isodate
|
||||
import copy
|
||||
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.db.models.query import Q
|
||||
|
@ -658,3 +676,141 @@ class ChangementDeSituationMaritaleView(APIView):
|
|||
})
|
||||
|
||||
changement_de_situation_maritale = ChangementDeSituationMaritaleView.as_view()
|
||||
|
||||
|
||||
class SeparationView(APIView):
|
||||
def enfants_communs(self, individu, conjoint):
|
||||
children = set(enfant for enfant, rel in utils.enfants(individu))
|
||||
children &= set(enfant for enfant, rel in utils.enfants(conjoint))
|
||||
|
||||
return sorted(children, key=lambda e: e.id)
|
||||
|
||||
def get(self, request, identifier, format=None):
|
||||
qs = Entity.objects.prefetch_related(
|
||||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
|
||||
if individu.content['statut_legal'] != 'majeur':
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'__all__': ['individu non majeur'],
|
||||
}
|
||||
})
|
||||
|
||||
conjoint, conjoint_rel = utils.conjoint(individu)
|
||||
if not conjoint:
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'__all__': ["cet individu n'a pas actuellement de relation maritale"],
|
||||
}
|
||||
})
|
||||
return Response({
|
||||
'err': 0,
|
||||
'data': {
|
||||
'union': individu_to_response(conjoint),
|
||||
'union_statut': conjoint_rel.content['statut'],
|
||||
'enfants': [individu_to_response(enfant) for enfant in
|
||||
self.enfants_communs(individu, conjoint)],
|
||||
},
|
||||
})
|
||||
|
||||
def post(self, request, identifier, format=None):
|
||||
qs = Entity.objects.prefetch_related(
|
||||
'left_relations__schema', 'left_relations__right',
|
||||
'right_relations__schema', 'right_relations__left',
|
||||
)
|
||||
individu = get_object_or_404(qs, schema__slug='individu', id=identifier)
|
||||
|
||||
utils.PersonSearch.decorate_individu(individu)
|
||||
|
||||
if individu.content['statut_legal'] != 'majeur':
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'__all__': ['individu non majeur'],
|
||||
}
|
||||
})
|
||||
|
||||
conjoint, conjoint_rel = utils.conjoint(individu)
|
||||
if not conjoint:
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'__all__': ["cet individu n'a pas actuellement de relation maritale"],
|
||||
}
|
||||
})
|
||||
|
||||
enfants_communs = self.enfants_communs(individu, conjoint)
|
||||
|
||||
adresse_1 = utils.adresse(individu)
|
||||
|
||||
adresse_2 = copy.copy(adresse_1)
|
||||
adresse_2.id = None
|
||||
adresse_2.save()
|
||||
|
||||
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')
|
||||
.update(right=adresse_2))
|
||||
assert c == 1
|
||||
|
||||
habite_schema = RelationSchema.objects.get(slug='habite')
|
||||
|
||||
errors = []
|
||||
keys = ['enfant_%s' % enfant.id for enfant in enfants_communs]
|
||||
for key in request.data:
|
||||
if key not in keys:
|
||||
errors[key] = ['clé inconnue']
|
||||
|
||||
for enfant in enfants_communs:
|
||||
principale = request.data.get('enfant_%s' % enfant.id)
|
||||
if principale not in [1, 2]:
|
||||
errors['enfant_%s' % enfant.id] = ['valeur invalide']
|
||||
if errors:
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': errors,
|
||||
})
|
||||
|
||||
# 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)
|
||||
assert c == 1
|
||||
|
||||
# crée la nouvelle relation 'habite' et pose le flag principale si demandé
|
||||
for enfant in enfants_communs:
|
||||
rel_adresse_1 = Relation.objects.get(left=enfant, right=adresse_1)
|
||||
|
||||
rel_adresse_2 = Relation(
|
||||
left=enfant, right=adresse_2,
|
||||
schema=habite_schema,
|
||||
content={
|
||||
'principale': False,
|
||||
})
|
||||
|
||||
principale = request.data.get('enfant_%s' % enfant.id)
|
||||
if principale == 1:
|
||||
rel_adresse_1.content['principale'] = True
|
||||
rel_adresse_1.save()
|
||||
elif principale == 2:
|
||||
rel_adresse_2.content['principale'] = True
|
||||
rel_adresse_2.save()
|
||||
|
||||
# supprime la relation
|
||||
c, c_by_type = Relation.objects.filter(
|
||||
left__in=[individu, conjoint],
|
||||
right__in=[individu, conjoint],
|
||||
schema__slug='union').delete()
|
||||
assert c == 1
|
||||
|
||||
return Response({
|
||||
'err': 0,
|
||||
})
|
||||
|
||||
separation = SeparationView.as_view()
|
||||
|
|
|
@ -18,6 +18,8 @@ urlpatterns = [
|
|||
url(r'^individu/(?P<identifier>\d+)/changement-de-situation-maritale/$',
|
||||
api_views.changement_de_situation_maritale,
|
||||
name='rsu-api-changement-de-situation-maritale'),
|
||||
url(r'^individu/(?P<identifier>\d+)/separation/$',
|
||||
api_views.separation, name='rsu-api-separation'),
|
||||
url(r'^individu/$', api_views.create_individu, name='rsu-api-create-individu'),
|
||||
url(r'^declaration-union/$', api_views.declaration_union,
|
||||
name='rsu-api-declaration-union'),
|
||||
|
|
Loading…
Reference in New Issue