views.py 4.14 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
16
from users.views import has_access
17
from cotisations.views import end_adhesion
Dalahro's avatar
Dalahro committed
18 19 20 21 22 23

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
24
@login_required
Dalahro's avatar
Dalahro committed
25 26 27 28 29
def search_result(search, type):
    date_deb = None
    date_fin = None 
    states=[]
    co=[]
Dalahro's avatar
Dalahro committed
30
    aff=[]
Dalahro's avatar
Dalahro committed
31 32 33 34 35 36 37 38
    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
39
        aff = ['0','1','2','3','4','5','6']
Dalahro's avatar
Dalahro committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
    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
54 55
    switchlist = None
    portlist = None
Dalahro's avatar
Dalahro committed
56 57 58 59
    connexion = []

    for i in aff:
        if i == '0':
60
            users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query)
61 62
            connexion = []
            for user in users:
63
                end=end_adhesion(user)
Dalahro's avatar
Dalahro committed
64 65
                access=has_access(user)
                if(len(co)==0 or (len(co)==1 and bool(co[0])==access) or (len(co)==2 and (bool(co[0])==access or bool(co[1])==access))):
66 67 68 69
                    if(end!=None):
                        connexion.append([user, access, end])
                    else:
                        connexion.append([user, access, "Non adhérent"])
Dalahro's avatar
Dalahro committed
70 71
        query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
        if i == '1':
72
            machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search))
Dalahro's avatar
Dalahro committed
73 74 75
        if i == '2':   
            factures = Facture.objects.filter(query & date_query)
        if i == '3':    
Dalahro's avatar
Dalahro committed
76
            bans = Ban.objects.filter(query)
Dalahro's avatar
Dalahro committed
77 78
        if i == '4':    
            whitelists = Whitelist.objects.filter(query)
Dalahro's avatar
Dalahro committed
79 80 81 82
        if i == '5':    
            portlist = Port.objects.filter(details__icontains = search)
        if i == '6':    
            switchlist = Switch.objects.filter(details__icontains = search)
83
    return {'users_list': connexion, 'interfaces_list' : machines, 'facture_list' : factures, 'ban_list' : bans, 'white_list': whitelists, 'port_list':portlist, 'switch_list':switchlist}
Dalahro's avatar
Dalahro committed
84

chirac's avatar
chirac committed
85
@login_required
Dalahro's avatar
Dalahro committed
86 87 88 89 90
def search(request):
    if request.method == 'POST':
        search = SearchForm(request.POST or None)
        if search.is_valid():
            return form(search_result(search, False), 'search/index.html',request)
Dalahro's avatar
Dalahro committed
91 92 93 94
        return form({'searchform' : search}, 'search/search.html', request)
    else:
        search = SearchForm(request.POST or None) 
        return form({'searchform': search}, 'search/search.html',request)
Dalahro's avatar
Dalahro committed
95

chirac's avatar
chirac committed
96
@login_required
Dalahro's avatar
Dalahro committed
97 98 99 100 101 102 103 104 105
def searchp(request):
    if request.method == 'POST':
        search = SearchFormPlus(request.POST or None)
        if search.is_valid():
            return form(search_result(search, True), 'search/index.html',request)
        return form({'searchform' : search}, 'search/search.html', request)
    else:
        search = SearchFormPlus(request.POST or None) 
        return form({'searchform': search}, 'search/search.html',request)