Format search code

This commit is contained in:
Alexandre Iooss 2019-08-08 21:54:36 +02:00
parent c225f5045e
commit d0f2140d14
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
1 changed files with 22 additions and 11 deletions

View File

@ -37,33 +37,42 @@ def search_result(search, type, request):
search = search.cleaned_data['search_field'] search = search.cleaned_data['search_field']
query1 = Q() query1 = Q()
recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None, 'jeux_list': None} recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None,
'jeux_list': None}
if request.user.has_perms(('perm',)): if request.user.has_perms(('perm',)):
query = Q(user__username__icontains=search) | Q(user__first_name__icontains=search) | Q( query = Q(user__username__icontains=search) | Q(
user__first_name__icontains=search) | Q(
user__last_name__icontains=search) user__last_name__icontains=search)
else: else:
query = (Q(user__username__icontains=search) | Q(user__first_name__icontains=search) | Q( query = (Q(user__username__icontains=search) | Q(
user__first_name__icontains=search) | Q(
user__last_name__icontains=search)) & Q(user=request.user) user__last_name__icontains=search)) & Q(user=request.user)
for i in aff: for i in aff:
if i == '0': if i == '0':
query_user_list = Q(username__icontains=search) | Q(first_name__icontains=search) | Q( query_user_list = Q(username__icontains=search) | Q(
first_name__icontains=search) | Q(
last_name__icontains=search) & query1 last_name__icontains=search) & query1
if request.user.has_perms(('perm',)): if request.user.has_perms(('perm',)):
recherche['users_list'] = User.objects.filter(query_user_list).order_by('last_name') recherche['users_list'] = User.objects.filter(
query_user_list).order_by('last_name')
else: else:
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by( recherche['users_list'] = User.objects.filter(
query_user_list & Q(id=request.user.id)).order_by(
'last_name') 'last_name')
if i == '1': if i == '1':
recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse() recherche['emprunts_list'] = Emprunt.objects.filter(
query & date_query).order_by('date_emprunt').reverse()
if i == '2': if i == '2':
recherche['medias_list'] = Media.objects.filter( recherche['medias_list'] = Media.objects.filter(
Q(auteur__nom__icontains=search) | Q(titre__icontains=search)) Q(auteur__nom__icontains=search) | Q(titre__icontains=search))
if i == '3': if i == '3':
recherche['jeux_list'] = Jeu.objects.filter( recherche['jeux_list'] = Jeu.objects.filter(
Q(nom__icontains=search) | Q(proprietaire__username__icontains=search) | Q( Q(nom__icontains=search) | Q(
proprietaire__first_name__icontains=search) | Q(proprietaire__last_name__icontains=search)) proprietaire__username__icontains=search) | Q(
proprietaire__first_name__icontains=search) | Q(
proprietaire__last_name__icontains=search))
for r in recherche: for r in recherche:
if recherche[r] is not None: if recherche[r] is not None:
@ -78,7 +87,8 @@ def search_result(search, type, request):
def search(request): def search(request):
search = SearchForm(request.POST or None) search = SearchForm(request.POST or None)
if search.is_valid(): if search.is_valid():
return form(search_result(search, False, request), 'search/index.html', request) return form(search_result(search, False, request), 'search/index.html',
request)
return form({'searchform': search}, 'search/search.html', request) return form({'searchform': search}, 'search/search.html', request)
@ -86,5 +96,6 @@ def search(request):
def searchp(request): def searchp(request):
search = SearchFormPlus(request.POST or None) search = SearchFormPlus(request.POST or None)
if search.is_valid(): if search.is_valid():
return form(search_result(search, True, request), 'search/index.html', request) return form(search_result(search, True, request), 'search/index.html',
request)
return form({'searchform': search}, 'search/search.html', request) return form({'searchform': search}, 'search/search.html', request)