views.py 3.46 KB
Newer Older
Amap's avatar
Amap committed
1
# coding: utf8
Gabriel Detraz's avatar
Gabriel Detraz committed
2 3 4 5 6 7 8 9 10 11
# App de recherche pour amap
# Augustin lemesle, Gabriel Détraz, Goulven Kermarec
# Gplv2
from django.shortcuts import render
from django.shortcuts import render_to_response, get_object_or_404
from django.core.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
12 13
from users.models import User

Gabriel Detraz's avatar
Gabriel Detraz committed
14
from search.models import SearchForm, SearchFormPlus
15
from panier.models import Commande, Panier, Credit, Livraison
Gabriel Detraz's avatar
Gabriel Detraz committed
16

Amap's avatar
Amap committed
17
from amap.settings import SEARCH_RESULT
18

Gabriel Detraz's avatar
Gabriel Detraz committed
19 20 21 22 23 24 25
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render_to_response(template, c, context_instance=RequestContext(request))

def search_result(search, type, request):
    date_deb = None
Amap's avatar
Amap committed
26 27
    date_fin = None
    date_livraison = None 
Gabriel Detraz's avatar
Gabriel Detraz committed
28 29 30 31 32 33 34 35
    states=[]
    co=[]
    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']
36
        date_livraison = search.cleaned_data['date_livraison']
Gabriel Detraz's avatar
Gabriel Detraz committed
37 38 39 40 41 42 43
    date_query = Q()
    if aff==[]:
        aff = ['0','1','2','3','4','5','6']
    if date_deb != None:
        date_query = date_query & Q(date__gte=date_deb)
    if date_fin != None:
        date_query = date_query & Q(date__lte=date_fin)
44 45
    if date_livraison != None:
        date_query = date_query & Q(date=date_livraison.date) 
Gabriel Detraz's avatar
Gabriel Detraz committed
46 47 48 49 50 51
    search = search.cleaned_data['search_field']
    query = Q() 
    for s in states:
        query = query | Q(state = s)
    
    users = None
52 53 54 55
    commande = None
    panier = None
    credit = None
    livraison = None
Gabriel Detraz's avatar
Gabriel Detraz committed
56 57 58

    for i in aff:
        if i == '0':
59
            users = User.objects.filter((Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query)[:SEARCH_RESULT]
60 61 62
            query = Q(user__pseudo__icontains = search) | Q(user__name__icontains = search) | Q(user__surname__icontains = search)
            if not request.user.has_perms(('bureau',)):
                users = [request.user]
Gabriel Detraz's avatar
Gabriel Detraz committed
63
        if i == '1':
64
            panier = Panier.objects.filter(nom__icontains = search)[:SEARCH_RESULT]
Gabriel Detraz's avatar
Gabriel Detraz committed
65
        if i == '2':   
Amap's avatar
Amap committed
66
            commande = Commande.objects.filter(Q(date=Livraison.objects.filter(date_query)) & query)
67
            if not request.user.has_perms(('bureau',)):
68
                commande = commande.filter(user=request.user)[:SEARCH_RESULT]
Gabriel Detraz's avatar
Gabriel Detraz committed
69
        if i == '3':    
70
            livraison = Livraison.objects.filter(date_query)[:SEARCH_RESULT]
Gabriel Detraz's avatar
Gabriel Detraz committed
71
        if i == '4':    
72
            credit = Credit.objects.filter(query)[:SEARCH_RESULT]
73 74
            if not request.user.has_perms(('bureau',)):
                credit = credit.filter(user=request.user)
75
    return {'users_list': users, 'article_list' : panier, 'paniers_list' : commande, 'livraison_list' : livraison, 'credit_list' : credit , 'max_result' : SEARCH_RESULT}
Gabriel Detraz's avatar
Gabriel Detraz committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89

@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)