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
|
|
|
|
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
|
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
|
2016-07-04 21:56:51 +00:00
|
|
|
from search.models import SearchForm, SearchFormPlus
|
2016-07-02 22:27:22 +00:00
|
|
|
|
|
|
|
def form(ctx, template, request):
|
|
|
|
c = ctx
|
|
|
|
c.update(csrf(request))
|
|
|
|
return render_to_response(template, c, context_instance=RequestContext(request))
|
|
|
|
|
2016-07-09 15:45:13 +00:00
|
|
|
def search_result(search, type, request):
|
2016-07-04 21:56:51 +00:00
|
|
|
date_deb = None
|
|
|
|
date_fin = None
|
|
|
|
states=[]
|
|
|
|
co=[]
|
2016-07-06 21:53:44 +00:00
|
|
|
aff=[]
|
2016-07-04 21:56:51 +00:00
|
|
|
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==[]:
|
2016-07-06 21:53:44 +00:00
|
|
|
aff = ['0','1','2','3','4','5','6']
|
2016-07-04 21:56:51 +00:00
|
|
|
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
|
2016-07-06 21:53:44 +00:00
|
|
|
switchlist = None
|
|
|
|
portlist = None
|
2016-07-04 21:56:51 +00:00
|
|
|
connexion = []
|
|
|
|
|
|
|
|
for i in aff:
|
|
|
|
if i == '0':
|
2016-07-03 22:03:33 +00:00
|
|
|
users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query)
|
2016-07-04 21:56:51 +00:00
|
|
|
query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
|
|
|
|
if i == '1':
|
2016-07-04 00:48:24 +00:00
|
|
|
machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search))
|
2016-07-04 21:56:51 +00:00
|
|
|
if i == '2':
|
|
|
|
factures = Facture.objects.filter(query & date_query)
|
|
|
|
if i == '3':
|
2016-07-02 22:27:22 +00:00
|
|
|
bans = Ban.objects.filter(query)
|
2016-07-04 21:56:51 +00:00
|
|
|
if i == '4':
|
|
|
|
whitelists = Whitelist.objects.filter(query)
|
2016-07-06 21:53:44 +00:00
|
|
|
if i == '5':
|
|
|
|
portlist = Port.objects.filter(details__icontains = search)
|
|
|
|
if i == '6':
|
|
|
|
switchlist = Switch.objects.filter(details__icontains = search)
|
2016-07-10 02:02:48 +00:00
|
|
|
return {'users_list': users, 'interfaces_list' : machines, 'facture_list' : factures, 'ban_list' : bans, 'white_list': whitelists, 'port_list':portlist, 'switch_list':switchlist}
|
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):
|
2016-07-09 15:45:13 +00:00
|
|
|
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)
|
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):
|
2016-07-09 15:45:13 +00:00
|
|
|
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)
|