Commit a634ce8a authored by Maël Kervella's avatar Maël Kervella

Gros nettoyage de code + virer les filtres sur l'état de connexion

parent f8992ca0
...@@ -22,19 +22,76 @@ ...@@ -22,19 +22,76 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db.models import Q from django.db import models
from simple_search import BaseSearchForm from django import forms
from django.forms import Form
from users.models import User, School from django.forms import ModelForm
class UserSearchForm(BaseSearchForm): CHOICES_USER = (
class Meta: ('0', 'Actifs'),
base_qs = User.objects ('1', 'Désactivés'),
search_fields = ('^name', 'description', 'specifications', '=id') ('2', 'Archivés'),
)
# assumes a fulltext index has been defined on the fields
# 'name,description,specifications,id' CHOICES_AFF = (
fulltext_indexes = ( ('0', 'Utilisateurs'),
('name', 2), # name matches are weighted higher ('1', 'Machines'),
('name,description,specifications,id', 1), ('2', 'Factures'),
) ('3', 'Bannissements'),
('4', 'Accès à titre gracieux'),
('6', 'Switchs'),
('5', 'Ports'),
)
def initial_choices(c):
return [i[0] for i in c]
class SearchForm(Form):
q = forms.CharField(label = 'Search', max_length = 100)
def clean(self):
cleaned_data = super(SearchForm, self).clean()
q = cleaned_data.get('q', '')
class SearchFormPlus(Form):
q = forms.CharField(
label = 'Search',
max_length = 100,
required=False
)
u = forms.MultipleChoiceField(
label="Filtre utilisateurs",
required=False,
widget=forms.CheckboxSelectMultiple,
choices=CHOICES_USER,
initial=initial_choices(CHOICES_USER)
)
a = forms.MultipleChoiceField(
label="Filtre affichage",
required=False,
widget=forms.CheckboxSelectMultiple,
choices=CHOICES_AFF,
initial=initial_choices(CHOICES_AFF)
)
s = forms.DateField(
required=False,
label="Date de début",
help_text='DD/MM/YYYY',
input_formats=['%d/%m/%Y']
)
e = forms.DateField(
required=False,
help_text='DD/MM/YYYY',
input_formats=['%d/%m/%Y'],
label="Date de fin"
)
def clean(self):
cleaned_data = super(SearchFormPlus, self).clean()
q = cleaned_data.get('q')
u = cleaned_data.get('u')
a = cleaned_data.get('a')
s = cleaned_data.get('s')
e = cleaned_data.get('e')
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Gabriel Détraz
# Copyright © 2017 Goulven Kermarec
# Copyright © 2017 Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from __future__ import unicode_literals
from django.db import models
from django import forms
from django.forms import Form
from django.forms import ModelForm
CHOICES_USER = (
('0', 'Actifs'),
('1', 'Désactivés'),
('2', 'Archivés'),
)
CHOICES_CO = (
(1, 'Active'),
("", 'Désactivée'),
)
CHOICES_AFF = (
('0', 'Utilisateurs'),
('1', 'Machines'),
('2', 'Factures'),
('3', 'Bannissements'),
('4', 'Accès à titre gracieux'),
('6', 'Switchs'),
('5', 'Ports'),
)
class SearchForm(Form):
query = forms.CharField(label = 'Search', max_length = 100)
class SearchFormPlus(Form):
query = forms.CharField(label = 'Search', max_length = 100, required=False)
user_state = forms.MultipleChoiceField(label="Filtre utilisateurs", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_USER)
co_state = forms.MultipleChoiceField(label="Filtre connexion", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_CO)
aff = forms.MultipleChoiceField(label="Filtre affichage", required=False, widget =forms.CheckboxSelectMultiple,choices=CHOICES_AFF)
start = forms.DateField(required=False, label="Date de début", help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'])
end = forms.DateField(required=False, help_text='DD/MM/YYYY', input_formats=['%d/%m/%Y'], label="Date de fin")
...@@ -37,93 +37,191 @@ from users.models import User, Ban, Whitelist ...@@ -37,93 +37,191 @@ from users.models import User, Ban, Whitelist
from machines.models import Machine, Interface from machines.models import Machine, Interface
from topologie.models import Port, Switch from topologie.models import Port, Switch
from cotisations.models import Facture from cotisations.models import Facture
from search.models import SearchForm, SearchFormPlus
from preferences.models import GeneralOption from preferences.models import GeneralOption
from search.forms import (
SearchForm,
SearchFormPlus,
CHOICES_USER,
CHOICES_AFF,
initial_choices
)
def search_result(search_form, type, request): def get_results(query, request, filters={}):
start = None start = filters.get('s', None)
end = None end = filters.get('e', None)
user_state = [] user_state = filters.get('u', initial_choices(CHOICES_USER))
co_state = [] aff = filters.get('a', initial_choices(CHOICES_AFF))
aff = []
if(type): options, created = GeneralOption.objects.get_or_create()
aff = search_form.cleaned_data['aff'] max_result = options.search_display_page
co_state = search_form.cleaned_data['co_state']
user_state = search_form.cleaned_data['user_state'] user_state_filter = Q()
start = search_form.cleaned_data['start']
end = search_form.cleaned_data['end']
date_query = Q()
if aff==[]:
aff = ['0','1','2','3','4','5','6']
if start != None:
date_query = date_query & Q(date__gte=start)
if end != None:
date_query = date_query & Q(date__lte=end)
search = search_form.cleaned_data['query']
query1 = Q()
for s in user_state: for s in user_state:
query1 = query1 | Q(state = s) user_state_filter |= Q(state = s)
connexion = [] connexion = []
recherche = {'users_list': None, 'machines_list' : [], 'facture_list' : None, 'ban_list' : None, 'white_list': None, 'port_list': None, 'switch_list': None} results = {
'users_list': User.objects.none(),
if request.user.has_perms(('cableur',)): 'machines_list' : Machine.objects.none(),
query = Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search) 'facture_list' : Facture.objects.none(),
else: 'ban_list' : Ban.objects.none(),
query = (Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user) 'white_list': Whitelist.objects.none(),
'port_list': Port.objects.none(),
'switch_list': Switch.objects.none()
for i in aff: }
if i == '0':
query_user_list = Q(adherent__room__name__icontains = search) | Q(club__room__name__icontains = search) | Q(pseudo__icontains = search) | Q(adherent__name__icontains = search) | Q(surname__icontains = search) & query1 users_filter = Q(
if request.user.has_perms(('cableur',)): user__pseudo__icontains = query
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct() ) | Q(
else : user__adherent__name__icontains = query
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname').distinct() ) | Q(
if i == '1': user__surname__icontains = query
query_machine_list = Q(machine__user__pseudo__icontains = search) | Q(machine__user__adherent__name__icontains = search) | Q(machine__user__surname__icontains = search) | Q(mac_address__icontains = search) | Q(ipv4__ipv4__icontains = search) | Q(domain__name__icontains = search) | Q(domain__related_domain__name__icontains = search) )
if request.user.has_perms(('cableur',)): if not request.user.has_perms(('cableur',)):
data = Interface.objects.filter(query_machine_list).distinct() users_filter &= Q(user = request.user)
else:
data = Interface.objects.filter(query_machine_list & Q(machine__user__id = request.user.id)).distinct() # Users
for d in data: if '0' in aff:
recherche['machines_list'].append(d.machine) filter_user_list = Q(
if i == '2': adherent__room__name__icontains = query
recherche['facture_list'] = Facture.objects.filter(query & date_query).distinct() ) | Q(
if i == '3': club__room__name__icontains = query
recherche['ban_list'] = Ban.objects.filter(query).distinct() ) | Q(
if i == '4': pseudo__icontains = query
recherche['white_list'] = Whitelist.objects.filter(query).distinct() ) | Q(
if i == '5': adherent__name__icontains = query
recherche['port_list'] = Port.objects.filter(details__icontains = search).distinct() ) | Q(
if not request.user.has_perms(('cableur',)): surname__icontains = query
recherche['port_list'] = None ) & user_state_filter
if i == '6': if not request.user.has_perms(('cableur',)):
recherche['switch_list'] = Switch.objects.filter(details__icontains = search).distinct() filter_user_list &= Q(id=request.user.id)
if not request.user.has_perms(('cableur',)): results['users_list'] = User.objects.filter(
recherche['switch_list'] = None filter_user_list
options, created = GeneralOption.objects.get_or_create() ).order_by('state', 'surname').distinct()[:max_result]
search_display_page = options.search_display_page
# Machines
if '1' in aff:
filter_machine_list = Q(
user__pseudo__icontains = query
) | Q(
user__adherent__name__icontains = query
) | Q(
user__surname__icontains = query
) | Q(
interface__mac_address__icontains = query
) | Q(
interface__ipv4__ipv4__icontains = query
) | Q(
interface__domain__name__icontains = query
) | Q(
interface__domain__related_domain__name__icontains = query
)
if not request.user.has_perms(('cableur',)):
filter_machine_list &= Q(machine__user__id=request.user.id)
results['machines_list'] = Machine.objects.filter(
filter_machine_list
).order_by('name').distinct()[:max_result]
# Factures
if '2' in aff:
date_filter = Q()
if start != None:
date_filter &= Q(date__gte=start)
if end != None:
date_filter &= Q(date__lte=end)
results['facture_list'] = Facture.objects.filter(
users_filter & date_filter
).order_by('date').distinct()[:max_result]
# Bans
if '3' in aff:
date_filter = Q()
if start != None:
date_filter &= (
Q(date_start__gte=start) & Q(date_end__gte=start)
) | (
Q(date_start__lte=start) & Q(date_end__gte=start)
) | (
Q(date_start__gte=start) & Q(date_end__lte=start)
)
if end != None:
date_filter &= (
Q(date_start__lte=end) & Q(date_end__lte=end)
) | (
Q(date_start__lte=end) & Q(date_end__gte=end)
) | (
Q(date_start__gte=end) & Q(date_end__lte=end)
)
results['ban_list'] = Ban.objects.filter(
users_filter & date_filter
).order_by('date_end').distinct()[:max_result]
# Whitelists
if '4' in aff:
date_filter = Q()
if start != None:
date_filter &= (
Q(date_start__gte=start) & Q(date_end__gte=start)
) | (
Q(date_start__lte=start) & Q(date_end__gte=start)
) | (
Q(date_start__gte=start) & Q(date_end__lte=start)
)
if end != None:
date_filter &= (
Q(date_start__lte=end) & Q(date_end__lte=end)
) | (
Q(date_start__lte=end) & Q(date_end__gte=end)
) | (
Q(date_start__gte=end) & Q(date_end__lte=end)
)
results['white_list'] = Whitelist.objects.filter(
users_filter & date_filter
).order_by('date_end').distinct()[:max_result]
# Switch ports
if '5' in aff and request.user.has_perms(('cableur',)):
results['port_list'] = Port.objects.filter(
details__icontains=query
).order_by('switch', 'port').distinct()[:max_result]
for r in recherche: # Switches
if recherche[r] != None: if '6' in aff and request.user.has_perms(('cableur')):
recherche[r] = recherche[r][:search_display_page] results['switch_list'] = Switch.objects.filter(
details__icontains=query
).order_by('stack', 'stack_member_id').distinct()[:max_result]
recherche.update({'max_result': search_display_page}) results.update({'max_result': max_result})
return recherche return results
@login_required @login_required
def search(request): def search(request):
search_form = SearchForm(request.GET or None) search_form = SearchForm(request.GET or None)
if search_form.is_valid(): if search_form.is_valid():
return render(request, 'search/index.html', search_result(search_form, False, request)) return render(
request,
'search/index.html',
get_results(
search_form.cleaned_data.get('q', ''),
request,
search_form.cleaned_data
)
)
return render(request, 'search/search.html', {'search_form' : search_form}) return render(request, 'search/search.html', {'search_form' : search_form})
@login_required @login_required
def searchp(request): def searchp(request):
search_form = SearchFormPlus(request.GET or None) search_form = SearchFormPlus(request.GET or None)
if search_form.is_valid(): if search_form.is_valid():
return render(request, 'search/index.html', search_result(search_form, True, request)) return render(
request,
'search/index.html',
get_results(
search_form.cleaned_data.get('q', ''),
request,
search_form.cleaned_data
)
)
return render(request, 'search/search.html', {'search_form' : search_form}) return render(request, 'search/search.html', {'search_form' : search_form})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment