ajout ws déclaration d'adresse principale
This commit is contained in:
parent
40acd21a89
commit
40807a8bd4
|
@ -34,6 +34,16 @@ def test_person_search(db, rsu):
|
|||
|
||||
@pytest.mark.django_db(True)
|
||||
def test_create_individu(app, rsu_schema):
|
||||
|
||||
def get_reseau(identifier):
|
||||
reseau_url = reverse('rsu-api-reseau', kwargs={
|
||||
'identifier': identifier
|
||||
})
|
||||
response = app.get(reseau_url)
|
||||
assert response.json['err'] == 0
|
||||
assert response.json['data']
|
||||
return response.json['data']
|
||||
|
||||
create_url = reverse('rsu-api-create-individu')
|
||||
response = app.post_json(create_url, params={}, status=400)
|
||||
assert response.json['err'] == 1
|
||||
|
@ -207,3 +217,46 @@ def test_create_individu(app, rsu_schema):
|
|||
response = app.get(response.json['more'])
|
||||
assert [d['content']['id'] for d in response.json['data']] == range(9, -1, -1)
|
||||
assert 'more' not in response.json
|
||||
|
||||
declaration_adresse_principale_url = reverse('rsu-api-declaration-adresse-principale', kwargs={
|
||||
'identifier': enfant_id
|
||||
})
|
||||
|
||||
data = get_reseau(enfant_id)
|
||||
assert len(data['adresses']) == 1
|
||||
assert data['adresses'][0]['principale'] is False
|
||||
|
||||
response = app.post_json(declaration_adresse_principale_url, params={
|
||||
'adresse_principale': 1,
|
||||
})
|
||||
|
||||
assert response.json['err'] == 0
|
||||
|
||||
data = get_reseau(enfant_id)
|
||||
assert len(data['adresses']) == 1
|
||||
assert data['adresses'][0]['principale'] is True
|
||||
|
||||
response = app.post_json(declaration_adresse_principale_url, params={
|
||||
'adresse_principale': 2,
|
||||
})
|
||||
|
||||
assert response.json['err'] == 1
|
||||
assert response.json['errors']['adresse_principale'] == ['adresse inconnue']
|
||||
|
||||
response = app.post_json(declaration_adresse_principale_url, params={
|
||||
'adresse_principale': 0,
|
||||
})
|
||||
|
||||
assert response.json['err'] == 1
|
||||
assert response.json['errors']['adresse_principale'] == ['adresse inconnue']
|
||||
|
||||
declaration_adresse_principale_url = reverse('rsu-api-declaration-adresse-principale', kwargs={
|
||||
'identifier': first_id
|
||||
})
|
||||
|
||||
response = app.post_json(declaration_adresse_principale_url, params={
|
||||
'adresse_principale': 1,
|
||||
})
|
||||
|
||||
assert response.json['err'] == 1
|
||||
assert response.json['errors']['__all__'] == ['individu non mineur']
|
||||
|
|
|
@ -559,3 +559,54 @@ class JournalView(APIView):
|
|||
})
|
||||
|
||||
journal = JournalView.as_view()
|
||||
|
||||
|
||||
class DeclarationAdressePrincipaleSerializer(serializers.Serializer):
|
||||
adresse_principale = serializers.IntegerField()
|
||||
|
||||
|
||||
class DeclarationAdressePrincipaleView(APIView):
|
||||
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)
|
||||
|
||||
serializer = DeclarationAdressePrincipaleSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': serializer.errors,
|
||||
}, status=400)
|
||||
|
||||
if individu.content['statut_legal'] != 'mineur':
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'__all__': ['individu non mineur'],
|
||||
}
|
||||
})
|
||||
adresses = list(utils.adresses(individu))
|
||||
idx = serializer.validated_data['adresse_principale']
|
||||
if not (1 <= idx <= len(adresses)):
|
||||
return Response({
|
||||
'err': 1,
|
||||
'errors': {
|
||||
'adresse_principale': ['adresse inconnue'],
|
||||
}
|
||||
})
|
||||
for i, (adresse, rel) in enumerate(adresses):
|
||||
if i + 1 == idx:
|
||||
if not rel.content['principale']:
|
||||
rel.content['principale'] = True
|
||||
rel.save()
|
||||
else:
|
||||
if rel.content['principale']:
|
||||
rel.content['principale'] = False
|
||||
rel.save()
|
||||
return Response({
|
||||
'err': 0
|
||||
})
|
||||
|
||||
declaration_adresse_principale = DeclarationAdressePrincipaleView.as_view()
|
||||
|
|
|
@ -12,6 +12,9 @@ urlpatterns = [
|
|||
url(r'^individu/(?P<identifier>\d+)/declaration-responsabilite-legale/$',
|
||||
api_views.declaration_responsabilite_legale,
|
||||
name='rsu-api-declaration-responsabilite-legale'),
|
||||
url(r'^individu/(?P<identifier>\d+)/declaration-adresse-principale/$',
|
||||
api_views.declaration_adresse_principale,
|
||||
name='rsu-api-declaration-adresse-principale'),
|
||||
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