views.py 3.25 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 5 6 7
# Gplv2
from django.shortcuts import render
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
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 19 20 21

def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render_to_response(template, c, context_instance=RequestContext(request))

chirac's avatar
chirac committed
22
def search_result(search, type, request):
Dalahro's avatar
Dalahro committed
23 24 25 26
    date_deb = None
    date_fin = None 
    states=[]
    co=[]
Dalahro's avatar
Dalahro committed
27
    aff=[]
Dalahro's avatar
Dalahro committed
28 29 30 31 32 33 34 35
    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
36
        aff = ['0','1','2','3','4','5','6']
Dalahro's avatar
Dalahro committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    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']
    query = Q() 
    for s in states:
        query = query | Q(state = s)
    
    users = None
    machines = None
    factures = None
    bans = None
    whitelists = None
Dalahro's avatar
Dalahro committed
51 52
    switchlist = None
    portlist = None
Dalahro's avatar
Dalahro committed
53 54 55 56
    connexion = []

    for i in aff:
        if i == '0':
57
            users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query)
Dalahro's avatar
Dalahro committed
58 59
        query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
        if i == '1':
60
            machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search))
Dalahro's avatar
Dalahro committed
61 62 63
        if i == '2':   
            factures = Facture.objects.filter(query & date_query)
        if i == '3':    
Dalahro's avatar
Dalahro committed
64
            bans = Ban.objects.filter(query)
Dalahro's avatar
Dalahro committed
65 66
        if i == '4':    
            whitelists = Whitelist.objects.filter(query)
Dalahro's avatar
Dalahro committed
67 68 69 70
        if i == '5':    
            portlist = Port.objects.filter(details__icontains = search)
        if i == '6':    
            switchlist = Switch.objects.filter(details__icontains = search)
71
    return {'users_list': users, 'interfaces_list' : machines, 'facture_list' : factures, 'ban_list' : bans, 'white_list': whitelists, 'port_list':portlist, 'switch_list':switchlist}
Dalahro's avatar
Dalahro committed
72

chirac's avatar
chirac committed
73
@login_required
Dalahro's avatar
Dalahro committed
74
def search(request):
chirac's avatar
chirac committed
75 76 77 78
    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
79

chirac's avatar
chirac committed
80
@login_required
Dalahro's avatar
Dalahro committed
81
def searchp(request):
chirac's avatar
chirac committed
82 83 84 85
    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)