2017-01-15 23:01:18 +00:00
|
|
|
# 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.
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
# App de recherche pour re2o
|
2016-07-08 10:35:53 +00:00
|
|
|
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
|
2016-07-02 22:27:22 +00:00
|
|
|
# Gplv2
|
2017-09-10 23:29:24 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-07-02 22:27:22 +00:00
|
|
|
from django.shortcuts import render
|
2016-12-14 02:52:59 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2016-12-12 12:32:18 +00:00
|
|
|
from django.template.context_processors import csrf
|
2016-07-02 22:27:22 +00:00
|
|
|
from django.template import Context, RequestContext, loader
|
2016-07-08 10:35:53 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
from django.db.models import Q
|
2016-07-04 21:56:51 +00:00
|
|
|
from users.models import User, Ban, Whitelist
|
2016-07-04 00:48:24 +00:00
|
|
|
from machines.models import Machine, Interface
|
2016-07-06 21:53:44 +00:00
|
|
|
from topologie.models import Port, Switch
|
2016-07-02 22:27:22 +00:00
|
|
|
from cotisations.models import Facture
|
2016-07-04 21:56:51 +00:00
|
|
|
from search.models import SearchForm, SearchFormPlus
|
2017-06-25 23:29:34 +00:00
|
|
|
from preferences.models import GeneralOption
|
2016-10-12 20:58:41 +00:00
|
|
|
|
2017-11-01 16:01:10 +00:00
|
|
|
def search_result(search_form, type, request):
|
|
|
|
start = None
|
|
|
|
end = None
|
|
|
|
user_state = []
|
|
|
|
co_state = []
|
|
|
|
aff = []
|
2016-07-04 21:56:51 +00:00
|
|
|
if(type):
|
2017-11-01 16:01:10 +00:00
|
|
|
aff = search_form.cleaned_data['aff']
|
|
|
|
co_state = search_form.cleaned_data['co_state']
|
|
|
|
user_state = search_form.cleaned_data['user_state']
|
|
|
|
start = search_form.cleaned_data['start']
|
|
|
|
end = search_form.cleaned_data['end']
|
2016-07-04 21:56:51 +00:00
|
|
|
date_query = Q()
|
|
|
|
if aff==[]:
|
2016-07-06 21:53:44 +00:00
|
|
|
aff = ['0','1','2','3','4','5','6']
|
2017-11-01 16:01:10 +00:00
|
|
|
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']
|
2016-11-01 23:52:56 +00:00
|
|
|
query1 = Q()
|
2017-11-01 16:01:10 +00:00
|
|
|
for s in user_state:
|
2016-11-01 23:52:56 +00:00
|
|
|
query1 = query1 | Q(state = s)
|
|
|
|
|
2017-11-01 16:01:10 +00:00
|
|
|
connexion = []
|
2016-11-01 23:52:56 +00:00
|
|
|
|
2016-11-24 17:12:11 +00:00
|
|
|
recherche = {'users_list': None, 'machines_list' : [], 'facture_list' : None, 'ban_list' : None, 'white_list': None, 'port_list': None, 'switch_list': None}
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-01-15 14:55:37 +00:00
|
|
|
if request.user.has_perms(('cableur',)):
|
2017-10-23 03:02:55 +00:00
|
|
|
query = Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)
|
2017-01-15 14:55:37 +00:00
|
|
|
else:
|
2017-10-23 03:02:55 +00:00
|
|
|
query = (Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user)
|
2017-01-15 14:55:37 +00:00
|
|
|
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2016-07-04 21:56:51 +00:00
|
|
|
for i in aff:
|
|
|
|
if i == '0':
|
2017-10-25 21:37:12 +00:00
|
|
|
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
|
2017-01-15 14:55:37 +00:00
|
|
|
if request.user.has_perms(('cableur',)):
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct()
|
2017-01-15 14:55:37 +00:00
|
|
|
else :
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname').distinct()
|
2016-07-04 21:56:51 +00:00
|
|
|
if i == '1':
|
2017-10-23 03:02:55 +00:00
|
|
|
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)
|
2017-01-15 14:55:37 +00:00
|
|
|
if request.user.has_perms(('cableur',)):
|
2017-09-06 13:06:31 +00:00
|
|
|
data = Interface.objects.filter(query_machine_list).distinct()
|
2017-01-15 14:55:37 +00:00
|
|
|
else:
|
2017-09-06 13:06:31 +00:00
|
|
|
data = Interface.objects.filter(query_machine_list & Q(machine__user__id = request.user.id)).distinct()
|
2016-11-24 17:12:11 +00:00
|
|
|
for d in data:
|
|
|
|
recherche['machines_list'].append(d.machine)
|
2016-11-01 14:29:12 +00:00
|
|
|
if i == '2':
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['facture_list'] = Facture.objects.filter(query & date_query).distinct()
|
2016-11-01 14:29:12 +00:00
|
|
|
if i == '3':
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['ban_list'] = Ban.objects.filter(query).distinct()
|
2016-11-01 14:29:12 +00:00
|
|
|
if i == '4':
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['white_list'] = Whitelist.objects.filter(query).distinct()
|
2016-11-01 14:29:12 +00:00
|
|
|
if i == '5':
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['port_list'] = Port.objects.filter(details__icontains = search).distinct()
|
2017-01-15 14:55:37 +00:00
|
|
|
if not request.user.has_perms(('cableur',)):
|
|
|
|
recherche['port_list'] = None
|
2016-11-01 14:29:12 +00:00
|
|
|
if i == '6':
|
2017-09-06 13:06:31 +00:00
|
|
|
recherche['switch_list'] = Switch.objects.filter(details__icontains = search).distinct()
|
2017-01-15 14:55:37 +00:00
|
|
|
if not request.user.has_perms(('cableur',)):
|
|
|
|
recherche['switch_list'] = None
|
2017-06-25 23:29:34 +00:00
|
|
|
options, created = GeneralOption.objects.get_or_create()
|
|
|
|
search_display_page = options.search_display_page
|
2016-11-01 14:29:12 +00:00
|
|
|
|
|
|
|
for r in recherche:
|
|
|
|
if recherche[r] != None:
|
2017-06-25 23:29:34 +00:00
|
|
|
recherche[r] = recherche[r][:search_display_page]
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-06-25 23:29:34 +00:00
|
|
|
recherche.update({'max_result': search_display_page})
|
2016-11-01 14:29:12 +00:00
|
|
|
|
|
|
|
return recherche
|
2016-07-04 21:56:51 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-04 21:56:51 +00:00
|
|
|
def search(request):
|
2017-11-01 16:01:10 +00:00
|
|
|
search_form = SearchForm(request.GET or None)
|
|
|
|
if search_form.is_valid():
|
|
|
|
return render(request, 'search/index.html', search_result(search_form, False, request))
|
|
|
|
return render(request, 'search/search.html', {'search_form' : search_form})
|
2016-07-04 21:56:51 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2016-07-04 21:56:51 +00:00
|
|
|
def searchp(request):
|
2017-11-01 16:01:10 +00:00
|
|
|
search_form = SearchFormPlus(request.GET or None)
|
|
|
|
if search_form.is_valid():
|
|
|
|
return render(request, 'search/index.html', search_result(search_form, True, request))
|
|
|
|
return render(request, 'search/search.html', {'search_form' : search_form})
|