diff --git a/src/authentic2/api_views.py b/src/authentic2/api_views.py index 9adebefe1..b3d9b3814 100644 --- a/src/authentic2/api_views.py +++ b/src/authentic2/api_views.py @@ -525,6 +525,7 @@ class BaseUserSerializer(serializers.ModelSerializer): class DuplicateUserSerializer(BaseUserSerializer): duplicate_distance = serializers.FloatField(required=True, source='dist') + text = serializers.CharField(required=True, source='get_full_name') class SlugFromNameDefault: @@ -810,8 +811,9 @@ class UsersAPI(api_mixins.GetOrCreateMixinView, HookMixin, ExceptionHandlerMixin serializer = self.get_serializer(data=request.query_params, partial=True) if not serializer.is_valid(): response = { - 'results': [], - 'errors': serializer.errors + 'data': [], + 'err': 1, + 'err_desc': serializer.errors } return Response(response, status.HTTP_400_BAD_REQUEST) data = serializer.validated_data @@ -820,8 +822,9 @@ class UsersAPI(api_mixins.GetOrCreateMixinView, HookMixin, ExceptionHandlerMixin last_name = data.get('last_name') if not (first_name and last_name): response = { - 'results': [], - 'errors': 'first_name and last_name parameters are mandatory.', + 'data': [], + 'err': 1, + 'err_desc': 'first_name and last_name parameters are mandatory.', } return Response(response, status.HTTP_400_BAD_REQUEST) @@ -830,7 +833,8 @@ class UsersAPI(api_mixins.GetOrCreateMixinView, HookMixin, ExceptionHandlerMixin qs = User.objects.find_duplicates(first_name, last_name, birthdate=birthdate) return Response({ - 'results': DuplicateUserSerializer(qs, many=True).data, + 'data': DuplicateUserSerializer(qs, many=True).data, + 'err': 0, }) diff --git a/tests/test_api.py b/tests/test_api.py index 5c0916cc8..fdc73f4be 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1854,23 +1854,24 @@ def test_find_duplicates(app, admin, settings): 'last_name': last_name, } resp = app.get('/api/users/find_duplicates/', params=exact_match) - assert resp.json['results'][0]['id'] == user.id - assert resp.json['results'][0]['duplicate_distance'] == 0 + assert resp.json['data'][0]['id'] == user.id + assert resp.json['data'][0]['duplicate_distance'] == 0 + assert resp.json['data'][0]['text'] == 'Jean-Kévin Du Château' typo = { 'first_name': 'Jean Kévin', 'last_name': 'Du Châtau', } resp = app.get('/api/users/find_duplicates/', params=typo) - assert resp.json['results'][0]['id'] == user.id - assert resp.json['results'][0]['duplicate_distance'] > 0 + assert resp.json['data'][0]['id'] == user.id + assert resp.json['data'][0]['duplicate_distance'] > 0 typo = { 'first_name': 'Jean Kévin', 'last_name': 'Château', } resp = app.get('/api/users/find_duplicates/', params=typo) - assert resp.json['results'][0]['id'] == user.id + assert resp.json['data'][0]['id'] == user.id other_person = { 'first_name': 'Jean-Kévin', @@ -1878,14 +1879,14 @@ def test_find_duplicates(app, admin, settings): } user = User.objects.create(first_name='Éléonore', last_name='âêîôû') resp = app.get('/api/users/find_duplicates/', params=other_person) - assert len(resp.json['results']) == 0 + assert len(resp.json['data']) == 0 other_person = { 'first_name': 'Pierre', 'last_name': 'Du Château', } resp = app.get('/api/users/find_duplicates/', params=other_person) - assert len(resp.json['results']) == 0 + assert len(resp.json['data']) == 0 def test_find_duplicates_unaccent(app, admin, settings): @@ -1895,7 +1896,7 @@ def test_find_duplicates_unaccent(app, admin, settings): user = User.objects.create(first_name='Éléonore', last_name='âêîôû') resp = app.get('/api/users/find_duplicates/', params={'first_name': 'Eleonore', 'last_name': 'aeiou'}) - assert resp.json['results'][0]['id'] == user.id + assert resp.json['data'][0]['id'] == user.id def test_find_duplicates_birthdate(app, admin, settings): @@ -1914,14 +1915,14 @@ def test_find_duplicates_birthdate(app, admin, settings): 'last_name': 'Dupont', } resp = app.get('/api/users/find_duplicates/', params=params) - assert len(resp.json['results']) == 2 + assert len(resp.json['data']) == 2 params['birthdate'] = '1980-01-2', resp = app.get('/api/users/find_duplicates/', params=params) - assert len(resp.json['results']) == 2 - assert resp.json['results'][0]['id'] == user.pk + assert len(resp.json['data']) == 2 + assert resp.json['data'][0]['id'] == user.pk params['birthdate'] = '1980-01-3', resp = app.get('/api/users/find_duplicates/', params=params) - assert len(resp.json['results']) == 2 - assert resp.json['results'][0]['id'] == homonym.pk + assert len(resp.json['data']) == 2 + assert resp.json['data'][0]['id'] == homonym.pk diff --git a/tests/test_large_userbase.py b/tests/test_large_userbase.py index 2010112bc..d20634563 100644 --- a/tests/test_large_userbase.py +++ b/tests/test_large_userbase.py @@ -75,7 +75,7 @@ def test_large_userbase_find_duplicates(large_userbase, app, admin): for i in range(100): resp = app.get('/api/users/find_duplicates/', params=params) - assert len(resp.json['results']) >= 1 + assert len(resp.json['data']) >= 1 def test_large_userbase_find_duplicates_with_birthdate(large_userbase, app, admin): @@ -90,4 +90,4 @@ def test_large_userbase_find_duplicates_with_birthdate(large_userbase, app, admi for i in range(100): resp = app.get('/api/users/find_duplicates/', params=params) - assert len(resp.json['results']) >= 1 + assert len(resp.json['data']) >= 1