views.py 4.48 KB
Newer Older
Med's avatar
Med committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# Re2o est  un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# App de recherche pour re2o
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
# Gplv2
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.template.context_processors import csrf
from django.template import Context, RequestContext, loader
from django.contrib.auth.decorators import login_required

from django.db.models import Q
from users.models import User
from search.forms import SearchForm, SearchFormPlus

Med's avatar
Med committed
36 37
from med.settings import SEARCH_DISPLAY_PAGE

Med's avatar
Med committed
38
from media.models import Media, Jeu, Emprunt
Med's avatar
Med committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

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

def search_result(search, type, request):
    date_deb = None
    date_fin = None
    states=[]
    aff=[]
    if(type):
        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()
    if aff==[]:
Med's avatar
Med committed
57
        aff = ['0','1','2','3']
Med's avatar
Med committed
58 59 60 61 62 63 64 65 66 67 68
    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:
        query1 = query1 | Q(state = s)
    
    connexion = [] 
   
Med's avatar
Med committed
69
    recherche = {'users_list': None, 'emprunts_list' : None, 'medias_list' : None, 'jeux_list': None}
Med's avatar
Med committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    if request.user.has_perms(('perm',)):
        query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
    else:
        query = (Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)) & Q(user = request.user)


    for i in aff:
        if i == '0':
            query_user_list = Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search) & query1
            if request.user.has_perms(('perm',)):
                recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname')
            else :
                recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state', 'surname')
        if i == '1':
            recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse()
        if i == '2':
            recherche['medias_list'] = Media.objects.filter(Q(auteur__nom__icontains = search) | Q(titre__icontains = search))
Med's avatar
Med committed
88 89 90
        if i == '3':
            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))

Med's avatar
Med committed
91 92 93

    for r in recherche:
        if recherche[r] != None:
Med's avatar
Med committed
94
            recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE]
Med's avatar
Med committed
95

Med's avatar
Med committed
96
    recherche.update({'max_result': SEARCH_DISPLAY_PAGE})
Med's avatar
Med committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    return recherche

@login_required
def search(request):
    search = SearchForm(request.POST or None)
    if search.is_valid():
        return form(search_result(search, False, request), 'search/index.html',request)
    return form({'searchform' : search}, 'search/search.html', request)

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