experiENS/avisstage/documents.py

76 lines
2.2 KiB
Python
Raw Normal View History

2017-05-22 23:36:14 +02:00
from django_elasticsearch_dsl import DocType, Index, fields
2017-06-16 00:17:25 +02:00
from elasticsearch_dsl import analyzer, token_filter, tokenizer
2017-06-17 13:59:41 +02:00
2017-05-22 23:36:14 +02:00
from .models import Stage, AvisStage, AvisLieu
2017-06-17 13:59:41 +02:00
from .statics import PAYS_OPTIONS
PAYS_DICT = dict(PAYS_OPTIONS)
2017-05-22 23:36:14 +02:00
stage = Index('stages')
stage.settings(
number_of_shards=1,
number_of_replicas=0
)
2017-06-16 00:17:25 +02:00
text_analyzer = analyzer(
2017-06-17 13:59:41 +02:00
'default',
2017-06-16 00:17:25 +02:00
tokenizer="standard",
2017-06-17 13:59:41 +02:00
filter=['lowercase', 'standard', 'asciifolding',
token_filter("frstop", type="stop", stopwords="_french_"),
2017-06-16 00:17:25 +02:00
token_filter("frsnow", type="snowball", language="French")])
stage.analyzer(text_analyzer)
2017-05-22 23:36:14 +02:00
@stage.doc_type
class StageDocument(DocType):
lieux = fields.ObjectField(properties={
'nom': fields.StringField(),
2017-06-17 13:59:41 +02:00
'ville': fields.StringField(),
2017-05-22 23:36:14 +02:00
'pays': fields.StringField(),
})
2017-06-17 13:59:41 +02:00
auteur = fields.ObjectField(properties={
'nom': fields.StringField(),
})
2017-05-22 23:36:14 +02:00
thematiques = fields.StringField()
matieres = fields.StringField()
class Meta:
model = Stage
fields = [
'sujet',
'encadrants',
'type_stage',
'niveau_scol',
2017-06-17 13:59:41 +02:00
'structure',
'date_debut',
'date_fin'
2017-05-22 23:36:14 +02:00
]
def prepare_thematiques(self, instance):
2018-12-26 21:34:48 +01:00
return ", ".join(instance.thematiques.all().values_list("name", flat=True)).lower()
2017-05-22 23:36:14 +02:00
def prepare_matieres(self, instance):
2018-12-26 21:34:48 +01:00
return ", ".join(instance.matieres.all().values_list("nom", flat=True)).lower()
2017-05-22 23:36:14 +02:00
def prepare_niveau_scol(self, instance):
2018-12-26 21:34:48 +01:00
return instance.get_niveau_scol_display().lower()
2017-05-22 23:36:14 +02:00
def prepare_type_stage(self, instance):
2018-12-26 21:34:48 +01:00
return instance.type_stage_fancy.lower()
2017-06-17 13:59:41 +02:00
def prepare_date_fin(self, instance):
return instance.date_fin.year
def prepare_date_debut(self, instance):
return instance.date_debut.year
2018-12-26 21:34:48 +01:00
def prepare_sujet(self, instance):
return instance.sujet.lower()
2017-06-17 13:59:41 +02:00
# Hook pour l'affichage des noms de pays
def prepare(self, instance):
data = super(StageDocument, self).prepare(instance)
for lieu in data['lieux']:
2018-12-26 21:34:48 +01:00
lieu['pays'] = PAYS_DICT[lieu['pays']].lower()
2017-06-17 13:59:41 +02:00
return data