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
|
2017-06-25 23:29:34 +00:00
|
|
|
from preferences.models import GeneralOption
|
2017-11-01 23:19:41 +00:00
|
|
|
from search.forms import (
|
|
|
|
SearchForm,
|
|
|
|
SearchFormPlus,
|
|
|
|
CHOICES_USER,
|
|
|
|
CHOICES_AFF,
|
|
|
|
initial_choices
|
|
|
|
)
|
2017-11-02 00:25:24 +00:00
|
|
|
from re2o.utils import SortTable
|
2016-10-12 20:58:41 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
def get_results(query, request, filters={}):
|
|
|
|
start = filters.get('s', None)
|
|
|
|
end = filters.get('e', None)
|
|
|
|
user_state = filters.get('u', initial_choices(CHOICES_USER))
|
|
|
|
aff = filters.get('a', initial_choices(CHOICES_AFF))
|
|
|
|
|
|
|
|
options, created = GeneralOption.objects.get_or_create()
|
|
|
|
max_result = options.search_display_page
|
|
|
|
|
|
|
|
user_state_filter = Q()
|
2017-11-01 16:01:10 +00:00
|
|
|
for s in user_state:
|
2017-11-01 23:19:41 +00:00
|
|
|
user_state_filter |= Q(state = s)
|
2016-11-01 23:52:56 +00:00
|
|
|
|
2017-11-01 16:01:10 +00:00
|
|
|
connexion = []
|
2016-11-01 23:52:56 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
results = {
|
|
|
|
'users_list': User.objects.none(),
|
|
|
|
'machines_list' : Machine.objects.none(),
|
|
|
|
'facture_list' : Facture.objects.none(),
|
|
|
|
'ban_list' : Ban.objects.none(),
|
|
|
|
'white_list': Whitelist.objects.none(),
|
|
|
|
'port_list': Port.objects.none(),
|
|
|
|
'switch_list': Switch.objects.none()
|
|
|
|
}
|
|
|
|
|
|
|
|
users_filter = Q(
|
|
|
|
user__pseudo__icontains = query
|
|
|
|
) | Q(
|
|
|
|
user__adherent__name__icontains = query
|
|
|
|
) | Q(
|
|
|
|
user__surname__icontains = query
|
|
|
|
)
|
|
|
|
if not request.user.has_perms(('cableur',)):
|
|
|
|
users_filter &= Q(user = request.user)
|
|
|
|
|
|
|
|
# Users
|
|
|
|
if '0' in aff:
|
|
|
|
filter_user_list = Q(
|
|
|
|
adherent__room__name__icontains = query
|
|
|
|
) | Q(
|
|
|
|
club__room__name__icontains = query
|
|
|
|
) | Q(
|
|
|
|
pseudo__icontains = query
|
|
|
|
) | Q(
|
|
|
|
adherent__name__icontains = query
|
|
|
|
) | Q(
|
|
|
|
surname__icontains = query
|
|
|
|
) & user_state_filter
|
|
|
|
if not request.user.has_perms(('cableur',)):
|
|
|
|
filter_user_list &= Q(id=request.user.id)
|
2017-11-02 00:25:24 +00:00
|
|
|
results['users_list'] = User.objects.filter(filter_user_list)
|
|
|
|
results['users_list'] = SortTable.sort(
|
|
|
|
results['users_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.USERS_INDEX
|
|
|
|
)
|
2017-11-01 23:19:41 +00:00
|
|
|
|
|
|
|
# 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',)):
|
2017-11-02 13:43:41 +00:00
|
|
|
filter_machine_list &= Q(user__id=request.user.id)
|
2017-11-02 00:25:24 +00:00
|
|
|
results['machines_list'] = Machine.objects.filter(filter_machine_list)
|
|
|
|
results['machines_list'] = SortTable.sort(
|
|
|
|
results['machines_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.MACHINES_INDEX
|
|
|
|
)
|
2017-11-01 23:19:41 +00:00
|
|
|
|
|
|
|
# Factures
|
|
|
|
if '2' in aff:
|
2017-11-02 00:25:24 +00:00
|
|
|
filter_facture_list = users_filter
|
2017-11-01 23:19:41 +00:00
|
|
|
if start != None:
|
2017-11-02 00:25:24 +00:00
|
|
|
filter_facture_list &= Q(date__gte=start)
|
2017-11-01 23:19:41 +00:00
|
|
|
if end != None:
|
2017-11-02 00:25:24 +00:00
|
|
|
filter_facture_list &= Q(date__lte=end)
|
|
|
|
results['facture_list'] = Facture.objects.filter(filter_facture_list)
|
|
|
|
results['facture_list'] = SortTable.sort(
|
|
|
|
results['facture_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.COTISATIONS_INDEX
|
|
|
|
)
|
2017-11-01 23:19:41 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
)
|
2017-11-02 00:25:24 +00:00
|
|
|
results['ban_list'] = Ban.objects.filter(users_filter & date_filter)
|
|
|
|
results['ban_list'] = SortTable.sort(
|
|
|
|
results['ban_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.USERS_INDEX_BAN
|
|
|
|
)
|
2017-11-01 23:19:41 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
)
|
2017-11-02 00:25:24 +00:00
|
|
|
results['white_list'] = Whitelist.objects.filter(users_filter & date_filter)
|
|
|
|
results['white_list'] = SortTable.sort(
|
|
|
|
results['white_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.USERS_INDEX_WHITE
|
|
|
|
)
|
2017-11-01 23:19:41 +00:00
|
|
|
|
|
|
|
# Switch ports
|
|
|
|
if '5' in aff and request.user.has_perms(('cableur',)):
|
2017-11-02 00:25:24 +00:00
|
|
|
results['port_list'] = Port.objects.filter(details__icontains=query)
|
|
|
|
results['port_list'] = SortTable.sort(
|
|
|
|
results['port_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.TOPOLOGIE_INDEX_PORT
|
|
|
|
)
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
# Switches
|
|
|
|
if '6' in aff and request.user.has_perms(('cableur')):
|
2017-11-02 00:25:24 +00:00
|
|
|
results['switch_list'] = Switch.objects.filter(details__icontains=query)
|
|
|
|
results['switch_list'] = SortTable.sort(
|
|
|
|
results['switch_list'],
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.TOPOLOGIE_INDEX
|
|
|
|
)
|
|
|
|
|
|
|
|
for r in results.keys():
|
|
|
|
results[r] = results[r].distinct()[:max_result]
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
results.update({'max_result': max_result})
|
2017-11-02 00:35:22 +00:00
|
|
|
results.update({'search_term': query})
|
2016-11-01 14:29:12 +00:00
|
|
|
|
2017-11-01 23:19:41 +00:00
|
|
|
return results
|
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():
|
2017-11-01 23:19:41 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'search/index.html',
|
|
|
|
get_results(
|
|
|
|
search_form.cleaned_data.get('q', ''),
|
|
|
|
request,
|
|
|
|
search_form.cleaned_data
|
|
|
|
)
|
|
|
|
)
|
2017-11-01 16:01:10 +00:00
|
|
|
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():
|
2017-11-01 23:19:41 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'search/index.html',
|
|
|
|
get_results(
|
|
|
|
search_form.cleaned_data.get('q', ''),
|
|
|
|
request,
|
|
|
|
search_form.cleaned_data
|
|
|
|
)
|
|
|
|
)
|
2017-11-01 16:01:10 +00:00
|
|
|
return render(request, 'search/search.html', {'search_form' : search_form})
|