api: limit find_duplicates to one organization unit (#55419)
This commit is contained in:
parent
31f25ccc01
commit
f391bb9579
|
@ -870,9 +870,13 @@ class UsersAPI(api_mixins.GetOrCreateMixinView, HookMixin, ExceptionHandlerMixin
|
|||
}
|
||||
return Response(response, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
ou = None
|
||||
if 'ou' in request.query_params:
|
||||
ou = data['ou']
|
||||
|
||||
attributes = data.pop('attributes', {})
|
||||
birthdate = attributes.get('birthdate')
|
||||
qs = User.objects.find_duplicates(first_name, last_name, birthdate=birthdate)
|
||||
qs = User.objects.find_duplicates(first_name, last_name, birthdate=birthdate, ou=ou)
|
||||
|
||||
return Response(
|
||||
{
|
||||
|
|
|
@ -106,7 +106,7 @@ class UserQuerySet(models.QuerySet):
|
|||
return qs
|
||||
|
||||
def find_duplicates(
|
||||
self, first_name=None, last_name=None, fullname=None, birthdate=None, limit=5, threshold=None
|
||||
self, first_name=None, last_name=None, fullname=None, birthdate=None, limit=5, threshold=None, ou=None
|
||||
):
|
||||
self.set_trigram_similarity_threshold(threshold=threshold or app_settings.A2_DUPLICATES_THRESHOLD)
|
||||
|
||||
|
@ -121,6 +121,10 @@ class UserQuerySet(models.QuerySet):
|
|||
qs = qs.filter(name__trigram_similar=name)
|
||||
qs = qs.annotate(dist=TrigramDistance('name', name))
|
||||
qs = qs.order_by('dist')
|
||||
|
||||
if ou:
|
||||
qs = qs.filter(ou=ou)
|
||||
|
||||
if limit is not None:
|
||||
qs = qs[:limit]
|
||||
|
||||
|
|
|
@ -2152,6 +2152,26 @@ def test_find_duplicates_birthdate(app, admin, settings):
|
|||
assert resp.json['data'][0]['id'] == homonym.pk
|
||||
|
||||
|
||||
def test_find_duplicates_ou(app, admin, settings, ou1):
|
||||
settings.LANGUAGE_CODE = 'fr'
|
||||
app.authorization = ('Basic', (admin.username, admin.username))
|
||||
|
||||
user = User.objects.create(first_name='Jean', last_name='Dupont', ou=get_default_ou())
|
||||
homonym = User.objects.create(first_name='Jean', last_name='Dupont', ou=ou1)
|
||||
|
||||
params = {
|
||||
'first_name': 'Jeanne',
|
||||
'last_name': 'Dupont',
|
||||
}
|
||||
resp = app.get('/api/users/find_duplicates/', params=params)
|
||||
assert len(resp.json['data']) == 2
|
||||
|
||||
params['ou'] = get_default_ou().slug
|
||||
resp = app.get('/api/users/find_duplicates/', params=params)
|
||||
assert len(resp.json['data']) == 1
|
||||
assert resp.json['data'][0]['id'] == user.pk
|
||||
|
||||
|
||||
class MockedRequestResponse(mock.Mock):
|
||||
status_code = 200
|
||||
|
||||
|
|
Loading…
Reference in New Issue