2017-06-16 00:17:25 +02:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
|
|
|
from django import forms
|
2017-06-17 00:34:11 +02:00
|
|
|
from django.db.models import Q
|
2017-06-16 00:17:25 +02:00
|
|
|
|
2017-06-17 00:34:11 +02:00
|
|
|
from .documents import StageDocument
|
|
|
|
from .models import Stage
|
|
|
|
from .statics import TYPE_LIEU_OPTIONS, TYPE_STAGE_OPTIONS, NIVEAU_SCOL_OPTIONS
|
|
|
|
|
|
|
|
from datetime import date
|
2017-06-16 00:17:25 +02:00
|
|
|
|
|
|
|
# Recherche
|
|
|
|
class SearchForm(forms.Form):
|
2017-06-17 00:34:11 +02:00
|
|
|
generique = forms.CharField(required=False)
|
|
|
|
sujet = forms.CharField(label=u'À propos de', required=False)
|
|
|
|
sujet.full_line = True
|
|
|
|
|
|
|
|
apres_annee = forms.IntegerField(label=u'Après cette année', required=False)
|
|
|
|
avant_annee = forms.IntegerField(label=u'Avant cette année', required=False)
|
|
|
|
|
|
|
|
type_stage = forms.ChoiceField(label="", choices=([('', u'Type de stage')]
|
|
|
|
+ list(TYPE_STAGE_OPTIONS)),
|
|
|
|
required=False)
|
|
|
|
niveau_scol = forms.ChoiceField(label="", choices=([('', u'Année d\'études')]
|
|
|
|
+ list(NIVEAU_SCOL_OPTIONS)),
|
|
|
|
required=False)
|
|
|
|
|
|
|
|
contexte = forms.CharField(label=u'Contexte (lieu, encadrant⋅e⋅s, structure)',
|
|
|
|
required=False)
|
|
|
|
type_lieu = forms.ChoiceField(choices=([('', u'Type de lieu d\'accueil')]
|
|
|
|
+ list(TYPE_LIEU_OPTIONS)),
|
|
|
|
required=False)
|
|
|
|
tri = forms.ChoiceField(label=u'Trier par',
|
|
|
|
choices=[('pertinence', u'Par pertinence'),
|
|
|
|
('-date_maj',u'Par dernière mise à jour')],
|
|
|
|
required=False, initial='pertinence')
|
|
|
|
|
2017-06-16 00:17:25 +02:00
|
|
|
|
|
|
|
def cherche(**kwargs):
|
2017-06-17 00:34:11 +02:00
|
|
|
filtres = Q(public=True)
|
|
|
|
dsl = StageDocument.search()
|
|
|
|
|
|
|
|
use_dsl = False
|
|
|
|
|
|
|
|
def field_relevant(field, test_string=True):
|
|
|
|
return field in kwargs and \
|
|
|
|
kwargs[field] is not None and \
|
|
|
|
((not test_string) or kwargs[field].strip() != '')
|
|
|
|
|
|
|
|
#
|
|
|
|
# Recherche libre
|
|
|
|
#
|
|
|
|
|
|
|
|
# Champ générique : recherche dans tous les champs
|
|
|
|
if field_relevant("generique"):
|
|
|
|
print "Filtre generique", kwargs['generique']
|
|
|
|
dsl = dsl.filter(
|
2017-06-16 00:17:25 +02:00
|
|
|
"match",
|
|
|
|
_all={"query": kwargs["generique"],
|
|
|
|
"fuzziness": "auto"})
|
2017-06-17 00:34:11 +02:00
|
|
|
use_dsl = True
|
|
|
|
|
|
|
|
# Sujet -> Recherche dan les noms de sujets et les thématiques
|
|
|
|
if field_relevant("sujet"):
|
|
|
|
dsl = dsl.filter("multi_match",
|
|
|
|
query = kwargs["sujet"],
|
|
|
|
fields = ['sujet^2', 'thematiques'],
|
|
|
|
fuzziness = "auto")
|
|
|
|
use_dsl = True
|
|
|
|
|
|
|
|
# Contexte -> Encadrants, structure, lieu
|
|
|
|
if field_relevant("contexte"):
|
|
|
|
dsl = dsl.filter("multi_match",
|
|
|
|
query = kwargs["contexte"],
|
|
|
|
fields = ['encadrants', 'structure^2',
|
|
|
|
'lieux.nom', 'lieux.pays', 'lieux.ville'],
|
|
|
|
fuzziness = "auto")
|
|
|
|
use_dsl = True
|
|
|
|
|
|
|
|
#
|
|
|
|
# Filtres directs db
|
|
|
|
#
|
|
|
|
|
|
|
|
# Dates
|
|
|
|
if field_relevant('avant_annee', False):
|
|
|
|
dte = date(kwargs['avant_annee']+1, 1, 1)
|
|
|
|
filtres &= Q(date_fin__lt=dte)
|
|
|
|
|
|
|
|
if field_relevant('apres_annee', False):
|
|
|
|
dte = date(kwargs['apres_annee'], 1, 1)
|
|
|
|
filtres &= Q(date_debut__gte=dte)
|
|
|
|
|
|
|
|
# Type de stage
|
|
|
|
if field_relevant('type_stage'):
|
|
|
|
filtres &= Q(type_stage=kwargs["type_stage"])
|
|
|
|
|
|
|
|
if field_relevant('annee_scol'):
|
|
|
|
filtres &= Q(annee_scol=kwargs["annee_scol"])
|
|
|
|
|
|
|
|
# Type de lieu
|
|
|
|
if field_relevant('type_lieu'):
|
|
|
|
filtres &= Q(lieux__type_lieu=kwargs["type_lieu"])
|
|
|
|
|
|
|
|
|
|
|
|
# Application
|
|
|
|
if use_dsl:
|
|
|
|
filtres &= Q(id__in=[s.meta.id for s in dsl])
|
|
|
|
|
|
|
|
print filtres
|
|
|
|
resultat = Stage.objects.filter(filtres)
|
|
|
|
|
|
|
|
if field_relevant('tri') and kwargs['tri'] != 'pertinence':
|
|
|
|
resultat = resultat.order_by(kwargs['tri'])
|
|
|
|
return resultat
|
2017-06-16 00:17:25 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def recherche(request):
|
|
|
|
stages = []
|
|
|
|
if request.method == "GET":
|
|
|
|
form = SearchForm(request.GET)
|
|
|
|
if form.is_valid():
|
|
|
|
stages = cherche(**form.cleaned_data)
|
|
|
|
else:
|
|
|
|
form = SearchForm()
|
|
|
|
return render(request, 'avisstage/liste/recherche_resultats.html',
|
|
|
|
{"form": form, "stages":stages})
|