experiENS/avisstage/documents.py
2021-06-28 23:57:16 +02:00

83 lines
2.2 KiB
Python

from django_elasticsearch_dsl import Document, Index, fields
from elasticsearch_dsl import analyzer, token_filter, tokenizer
from .models import AvisLieu, AvisStage, Stage
from .statics import PAYS_OPTIONS
PAYS_DICT = dict(PAYS_OPTIONS)
stage = Index("stages")
stage.settings(number_of_shards=1, number_of_replicas=0)
text_analyzer = analyzer(
"default",
tokenizer="standard",
filter=[
"lowercase",
"asciifolding",
token_filter("frstop", type="stop", stopwords="_french_"),
token_filter("frsnow", type="snowball", language="French"),
],
)
stage.analyzer(text_analyzer)
@stage.doc_type
class StageDocument(Document):
lieux = fields.ObjectField(
properties={
"nom": fields.TextField(),
"ville": fields.TextField(),
"pays": fields.TextField(),
}
)
auteur = fields.ObjectField(
properties={
"nom": fields.TextField(),
}
)
thematiques = fields.TextField()
matieres = fields.TextField()
class Django:
model = Stage
fields = [
"sujet",
"encadrants",
"type_stage",
"niveau_scol",
"structure",
"date_debut",
"date_fin",
]
def prepare_thematiques(self, instance):
return ", ".join(
instance.thematiques.all().values_list("name", flat=True)
).lower()
def prepare_matieres(self, instance):
return ", ".join(instance.matieres.all().values_list("nom", flat=True)).lower()
def prepare_niveau_scol(self, instance):
return instance.get_niveau_scol_display().lower()
def prepare_type_stage(self, instance):
return instance.type_stage_fancy.lower()
def prepare_date_fin(self, instance):
return instance.date_fin.year
def prepare_date_debut(self, instance):
return instance.date_debut.year
def prepare_sujet(self, instance):
return instance.sujet.lower()
# Hook pour l'affichage des noms de pays
def prepare(self, instance):
data = super(StageDocument, self).prepare(instance)
for lieu in data["lieux"]:
lieu["pays"] = PAYS_DICT[lieu["pays"]].lower()
return data