med/search/views.py

91 lines
3.4 KiB
Python
Raw Normal View History

2019-08-02 12:57:53 +00:00
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2017-06-30 01:25:07 +00:00
2019-08-02 12:57:53 +00:00
from django.contrib.auth.decorators import login_required
2017-06-30 01:25:07 +00:00
from django.db.models import Q
2019-08-02 12:57:53 +00:00
from django.shortcuts import render
from django.template.context_processors import csrf
2017-06-30 01:25:07 +00:00
2017-07-03 00:41:14 +00:00
from med.settings import SEARCH_DISPLAY_PAGE
2017-07-03 18:59:50 +00:00
from media.models import Media, Jeu, Emprunt
2019-08-02 12:57:53 +00:00
from search.forms import SearchForm, SearchFormPlus
from users.models import User
2017-06-30 01:25:07 +00:00
def form(ctx, template, request):
c = ctx
c.update(csrf(request))
return render(request, template, c)
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
def search_result(search, type, request):
date_deb = None
date_fin = None
2019-08-02 12:57:53 +00:00
aff = []
2019-08-02 19:35:30 +00:00
if type:
2017-06-30 01:25:07 +00:00
aff = search.cleaned_data['affichage']
date_deb = search.cleaned_data['date_deb']
date_fin = search.cleaned_data['date_fin']
date_query = Q()
2019-08-02 19:35:30 +00:00
if not aff:
2019-08-02 12:57:53 +00:00
aff = ['0', '1', '2', '3']
2019-08-02 19:35:30 +00:00
if date_deb is not None:
2017-06-30 01:25:07 +00:00
date_query = date_query & Q(date_emprunt__gte=date_deb)
2019-08-02 19:35:30 +00:00
if date_fin is not None:
2017-06-30 01:25:07 +00:00
date_query = date_query & Q(date_emprunt__lte=date_fin)
search = search.cleaned_data['search_field']
query1 = Q()
2019-08-02 12:57:53 +00:00
recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None, 'jeux_list': None}
2017-06-30 01:25:07 +00:00
if request.user.has_perms(('perm',)):
2019-08-08 10:16:40 +00:00
query = Q(user__username__icontains=search) | Q(user__first_name__icontains=search) | Q(
2019-08-02 19:47:54 +00:00
user__last_name__icontains=search)
2017-06-30 01:25:07 +00:00
else:
2019-08-08 10:16:40 +00:00
query = (Q(user__username__icontains=search) | Q(user__first_name__icontains=search) | Q(
2019-08-02 19:47:54 +00:00
user__last_name__icontains=search)) & Q(user=request.user)
2017-06-30 01:25:07 +00:00
for i in aff:
if i == '0':
2019-08-08 10:16:40 +00:00
query_user_list = Q(username__icontains=search) | Q(first_name__icontains=search) | Q(
2019-08-02 19:47:54 +00:00
last_name__icontains=search) & query1
2017-06-30 01:25:07 +00:00
if request.user.has_perms(('perm',)):
2019-08-02 19:47:54 +00:00
recherche['users_list'] = User.objects.filter(query_user_list).order_by('last_name')
2019-08-02 12:57:53 +00:00
else:
2019-08-02 19:35:30 +00:00
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by(
2019-08-02 19:47:54 +00:00
'last_name')
2017-06-30 01:25:07 +00:00
if i == '1':
recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse()
if i == '2':
2019-08-02 12:57:53 +00:00
recherche['medias_list'] = Media.objects.filter(
Q(auteur__nom__icontains=search) | Q(titre__icontains=search))
2017-07-03 18:59:50 +00:00
if i == '3':
2019-08-02 12:57:53 +00:00
recherche['jeux_list'] = Jeu.objects.filter(
2019-08-02 19:35:30 +00:00
Q(nom__icontains=search) | Q(proprietaire__username__icontains=search) | Q(
2019-08-08 10:16:40 +00:00
proprietaire__first_name__icontains=search) | Q(proprietaire__last_name__icontains=search))
2017-06-30 01:25:07 +00:00
for r in recherche:
2019-08-02 19:35:30 +00:00
if recherche[r] is not None:
2017-07-03 00:41:14 +00:00
recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE]
2017-06-30 01:25:07 +00:00
2017-07-03 00:41:14 +00:00
recherche.update({'max_result': SEARCH_DISPLAY_PAGE})
2017-06-30 01:25:07 +00:00
return recherche
2019-08-02 12:57:53 +00:00
2017-06-30 01:25:07 +00:00
@login_required
def search(request):
search = SearchForm(request.POST or None)
if search.is_valid():
2019-08-02 12:57:53 +00:00
return form(search_result(search, False, request), 'search/index.html', request)
return form({'searchform': search}, 'search/search.html', request)
2017-06-30 01:25:07 +00:00
@login_required
def searchp(request):
search = SearchFormPlus(request.POST or None)
if search.is_valid():
2019-08-02 12:57:53 +00:00
return form(search_result(search, True, request), 'search/index.html', request)
return form({'searchform': search}, 'search/search.html', request)