diff --git a/search/models.py b/search/models.py index 909a641a..6e3c14d3 100644 --- a/search/models.py +++ b/search/models.py @@ -12,8 +12,27 @@ CHOICES = ( ('2', 'Archivés'), ) +CHOICES2 = ( + (1, 'Active'), + ("", 'Désactivée'), +) + +CHOICES3 = ( + ('0', 'Utilisateurs'), + ('1', 'Machines'), + ('2', 'Factures'), + ('3', 'Bannissements'), + ('4', 'Accès à titre gracieux'), +) + + class SearchForm(Form): + search_field = forms.CharField(label = 'Search', max_length = 100) + +class SearchFormPlus(Form): search_field = forms.CharField(label = 'Search', max_length = 100, required=False) - filtre = forms.MultipleChoiceField(label="Filtre utilisateur", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES) + filtre = forms.MultipleChoiceField(label="Filtre utilisateurs", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES) + connexion = forms.MultipleChoiceField(label="Filtre connexion", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES2) + affichage = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES3) date_deb = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y']) date_fin = forms.DateField(required=False, help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'], label="Date de fin") diff --git a/search/templates/search/index.html b/search/templates/search/index.html index 79dc6f20..b3221871 100644 --- a/search/templates/search/index.html +++ b/search/templates/search/index.html @@ -1,4 +1,4 @@ -{% extends "users/sidebar.html" %} +{% extends "search/sidebar.html" %} {% load bootstrap3 %} {% block title %}Résultats de la recherche{% endblock %} @@ -16,11 +16,15 @@

Résultats dans les factures :

{% include "cotisations/aff_cotisations.html" with facture_list=facture_list %} {% endif %} + {% if white_list %} +

Résultats dans les accès à titre gracieux :

+ {% include "users/aff_whitelists.html" with white_list=white_list %} + {% endif %} {% if ban_list %}

Résultats dans les banissements :

{% include "users/aff_bans.html" with ban_list=ban_list %} {% endif %} - {% if not ban_list and not machine_list and not users_list and not facture_list%} + {% if not ban_list and not machine_list and not users_list and not facture_list and not white_list%}

Aucun résultat

{% endif %}
diff --git a/search/templates/search/search.html b/search/templates/search/search.html index 9be85b89..d791d58e 100644 --- a/search/templates/search/search.html +++ b/search/templates/search/search.html @@ -1,4 +1,4 @@ -{% extends "users/sidebar.html" %} +{% extends "search/sidebar.html" %} {% load bootstrap3 %} {% block title %}Recherche{% endblock %} @@ -11,4 +11,9 @@ {% bootstrap_form searchform %} {% bootstrap_button "Search" button_type="submit" icon="search" %} +
+
+
+
+
{% endblock %} diff --git a/search/templates/search/sidebar.html b/search/templates/search/sidebar.html index e05c4b37..a55edd13 100644 --- a/search/templates/search/sidebar.html +++ b/search/templates/search/sidebar.html @@ -1,14 +1,6 @@ {% extends "base.html" %} {% block sidebar %} -

Créer un adhérent

-

Editer un adhérent

-

Liste des adhérents

-

Ajouter un droit rezo

-

Retirer un droit rezo

-

Ajouter un bannissement

-

Gérer les bannissements

-

Créer une facture

-

Editer une facture

-

Liste des factures

+

Recherche simple

+

Recherche avancée

{% endblock %} diff --git a/search/urls.py b/search/urls.py index cb1247b4..721bdb97 100644 --- a/search/urls.py +++ b/search/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ url(r'^$', views.search, name='search'), + url(r'^avance/$', views.searchp, name='searchp'), ] diff --git a/search/views.py b/search/views.py index b15a2ecd..b608885a 100644 --- a/search/views.py +++ b/search/views.py @@ -7,10 +7,10 @@ from django.core.context_processors import csrf from django.template import Context, RequestContext, loader from django.db.models import Q -from users.models import User, Ban +from users.models import User, Ban, Whitelist from machines.models import Machine, Interface from cotisations.models import Facture -from search.models import SearchForm +from search.models import SearchForm, SearchFormPlus from users.views import has_access def form(ctx, template, request): @@ -18,25 +18,72 @@ def form(ctx, template, request): c.update(csrf(request)) return render_to_response(template, c, context_instance=RequestContext(request)) +def search_result(search, type): + date_deb = None + date_fin = None + states=[] + co=[] + aff=[0,1,2,3,4] + if(type): + aff = search.cleaned_data['affichage'] + co = search.cleaned_data['connexion'] + states = search.cleaned_data['filtre'] + date_deb = search.cleaned_data['date_deb'] + date_fin = search.cleaned_data['date_fin'] + date_query = Q() + if aff==[]: + aff = [0,1,2,3,4] + if date_deb != None: + date_query = date_query & Q(date__gte=date_deb) + if date_fin != None: + date_query = date_query & Q(date__lte=date_fin) + search = search.cleaned_data['search_field'] + query = Q() + for s in states: + query = query | Q(state = s) + + users = None + machines = None + factures = None + bans = None + whitelists = None + connexion = [] + + for i in aff: + if i == '0': + users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query) + connexion = [] + for user in users: + access=has_access(user) + if(len(co)==0 or (len(co)==1 and bool(co[0])==access) or (len(co)==2 and (bool(co[0])==access or bool(co[1])==access))): + connexion.append([user, access]) + query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search) + if i == '1': + machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search)) + if i == '2': + factures = Facture.objects.filter(query & date_query) + if i == '3': + bans = Ban.objects.filter(query) + if i == '4': + whitelists = Whitelist.objects.filter(query) + return {'users_list': connexion, 'machine_list' : machines, 'facture_list' : factures, 'ban_list' : bans, 'white_list': whitelists} + def search(request): if request.method == 'POST': search = SearchForm(request.POST or None) if search.is_valid(): - states = search.cleaned_data['filtre'] - search = search.cleaned_data['search_field'] - query = Q() - for s in states: - query = query | Q(state = s) - users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query) - connexion = [] - for user in users: - connexion.append([user, has_access(user)]) - query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search) - machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search)) - factures = Facture.objects.filter(query) - bans = Ban.objects.filter(query) - return form({'users_list': connexion, 'machine_list' : machines, 'facture_list' : factures, 'ban_list' : bans}, 'search/index.html',request) + return form(search_result(search, False), 'search/index.html',request) return form({'searchform' : search}, 'search/search.html', request) else: search = SearchForm(request.POST or None) return form({'searchform': search}, 'search/search.html',request) + +def searchp(request): + if request.method == 'POST': + search = SearchFormPlus(request.POST or None) + if search.is_valid(): + return form(search_result(search, True), 'search/index.html',request) + return form({'searchform' : search}, 'search/search.html', request) + else: + search = SearchFormPlus(request.POST or None) + return form({'searchform': search}, 'search/search.html',request) diff --git a/templates/base.html b/templates/base.html index 057a8905..34079495 100644 --- a/templates/base.html +++ b/templates/base.html @@ -40,6 +40,7 @@
+