From ce012400e1e9b7764b3a197f07154eb18d86ebe2 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 8 Feb 2020 20:39:37 +0100 Subject: [PATCH] Club members autocomplete --- apps/member/forms.py | 10 +++++++++- apps/member/urls.py | 1 + apps/member/views.py | 14 +++++++++++++- apps/note/forms.py | 5 +++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/apps/member/forms.py b/apps/member/forms.py index 59d3fec2..5517f1ff 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -1,7 +1,7 @@ # -*- mode: python; coding: utf-8 -*- # Copyright (C) 2018-2019 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later - +from dal import autocomplete from django.contrib.auth.forms import UserChangeForm, UserCreationForm from django.contrib.auth.models import User from django import forms @@ -44,6 +44,14 @@ class MembershipForm(forms.ModelForm): class Meta: model = Membership fields = ('user','roles','date_start') + widgets = { + 'user': autocomplete.ModelSelect2(url='member:user_autocomplete', + attrs={ + 'data-placeholder': 'Nom ...', + 'data-minimum-input-length': 1, + }), + } + MemberFormSet = forms.modelformset_factory(Membership, form=MembershipForm, diff --git a/apps/member/urls.py b/apps/member/urls.py index 9bcc1095..d57541c2 100644 --- a/apps/member/urls.py +++ b/apps/member/urls.py @@ -18,4 +18,5 @@ urlpatterns = [ path('user/',views.UserListView.as_view(),name="user_list"), path('user/',views.UserDetailView.as_view(),name="user_detail"), path('user//update',views.UserUpdateView.as_view(),name="user_update_profile"), + path('user/user-autocomplete',views.UserAutocomplete.as_view(),name="user_autocomplete"), ] diff --git a/apps/member/views.py b/apps/member/views.py index 90ea5ec3..608e3dee 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -2,7 +2,7 @@ # Copyright (C) 2018-2019 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later - +from dal import autocomplete from django.contrib.auth.mixins import LoginRequiredMixin from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, ListView, DetailView, UpdateView @@ -115,6 +115,18 @@ class UserListView(LoginRequiredMixin,SingleTableView): return context +class UserAutocomplete(autocomplete.Select2QuerySetView): + """ + Auto complete note by aliases + """ + def get_queryset(self): + qs = User.objects.all() + + if self.q: + qs = qs.filter(username__regex=self.q) + + return qs + ################################### ############## CLUB ############### ################################### diff --git a/apps/note/forms.py b/apps/note/forms.py index 2e814be6..34d00487 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -24,6 +24,11 @@ class TransactionForm(forms.ModelForm): fields = ('destination', 'reason', 'amount',) widgets = { + 'source': autocomplete.ModelSelect2(url='note:note_autocomplete', + attrs={ + 'data-placeholder': 'Note ...', + 'data-minimum-input-length': 1, + }), 'destination': autocomplete.ModelSelect2(url='note:note_autocomplete', attrs={ 'data-placeholder': 'Note ...',