8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 11:53:12 +00:00
re2o/search/views.py

43 lines
2 KiB
Python
Raw Normal View History

2016-07-02 22:27:22 +00:00
# App de recherche pour re2o
# Gabriel Détraz, Goulven Kermarec
# 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
from django.db.models import Q
from users.models import User, Ban
from machines.models import Machine, Interface
2016-07-02 22:27:22 +00:00
from cotisations.models import Facture
from search.models import SearchForm
from users.views import has_access
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))
def search(request):
if request.method == 'POST':
search = SearchForm(request.POST or None)
if search.is_valid():
states = search.cleaned_data['filtre']
2016-07-02 22:27:22 +00:00
search = search.cleaned_data['search_field']
query = Q()
for s in states:
query = query | Q(state = s)
users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query)
connexion = []
for user in users:
connexion.append([user, has_access(user)])
2016-07-02 22:27:22 +00:00
query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
machines = Interface.objects.filter(machine=Machine.objects.filter(query)) | Interface.objects.filter(Q(dns__icontains = search))
2016-07-02 22:27:22 +00:00
factures = Facture.objects.filter(query)
bans = Ban.objects.filter(query)
return form({'users_list': connexion, 'machine_list' : machines, 'facture_list' : factures, 'ban_list' : bans}, 'search/index.html',request)
2016-07-02 22:27:22 +00:00
return form({'searchform' : search}, 'search/search.html', request)
else:
search = SearchForm(request.POST or None)
return form({'searchform': search}, 'search/search.html',request)