Separate the autocompletion logic form the views

This commit is contained in:
Martin Pépin 2020-07-04 13:50:19 +02:00
parent fbbc9937f6
commit 9a90f19502
No known key found for this signature in database
GPG key ID: E7520278B1774448
12 changed files with 78 additions and 82 deletions

View file

@ -1,7 +1,6 @@
import logging
from collections import namedtuple
from dal import autocomplete
from django.conf import settings
from django.db.models import Q
@ -100,14 +99,6 @@ class ModelSearch(SearchUnit):
return self.model.objects.filter(self.get_queryset_filter(keywords))
class Select2QuerySetView(ModelSearch, autocomplete.Select2QuerySetView):
"""Compatibility layer between ModelSearch and Select2QuerySetView."""
def get_queryset(self):
keywords = self.q.split()
return super().search(keywords)
# ---
# LDAP search
# ---

View file

@ -44,7 +44,7 @@ class MockLDAPMixin:
# Mock ldap module whose `initialize_method` always return the same ldap object.
mock_ldap_module = self.MockLDAPModule(mock_ldap_obj)
patcher = mock.patch("shared.views.autocomplete.ldap", new=mock_ldap_module)
patcher = mock.patch("shared.autocomplete.ldap", new=mock_ldap_module)
patcher.start()
self.addCleanup(patcher.stop)

11
shared/views.py Normal file
View file

@ -0,0 +1,11 @@
from dal import autocomplete
from shared.autocomplete import ModelSearch
class Select2QuerySetView(ModelSearch, autocomplete.Select2QuerySetView):
"""Compatibility layer between ModelSearch and Select2QuerySetView."""
def get_queryset(self):
keywords = self.q.split()
return super().search(keywords)