views.py 3.61 KB
Newer Older
erdnaxe's avatar
erdnaxe committed
1
2
3
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
Med's avatar
Med committed
4

erdnaxe's avatar
erdnaxe committed
5
from django.contrib.auth.decorators import login_required
Med's avatar
Med committed
6
from django.db.models import Q
erdnaxe's avatar
erdnaxe committed
7
8
from django.shortcuts import render
from django.template.context_processors import csrf
Med's avatar
Med committed
9

Med's avatar
Med committed
10
from med.settings import SEARCH_DISPLAY_PAGE
Med's avatar
Med committed
11
from media.models import Media, Jeu, Emprunt
erdnaxe's avatar
erdnaxe committed
12
13
14
from search.forms import SearchForm, SearchFormPlus
from users.models import User

Med's avatar
Med committed
15
16
17
18
19
20

def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render(request, template, c)

erdnaxe's avatar
erdnaxe committed
21

Med's avatar
Med committed
22
23
24
def search_result(search, type, request):
    date_deb = None
    date_fin = None
erdnaxe's avatar
erdnaxe committed
25
    aff = []
erdnaxe's avatar
erdnaxe committed
26
    if type:
Med's avatar
Med committed
27
28
29
30
        aff = search.cleaned_data['affichage']
        date_deb = search.cleaned_data['date_deb']
        date_fin = search.cleaned_data['date_fin']
    date_query = Q()
erdnaxe's avatar
erdnaxe committed
31
    if not aff:
erdnaxe's avatar
erdnaxe committed
32
        aff = ['0', '1', '2', '3']
erdnaxe's avatar
erdnaxe committed
33
    if date_deb is not None:
Med's avatar
Med committed
34
        date_query = date_query & Q(date_emprunt__gte=date_deb)
erdnaxe's avatar
erdnaxe committed
35
    if date_fin is not None:
Med's avatar
Med committed
36
37
38
        date_query = date_query & Q(date_emprunt__lte=date_fin)
    search = search.cleaned_data['search_field']
    query1 = Q()
erdnaxe's avatar
erdnaxe committed
39

erdnaxe's avatar
erdnaxe committed
40
41
    recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None,
                 'jeux_list': None}
Med's avatar
Med committed
42
43

    if request.user.has_perms(('perm',)):
erdnaxe's avatar
erdnaxe committed
44
45
        query = Q(user__username__icontains=search) | Q(
            user__first_name__icontains=search) | Q(
erdnaxe's avatar
erdnaxe committed
46
            user__last_name__icontains=search)
Med's avatar
Med committed
47
    else:
erdnaxe's avatar
erdnaxe committed
48
49
        query = (Q(user__username__icontains=search) | Q(
            user__first_name__icontains=search) | Q(
erdnaxe's avatar
erdnaxe committed
50
            user__last_name__icontains=search)) & Q(user=request.user)
Med's avatar
Med committed
51
52
53

    for i in aff:
        if i == '0':
erdnaxe's avatar
erdnaxe committed
54
55
            query_user_list = Q(username__icontains=search) | Q(
                first_name__icontains=search) | Q(
erdnaxe's avatar
erdnaxe committed
56
                last_name__icontains=search) & query1
Med's avatar
Med committed
57
            if request.user.has_perms(('perm',)):
erdnaxe's avatar
erdnaxe committed
58
59
                recherche['users_list'] = User.objects.filter(
                    query_user_list).order_by('last_name')
erdnaxe's avatar
erdnaxe committed
60
            else:
erdnaxe's avatar
erdnaxe committed
61
62
                recherche['users_list'] = User.objects.filter(
                    query_user_list & Q(id=request.user.id)).order_by(
erdnaxe's avatar
erdnaxe committed
63
                    'last_name')
Med's avatar
Med committed
64
        if i == '1':
erdnaxe's avatar
erdnaxe committed
65
66
            recherche['emprunts_list'] = Emprunt.objects.filter(
                query & date_query).order_by('date_emprunt').reverse()
Med's avatar
Med committed
67
        if i == '2':
erdnaxe's avatar
erdnaxe committed
68
69
            recherche['medias_list'] = Media.objects.filter(
                Q(auteur__nom__icontains=search) | Q(titre__icontains=search))
Med's avatar
Med committed
70
        if i == '3':
erdnaxe's avatar
erdnaxe committed
71
            recherche['jeux_list'] = Jeu.objects.filter(
erdnaxe's avatar
erdnaxe committed
72
73
74
75
                Q(nom__icontains=search) | Q(
                    proprietaire__username__icontains=search) | Q(
                    proprietaire__first_name__icontains=search) | Q(
                    proprietaire__last_name__icontains=search))
Med's avatar
Med committed
76
77

    for r in recherche:
erdnaxe's avatar
erdnaxe committed
78
        if recherche[r] is not None:
Med's avatar
Med committed
79
            recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE]
Med's avatar
Med committed
80

Med's avatar
Med committed
81
    recherche.update({'max_result': SEARCH_DISPLAY_PAGE})
Med's avatar
Med committed
82
83
84

    return recherche

erdnaxe's avatar
erdnaxe committed
85

Med's avatar
Med committed
86
87
88
89
@login_required
def search(request):
    search = SearchForm(request.POST or None)
    if search.is_valid():
erdnaxe's avatar
erdnaxe committed
90
91
        return form(search_result(search, False, request), 'search/index.html',
                    request)
erdnaxe's avatar
erdnaxe committed
92
93
    return form({'searchform': search}, 'search/search.html', request)

Med's avatar
Med committed
94
95
96
97
98

@login_required
def searchp(request):
    search = SearchFormPlus(request.POST or None)
    if search.is_valid():
erdnaxe's avatar
erdnaxe committed
99
100
        return form(search_result(search, True, request), 'search/index.html',
                    request)
erdnaxe's avatar
erdnaxe committed
101
    return form({'searchform': search}, 'search/search.html', request)