From e7956f3b62e54bc0c4ae529d489408e328301355 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 19 Jan 2017 12:36:40 -0200 Subject: [PATCH] Account search w/ autocomplete --- kfet/autocomplete.py | 26 +++++++++++++ .../kfet/account_search_autocomplete.html | 14 +++++++ kfet/templates/kfet/kpsul.html | 39 ++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 kfet/templates/kfet/account_search_autocomplete.html diff --git a/kfet/autocomplete.py b/kfet/autocomplete.py index 1ebbbc75..1c2b1a75 100644 --- a/kfet/autocomplete.py +++ b/kfet/autocomplete.py @@ -77,3 +77,29 @@ def account_create(request): data['options'] = options return render(request, "kfet/account_create_autocomplete.html", data) + + +def account_search(request): + if "q" not in request.GET: + raise Http404 + q = request.GET.get("q") + words = q.split() + + data = {'q': q} + + for word in words: + query = Account.objects.filter( + Q(cofprofile__user__username__icontains=word) | + Q(cofprofile__user__first_name__icontains=word) | + Q(cofprofile__user__last_name__icontains=word) + ).distinct() + + query = [(account.trigramme, account.cofprofile.user.get_full_name()) + for account in query] + + data['accounts'] = query + options = len(query) + + data['options'] = options + + return render(request, 'kfet/account_search_autocomplete.html', data) diff --git a/kfet/templates/kfet/account_search_autocomplete.html b/kfet/templates/kfet/account_search_autocomplete.html new file mode 100644 index 00000000..22e2cca3 --- /dev/null +++ b/kfet/templates/kfet/account_search_autocomplete.html @@ -0,0 +1,14 @@ +{% load kfet_tags %} + + + diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 26c98e55..6178f039 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -4,6 +4,7 @@ {% block extra_head %} + @@ -256,12 +257,48 @@ $(document).ready(function() { buttons += ''; } else { var url_base = '{% url 'kfet.account' %}' - buttons += ''; + buttons += ''; } } account_container.find('.buttons').html(buttons); } + // Search for an account + function searchAccount() { + var content = '
' ; + $.dialog({ + title: 'Recherche de compte', + content: content, + backgroundDismiss: true, + animation: 'top', + closeAnimation: 'bottom', + keyboardEnabled: true, + onOpen: function() { + var that=this ; + $('input#search_autocomplete').yourlabsAutocomplete({ + url: '{% url 'kfet.account.search.autocomplete' %}', + minimumCharacters: 2, + id: 'search_autocomplete', + choiceSelector: '.choice', + placeholder: "Chercher un utilisateur K-Fêt", + box: $(".yourlabs-autocomplete"), + }); + $('input#search_autocomplete').bind( + 'selectChoice', + function(e, choice, autocomplete) { + autocomplete.hide() ; + triInput.val(choice.find('.trigramme').text()) ; + triInput.trigger('input') ; + that.close() ; + }); + } + }); + } + + account_container.on('click', '.search', function () { + searchAccount() ; + }) ; + // Clear data function resetAccountData() { account_data = account_data_default;