views.py 4.57 KB
Newer Older
Dalahro's avatar
Dalahro committed
1
# App de recherche pour re2o
chirac's avatar
chirac committed
2
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
Dalahro's avatar
Dalahro committed
3 4
# Gplv2
from django.shortcuts import render
Gabriel Detraz's avatar
Gabriel Detraz committed
5
from django.shortcuts import get_object_or_404
6
from django.template.context_processors import csrf
Dalahro's avatar
Dalahro committed
7
from django.template import Context, RequestContext, loader
chirac's avatar
chirac committed
8
from django.contrib.auth.decorators import login_required
Dalahro's avatar
Dalahro committed
9 10

from django.db.models import Q
Dalahro's avatar
Dalahro committed
11
from users.models import User, Ban, Whitelist
12
from machines.models import Machine, Interface
Dalahro's avatar
Dalahro committed
13
from topologie.models import Port, Switch
Dalahro's avatar
Dalahro committed
14
from cotisations.models import Facture
Dalahro's avatar
Dalahro committed
15
from search.models import SearchForm, SearchFormPlus
Dalahro's avatar
Dalahro committed
16

17 18
from re2o.settings import SEARCH_RESULT

Dalahro's avatar
Dalahro committed
19 20 21
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
Gabriel Detraz's avatar
Gabriel Detraz committed
22
    return render(request, template, c)
Dalahro's avatar
Dalahro committed
23

chirac's avatar
chirac committed
24
def search_result(search, type, request):
Dalahro's avatar
Dalahro committed
25
    date_deb = None
26
    date_fin = None
Dalahro's avatar
Dalahro committed
27 28
    states=[]
    co=[]
Dalahro's avatar
Dalahro committed
29
    aff=[]
Dalahro's avatar
Dalahro committed
30 31 32 33 34 35 36 37
    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
38
        aff = ['0','1','2','3','4','5','6']
Dalahro's avatar
Dalahro committed
39 40 41 42 43
    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']
44
    query1 = Q()
Dalahro's avatar
Dalahro committed
45
    for s in states:
46 47 48 49
        query1 = query1 | Q(state = s)
    
    connexion = [] 
   
50
    recherche = {'users_list': None, 'machines_list' : [], 'facture_list' : None, 'ban_list' : None, 'white_list': None, 'port_list': None, 'switch_list': None}
51

52 53 54 55 56
    if request.user.has_perms(('cableur',)):
        query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
    else:
        query = (Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user)

57

Dalahro's avatar
Dalahro committed
58 59
    for i in aff:
        if i == '0':
60 61 62 63 64
            query_user_list = Q(room__name__icontains = search) | Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search) & query1
            if request.user.has_perms(('cableur',)):
                recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname')
            else :
                recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname')
Dalahro's avatar
Dalahro committed
65
        if i == '1':
66 67 68 69 70
            query_machine_list = Q(machine__user__pseudo__icontains = search) | Q(machine__user__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',)):
                data = Interface.objects.filter(query_machine_list)
            else:
                data = Interface.objects.filter(query_machine_list & Q(machine__user__id = request.user.id))
71 72
            for d in data:
                  recherche['machines_list'].append(d.machine)
73 74 75 76 77 78 79 80
        if i == '2':
            recherche['facture_list'] = Facture.objects.filter(query & date_query)
        if i == '3':
            recherche['ban_list'] = Ban.objects.filter(query)
        if i == '4':
            recherche['white_list'] = Whitelist.objects.filter(query)
        if i == '5':
            recherche['port_list'] = Port.objects.filter(details__icontains = search)
81 82
            if not request.user.has_perms(('cableur',)):
                recherche['port_list'] = None
83 84
        if i == '6':
            recherche['switch_list'] = Switch.objects.filter(details__icontains = search)
85 86
            if not request.user.has_perms(('cableur',)):
                recherche['switch_list'] = None
87 88 89 90 91 92 93 94

    for r in recherche:
        if recherche[r] != None:
            recherche[r] = recherche[r][:SEARCH_RESULT]

    recherche.update({'max_result': SEARCH_RESULT})

    return recherche
Dalahro's avatar
Dalahro committed
95

chirac's avatar
chirac committed
96
@login_required
Dalahro's avatar
Dalahro committed
97
def search(request):
chirac's avatar
chirac committed
98 99 100 101
    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
102

chirac's avatar
chirac committed
103
@login_required
Dalahro's avatar
Dalahro committed
104
def searchp(request):
chirac's avatar
chirac committed
105 106 107 108
    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)