views.py 5.91 KB
Newer Older
lhark's avatar
lhark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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.

Dalahro's avatar
Dalahro committed
23
# App de recherche pour re2o
chirac's avatar
chirac committed
24
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
Dalahro's avatar
Dalahro committed
25
# Gplv2
26 27 28

from __future__ import unicode_literals

Dalahro's avatar
Dalahro committed
29
from django.shortcuts import render
Gabriel Detraz's avatar
Gabriel Detraz committed
30
from django.shortcuts import get_object_or_404
31
from django.template.context_processors import csrf
Dalahro's avatar
Dalahro committed
32
from django.template import Context, RequestContext, loader
chirac's avatar
chirac committed
33
from django.contrib.auth.decorators import login_required
Dalahro's avatar
Dalahro committed
34 35

from django.db.models import Q
Dalahro's avatar
Dalahro committed
36
from users.models import User, Ban, Whitelist
37
from machines.models import Machine, Interface
Dalahro's avatar
Dalahro committed
38
from topologie.models import Port, Switch
Dalahro's avatar
Dalahro committed
39
from cotisations.models import Facture
Dalahro's avatar
Dalahro committed
40
from search.models import SearchForm, SearchFormPlus
41
from preferences.models import GeneralOption
42

Dalahro's avatar
Dalahro committed
43 44 45
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
Gabriel Detraz's avatar
Gabriel Detraz committed
46
    return render(request, template, c)
Dalahro's avatar
Dalahro committed
47

chirac's avatar
chirac committed
48
def search_result(search, type, request):
Dalahro's avatar
Dalahro committed
49
    date_deb = None
50
    date_fin = None
Dalahro's avatar
Dalahro committed
51 52
    states=[]
    co=[]
Dalahro's avatar
Dalahro committed
53
    aff=[]
Dalahro's avatar
Dalahro committed
54 55 56 57 58 59 60 61
    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==[]:
Dalahro's avatar
Dalahro committed
62
        aff = ['0','1','2','3','4','5','6']
Dalahro's avatar
Dalahro committed
63 64 65 66 67
    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']
68
    query1 = Q()
Dalahro's avatar
Dalahro committed
69
    for s in states:
70 71 72 73
        query1 = query1 | Q(state = s)
    
    connexion = [] 
   
74
    recherche = {'users_list': None, 'machines_list' : [], 'facture_list' : None, 'ban_list' : None, 'white_list': None, 'port_list': None, 'switch_list': None}
75

76
    if request.user.has_perms(('cableur',)):
77
        query = Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)
78
    else:
79
        query = (Q(user__pseudo__icontains = search) | Q(user__adherent__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user)
80

81

Dalahro's avatar
Dalahro committed
82 83
    for i in aff:
        if i == '0':
84
            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
85
            if request.user.has_perms(('cableur',)):
86
                recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname').distinct()
87
            else :
88
                recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname').distinct()
Dalahro's avatar
Dalahro committed
89
        if i == '1':
90
            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)
91
            if request.user.has_perms(('cableur',)):
92
                data = Interface.objects.filter(query_machine_list).distinct()
93
            else:
94
                data = Interface.objects.filter(query_machine_list & Q(machine__user__id = request.user.id)).distinct()
95 96
            for d in data:
                  recherche['machines_list'].append(d.machine)
97
        if i == '2':
98
            recherche['facture_list'] = Facture.objects.filter(query & date_query).distinct()
99
        if i == '3':
100
            recherche['ban_list'] = Ban.objects.filter(query).distinct()
101
        if i == '4':
102
            recherche['white_list'] = Whitelist.objects.filter(query).distinct()
103
        if i == '5':
104
            recherche['port_list'] = Port.objects.filter(details__icontains = search).distinct()
105 106
            if not request.user.has_perms(('cableur',)):
                recherche['port_list'] = None
107
        if i == '6':
108
            recherche['switch_list'] = Switch.objects.filter(details__icontains = search).distinct()
109 110
            if not request.user.has_perms(('cableur',)):
                recherche['switch_list'] = None
111 112
    options, created = GeneralOption.objects.get_or_create()
    search_display_page = options.search_display_page
113 114 115

    for r in recherche:
        if recherche[r] != None:
116
            recherche[r] = recherche[r][:search_display_page]
117

118
    recherche.update({'max_result': search_display_page})
119 120

    return recherche
Dalahro's avatar
Dalahro committed
121

chirac's avatar
chirac committed
122
@login_required
Dalahro's avatar
Dalahro committed
123
def search(request):
chirac's avatar
chirac committed
124 125 126 127
    search = SearchForm(request.POST or None)
    if search.is_valid():
        return form(search_result(search, False, request), 'search/index.html',request)
    return form({'searchform' : search}, 'search/search.html', request)
Dalahro's avatar
Dalahro committed
128

chirac's avatar
chirac committed
129
@login_required
Dalahro's avatar
Dalahro committed
130
def searchp(request):
chirac's avatar
chirac committed
131 132 133 134
    search = SearchFormPlus(request.POST or None)
    if search.is_valid():
        return form(search_result(search, True, request), 'search/index.html',request)
    return form({'searchform' : search}, 'search/search.html', request)