8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-09 11:26:27 +00:00
re2o/search/views.py

39 lines
1.7 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
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():
search = search.cleaned_data['search_field']
users = User.objects.filter(Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search))
connexion = []
for user in users:
connexion.append([user, has_access(user)])
2016-07-02 22:27:22 +00:00
machines = None
query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
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)