forked from DGNum/gestioCOF
Merge branch 'kerl/factor_autocompletion_views2' into 'master'
Petite réorganisation de l'autocomplétion See merge request klub-dev-ens/gestioCOF!428
This commit is contained in:
commit
24d7d90c28
12 changed files with 76 additions and 86 deletions
|
@ -42,7 +42,7 @@ from bda.models import (
|
||||||
Tirage,
|
Tirage,
|
||||||
)
|
)
|
||||||
from gestioncof.decorators import BuroRequiredMixin, buro_required, cof_required
|
from gestioncof.decorators import BuroRequiredMixin, buro_required, cof_required
|
||||||
from shared.views.autocomplete import Select2QuerySetView
|
from shared.views import Select2QuerySetView
|
||||||
|
|
||||||
|
|
||||||
@cof_required
|
@cof_required
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from shared.views import autocomplete
|
from shared import autocomplete
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ if "gestioncof" in settings.INSTALLED_APPS:
|
||||||
from wagtail.documents import urls as wagtaildocs_urls
|
from wagtail.documents import urls as wagtaildocs_urls
|
||||||
|
|
||||||
from gestioncof import csv_views, views as gestioncof_views
|
from gestioncof import csv_views, views as gestioncof_views
|
||||||
from gestioncof.autocomplete import autocomplete
|
|
||||||
from gestioncof.urls import (
|
from gestioncof.urls import (
|
||||||
calendar_patterns,
|
calendar_patterns,
|
||||||
clubs_patterns,
|
clubs_patterns,
|
||||||
|
@ -109,12 +108,12 @@ if "gestioncof" in settings.INSTALLED_APPS:
|
||||||
# Autocompletion
|
# Autocompletion
|
||||||
path(
|
path(
|
||||||
"autocomplete/registration",
|
"autocomplete/registration",
|
||||||
autocomplete,
|
gestioncof_views.RegistrationAutocompleteView.as_view(),
|
||||||
name="cof.registration.autocomplete",
|
name="cof.registration.autocomplete",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"user/autocomplete",
|
"user/autocomplete",
|
||||||
gestioncof_views.user_autocomplete,
|
gestioncof_views.UserAutocompleteView.as_view(),
|
||||||
name="cof-user-autocomplete",
|
name="cof-user-autocomplete",
|
||||||
),
|
),
|
||||||
# Liens utiles du COF et du BdA
|
# Liens utiles du COF et du BdA
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.http import Http404
|
|
||||||
from django.views.generic import TemplateView
|
|
||||||
|
|
||||||
from gestioncof.decorators import buro_required
|
from shared import autocomplete
|
||||||
from shared.views import autocomplete
|
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -41,22 +38,3 @@ class COFSearch(autocomplete.Compose):
|
||||||
("others", COFOthersSearch()),
|
("others", COFOthersSearch()),
|
||||||
("clippers", autocomplete.LDAPSearch()),
|
("clippers", autocomplete.LDAPSearch()),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
cof_search = COFSearch()
|
|
||||||
|
|
||||||
|
|
||||||
class AutocompleteView(TemplateView):
|
|
||||||
template_name = "gestioncof/search_results.html"
|
|
||||||
|
|
||||||
def get_context_data(self, *args, **kwargs):
|
|
||||||
ctx = super().get_context_data(*args, **kwargs)
|
|
||||||
if "q" not in self.request.GET:
|
|
||||||
raise Http404
|
|
||||||
q = self.request.GET["q"]
|
|
||||||
ctx["q"] = q
|
|
||||||
ctx.update(cof_search.search(q.split()))
|
|
||||||
return ctx
|
|
||||||
|
|
||||||
|
|
||||||
autocomplete = buro_required(AutocompleteView.as_view())
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ from django.urls import reverse
|
||||||
from bda.models import Salle, Tirage
|
from bda.models import Salle, Tirage
|
||||||
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
|
from gestioncof.models import CalendarSubscription, Club, Event, Survey, SurveyAnswer
|
||||||
from gestioncof.tests.mixins import MegaHelperMixin, ViewTestCaseMixin
|
from gestioncof.tests.mixins import MegaHelperMixin, ViewTestCaseMixin
|
||||||
|
from shared.autocomplete import Clipper
|
||||||
from shared.tests.mixins import CSVResponseMixin, ICalMixin, MockLDAPMixin
|
from shared.tests.mixins import CSVResponseMixin, ICalMixin, MockLDAPMixin
|
||||||
from shared.views.autocomplete import Clipper
|
|
||||||
|
|
||||||
from .utils import create_member, create_root, create_user
|
from .utils import create_member, create_root, create_user
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,8 @@ from django_cas_ng.views import LogoutView as CasLogoutView
|
||||||
from icalendar import Calendar, Event as Vevent
|
from icalendar import Calendar, Event as Vevent
|
||||||
|
|
||||||
from bda.models import Spectacle, Tirage
|
from bda.models import Spectacle, Tirage
|
||||||
from gestioncof.decorators import buro_required, cof_required
|
from gestioncof.autocomplete import COFSearch
|
||||||
|
from gestioncof.decorators import BuroRequiredMixin, buro_required, cof_required
|
||||||
from gestioncof.forms import (
|
from gestioncof.forms import (
|
||||||
CalendarForm,
|
CalendarForm,
|
||||||
ClubsForm,
|
ClubsForm,
|
||||||
|
@ -58,7 +59,7 @@ from gestioncof.models import (
|
||||||
SurveyQuestion,
|
SurveyQuestion,
|
||||||
SurveyQuestionAnswer,
|
SurveyQuestionAnswer,
|
||||||
)
|
)
|
||||||
from shared.views.autocomplete import Select2QuerySetView
|
from shared.views import Select2QuerySetView
|
||||||
|
|
||||||
|
|
||||||
class HomeView(LoginRequiredMixin, TemplateView):
|
class HomeView(LoginRequiredMixin, TemplateView):
|
||||||
|
@ -942,9 +943,19 @@ class ConfigUpdate(FormView):
|
||||||
##
|
##
|
||||||
|
|
||||||
|
|
||||||
class UserAutocomplete(Select2QuerySetView):
|
class UserAutocompleteView(BuroRequiredMixin, Select2QuerySetView):
|
||||||
model = User
|
model = User
|
||||||
search_fields = ("username", "first_name", "last_name")
|
search_fields = ("username", "first_name", "last_name")
|
||||||
|
|
||||||
|
|
||||||
user_autocomplete = buro_required(UserAutocomplete.as_view())
|
class RegistrationAutocompleteView(BuroRequiredMixin, TemplateView):
|
||||||
|
template_name = "gestioncof/search_results.html"
|
||||||
|
|
||||||
|
def get_context_data(self, *args, **kwargs):
|
||||||
|
ctx = super().get_context_data(*args, **kwargs)
|
||||||
|
if "q" not in self.request.GET:
|
||||||
|
raise Http404
|
||||||
|
q = self.request.GET["q"]
|
||||||
|
ctx["q"] = q
|
||||||
|
ctx.update(COFSearch().search(q.split()))
|
||||||
|
return ctx
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.http import Http404
|
|
||||||
from django.views.generic import TemplateView
|
|
||||||
|
|
||||||
from shared.views import autocomplete
|
from shared import autocomplete
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
@ -60,39 +57,3 @@ class KfetAutocomplete(autocomplete.Compose):
|
||||||
("users_notcof", OthersSearch()),
|
("users_notcof", OthersSearch()),
|
||||||
("clippers", autocomplete.LDAPSearch()),
|
("clippers", autocomplete.LDAPSearch()),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
kfet_autocomplete = KfetAutocomplete()
|
|
||||||
|
|
||||||
|
|
||||||
class AccountCreateAutocompleteView(PermissionRequiredMixin, TemplateView):
|
|
||||||
template_name = "kfet/account_create_autocomplete.html"
|
|
||||||
permission_required = "kfet.is_team"
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
|
||||||
ctx = super().get_context_data(**kwargs)
|
|
||||||
if "q" not in self.request.GET:
|
|
||||||
raise Http404
|
|
||||||
q = self.request.GET["q"]
|
|
||||||
ctx["q"] = q
|
|
||||||
results = kfet_autocomplete.search(q.split())
|
|
||||||
ctx["options"] = sum((len(res) for res in results.values()))
|
|
||||||
ctx.update(results)
|
|
||||||
return ctx
|
|
||||||
|
|
||||||
|
|
||||||
class AccountSearchAutocompleteView(PermissionRequiredMixin, TemplateView):
|
|
||||||
template_name = "kfet/account_search_autocomplete.html"
|
|
||||||
permission_required = "kfet.is_team"
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
|
||||||
ctx = super().get_context_data(**kwargs)
|
|
||||||
if "q" not in self.request.GET:
|
|
||||||
raise Http404
|
|
||||||
q = self.request.GET["q"]
|
|
||||||
ctx["q"] = q
|
|
||||||
ctx["accounts"] = [
|
|
||||||
(user.profile.account_kfet.trigramme, user.get_full_name())
|
|
||||||
for user in KfetAccountSearch().search(q.split())
|
|
||||||
]
|
|
||||||
return ctx
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
from django.urls import include, path, register_converter
|
from django.urls import include, path, register_converter
|
||||||
|
|
||||||
from kfet import autocomplete, converters, views
|
from kfet import converters, views
|
||||||
from kfet.decorators import teamkfet_required
|
from kfet.decorators import teamkfet_required
|
||||||
|
|
||||||
register_converter(converters.TrigrammeConverter, "trigramme")
|
register_converter(converters.TrigrammeConverter, "trigramme")
|
||||||
|
@ -38,13 +38,13 @@ urlpatterns = [
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"autocomplete/account_new",
|
"autocomplete/account_new",
|
||||||
autocomplete.AccountCreateAutocompleteView.as_view(),
|
views.AccountCreateAutocompleteView.as_view(),
|
||||||
name="kfet.account.create.autocomplete",
|
name="kfet.account.create.autocomplete",
|
||||||
),
|
),
|
||||||
# Account - Search
|
# Account - Search
|
||||||
path(
|
path(
|
||||||
"autocomplete/account_search",
|
"autocomplete/account_search",
|
||||||
autocomplete.AccountSearchAutocompleteView.as_view(),
|
views.AccountSearchAutocompleteView.as_view(),
|
||||||
name="kfet.account.search.autocomplete",
|
name="kfet.account.search.autocomplete",
|
||||||
),
|
),
|
||||||
# Account - Read
|
# Account - Read
|
||||||
|
|
|
@ -27,6 +27,7 @@ from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
||||||
from gestioncof.models import CofProfile
|
from gestioncof.models import CofProfile
|
||||||
from kfet import KFET_DELETED_TRIGRAMME, consumers
|
from kfet import KFET_DELETED_TRIGRAMME, consumers
|
||||||
from kfet.auth.decorators import kfet_password_auth
|
from kfet.auth.decorators import kfet_password_auth
|
||||||
|
from kfet.autocomplete import KfetAccountSearch, KfetAutocomplete
|
||||||
from kfet.config import kfet_config
|
from kfet.config import kfet_config
|
||||||
from kfet.decorators import teamkfet_required
|
from kfet.decorators import teamkfet_required
|
||||||
from kfet.forms import (
|
from kfet.forms import (
|
||||||
|
@ -2586,3 +2587,41 @@ class ArticleStatSales(ScaleMixin, JSONDetailView):
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
# ---
|
||||||
|
# Autocompletion views
|
||||||
|
# ---
|
||||||
|
|
||||||
|
|
||||||
|
class AccountCreateAutocompleteView(PermissionRequiredMixin, TemplateView):
|
||||||
|
template_name = "kfet/account_create_autocomplete.html"
|
||||||
|
permission_required = "kfet.is_team"
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
ctx = super().get_context_data(**kwargs)
|
||||||
|
if "q" not in self.request.GET:
|
||||||
|
raise Http404
|
||||||
|
q = self.request.GET["q"]
|
||||||
|
ctx["q"] = q
|
||||||
|
results = KfetAutocomplete().search(q.split())
|
||||||
|
ctx["options"] = sum((len(res) for res in results.values()))
|
||||||
|
ctx.update(results)
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
class AccountSearchAutocompleteView(PermissionRequiredMixin, TemplateView):
|
||||||
|
template_name = "kfet/account_search_autocomplete.html"
|
||||||
|
permission_required = "kfet.is_team"
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
ctx = super().get_context_data(**kwargs)
|
||||||
|
if "q" not in self.request.GET:
|
||||||
|
raise Http404
|
||||||
|
q = self.request.GET["q"]
|
||||||
|
ctx["q"] = q
|
||||||
|
ctx["accounts"] = [
|
||||||
|
(user.profile.account_kfet.trigramme, user.get_full_name())
|
||||||
|
for user in KfetAccountSearch().search(q.split())
|
||||||
|
]
|
||||||
|
return ctx
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from dal import autocomplete
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
|
@ -100,14 +99,6 @@ class ModelSearch(SearchUnit):
|
||||||
return self.model.objects.filter(self.get_queryset_filter(keywords))
|
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
|
# LDAP search
|
||||||
# ---
|
# ---
|
|
@ -44,7 +44,7 @@ class MockLDAPMixin:
|
||||||
# Mock ldap module whose `initialize_method` always return the same ldap object.
|
# Mock ldap module whose `initialize_method` always return the same ldap object.
|
||||||
mock_ldap_module = self.MockLDAPModule(mock_ldap_obj)
|
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()
|
patcher.start()
|
||||||
self.addCleanup(patcher.stop)
|
self.addCleanup(patcher.stop)
|
||||||
|
|
||||||
|
|
11
shared/views.py
Normal file
11
shared/views.py
Normal 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)
|
Loading…
Reference in a new issue