1
0
mirror of https://gitlab.crans.org/mediatek/med.git synced 2024-12-26 05:42:23 +00:00
med/search/views.py

96 lines
3.6 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
states = []
aff = []
if (type):
2017-06-30 01:25:07 +00:00
aff = search.cleaned_data['affichage']
states = search.cleaned_data['filtre']
date_deb = search.cleaned_data['date_deb']
date_fin = search.cleaned_data['date_fin']
date_query = Q()
2019-08-02 12:57:53 +00:00
if aff == []:
aff = ['0', '1', '2', '3']
2017-06-30 01:25:07 +00:00
if date_deb != None:
date_query = date_query & Q(date_emprunt__gte=date_deb)
if date_fin != None:
date_query = date_query & Q(date_emprunt__lte=date_fin)
search = search.cleaned_data['search_field']
query1 = Q()
for s in states:
2019-08-02 12:57:53 +00:00
query1 = query1 | Q(state=s)
connexion = []
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-02 12:57:53 +00:00
query = Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q(user__surname__icontains=search)
2017-06-30 01:25:07 +00:00
else:
2019-08-02 12:57:53 +00:00
query = (Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q(
user__surname__icontains=search)) & Q(user=request.user)
2017-06-30 01:25:07 +00:00
for i in aff:
if i == '0':
2019-08-02 12:57:53 +00:00
query_user_list = Q(pseudo__icontains=search) | Q(name__icontains=search) | Q(
surname__icontains=search) & query1
2017-06-30 01:25:07 +00:00
if request.user.has_perms(('perm',)):
recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname')
2019-08-02 12:57:53 +00:00
else:
recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state',
'surname')
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(
Q(nom__icontains=search) | Q(proprietaire__pseudo__icontains=search) | Q(
proprietaire__name__icontains=search) | Q(proprietaire__surname__icontains=search))
2017-06-30 01:25:07 +00:00
for r in recherche:
if recherche[r] != 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)