experiENS/avisstage/documents.py

84 lines
2.2 KiB
Python
Raw Permalink Normal View History

2021-06-28 21:44:47 +02:00
from django_elasticsearch_dsl import Document, Index, fields
2021-02-08 02:27:37 +01:00
from elasticsearch_dsl import analyzer, token_filter
2017-06-17 13:59:41 +02:00
2021-02-08 02:27:37 +01:00
from .models import Stage
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
2021-02-07 23:15:47 +01: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(
2021-02-07 23:15:47 +01:00
"default",
2017-06-16 00:17:25 +02:00
tokenizer="standard",
2021-02-07 23:15:47 +01:00
filter=[
"lowercase",
"asciifolding",
token_filter("frstop", type="stop", stopwords="_french_"),
token_filter("frsnow", type="snowball", language="French"),
],
)
2017-06-16 00:17:25 +02:00
stage.analyzer(text_analyzer)
2017-05-22 23:36:14 +02:00
2021-02-07 23:15:47 +01:00
2017-05-22 23:36:14 +02:00
@stage.doc_type
2021-06-28 21:44:47 +02:00
class StageDocument(Document):
2021-02-07 23:15:47 +01:00
lieux = fields.ObjectField(
properties={
"nom": fields.TextField(),
"ville": fields.TextField(),
"pays": fields.TextField(),
}
)
auteur = fields.ObjectField(
properties={
"nom": fields.TextField(),
}
)
2021-06-28 21:44:47 +02:00
thematiques = fields.TextField()
matieres = fields.TextField()
2021-06-29 00:11:18 +02:00
2021-06-28 21:44:47 +02:00
class Django:
2017-05-22 23:36:14 +02:00
model = Stage
fields = [
2021-02-07 23:15:47 +01:00
"sujet",
"encadrants",
"type_stage",
"niveau_scol",
"structure",
"date_debut",
"date_fin",
2017-05-22 23:36:14 +02:00
]
def prepare_thematiques(self, instance):
2021-02-07 23:15:47 +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()
2021-02-07 23:15:47 +01:00
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)
2021-02-07 23:15:47 +01:00
for lieu in data["lieux"]:
lieu["pays"] = PAYS_DICT[lieu["pays"]].lower()
2017-06-17 13:59:41 +02:00
return data