Skip to content

Commit

Permalink
✅ [TEST] Add tests for CirkwiParser (refs #3947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Mar 4, 2024
1 parent f7979b8 commit 6add793
Show file tree
Hide file tree
Showing 6 changed files with 483 additions and 27 deletions.
48 changes: 22 additions & 26 deletions geotrek/cirkwi/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class CirkwiParser(AttachmentParserMixin, XmlParser):
eid = 'eid'
default_language = settings.MODELTRANSLATION_DEFAULT_LANGUAGE
field_options = {
"geom": {"required": True},
"name": {"required": True},
Expand All @@ -19,18 +20,19 @@ class CirkwiParser(AttachmentParserMixin, XmlParser):
}

def get_part(self, dst, src, val):
if val is None:
return None
# Recursively extract XML attributes
if '@@' in src and src[:2] != '@@':
part, attrib = src.split('@@', 1)
return self.get_part(dst, f"@@{attrib}", val.find(part))
# Extract XML attributes
elif src[:2] == '@@':
elif src.startswith('@@'):
return val.attrib[src[2:]]
else:
# Replace language attribute
if "'<LANG>'" in src:
src = src.replace("<LANG>", self.default_language)
# Return a list of XML elements
if src[-2:] == '/*':
if src.endswith('/*'):
return val.findall(src[:-2])
# Return inner text if XML element exists
if val.find(src) is None:
Expand All @@ -57,24 +59,22 @@ def filter_attachments(self, src, val):
return attachments


class CirkwiTrekParserFr(CirkwiParser):
default_language = 'fr'
class CirkwiTrekParser(CirkwiParser):
model = Trek
url = "https://demo-admin.geotrek.fr/static/circkwi.xml"
results_path = 'circuit'
fields = {
"eid": "@@id_circuit",
"name": f"informations/information[@langue='{default_language}']/titre",
"description": (f"informations/information[@langue='{default_language}']/description",
f"informations/information[@langue='{default_language}']/informations_complementaires/information_complementaire/titre",
f"informations/information[@langue='{default_language}']/informations_complementaires/information_complementaire/description"),
"name": "informations/information[@langue='<LANG>']/titre",
"description": ("informations/information[@langue='<LANG>']/description",
"informations/information[@langue='<LANG>']/informations_complementaires/information_complementaire/titre",
"informations/information[@langue='<LANG>']/informations_complementaires/information_complementaire/description"),
"geom": "sens_circuit/fichier_trace@@url",
}
constant_fields = {
'practice': "Pédestre"
}
non_fields = {
'attachments': f"informations/information[@langue='{default_language}']/medias/images/image/*"
'attachments': "informations/information[@langue='<LANG>']/medias/images/image/*"
}
natural_keys = {
'practice': 'name'
Expand All @@ -85,26 +85,25 @@ def filter_geom(self, src, val):
return ApidaeTrekParser._get_geom_from_gpx(response.content)


class CirkwiTouristicContentParserFr(CirkwiParser):
default_language = 'fr'
class CirkwiTouristicContentParser(CirkwiParser):
model = TouristicContent
url = "https://demo-admin.geotrek.fr/static/poicirkwi.xml"
default_language = settings.MODELTRANSLATION_DEFAULT_LANGUAGE
results_path = 'poi'
fields = {
"eid": "@@id_poi",
"name": f"informations/information[@langue='{default_language}']/titre",
"description": (f"informations/information[@langue='{default_language}']/description",
f"informations/information[@langue='{default_language}']/informations_complementaires/information_complementaire/titre",
f"informations/information[@langue='{default_language}']/informations_complementaires/information_complementaire/description"),
"name": "informations/information[@langue='<LANG>']/titre",
"description": ("informations/information[@langue='<LANG>']/description",
"informations/information[@langue='<LANG>']/informations_complementaires/information_complementaire/titre",
"informations/information[@langue='<LANG>']/informations_complementaires/information_complementaire/description"),
"geom": ("adresse/position/lng", "adresse/position/lat"),
"practical_info": ("adresse/num", "adresse/rue", "adresse/cp", "adresse/ville", f"informations/information[@langue='{default_language}']/informations_complementaires/information_complementaire/*"),
"practical_info": ("adresse/num", "adresse/rue", "adresse/cp", "adresse/ville", "informations/information[@langue='<LANG>']/informations_complementaires/information_complementaire/*"),
"category": "categories/categorie/*",
}
m2m_fields = {
"type1": "categories/categorie/*",
}
non_fields = {
'attachments': f"informations/information[@langue='{default_language}']/medias/images/image/*"
'attachments': "informations/information[@langue='<LANG>']/medias/images/image/*"
}
field_options = {
"geom": {"required": True},
Expand All @@ -129,21 +128,17 @@ def filter_practical_info(self, src, val):
for other_info in other_infos:
infos += f"<br><strong>{other_info.find('titre').text} : </strong><br>"
infos += f"{other_info.find('description').text}<br>"
if not infos:
return None
return infos

def filter_category(self, src, val):
if val is None:
return None
name = val[0].attrib["nom"]
return self.apply_filter('category', src, name)

def filter_type1(self, src, val):
if val is None or len(val) < 2:
return None
label = val[1].attrib["nom"]
if self.field_options["type1"]["create"]:
if self.field_options.get("type1", {}).get("create", False):
type1, __ = TouristicContentType1.objects.get_or_create(category=self.obj.category, label=label)
else:
try:
Expand All @@ -152,6 +147,7 @@ def filter_type1(self, src, val):
self.add_warning(
_("Type 1 '{type}' does not exist for category '{cat}'. Please add it").format(
type=label, cat=self.obj.category.label))
return []
return [type1]

def filter_geom(self, src, val):
Expand Down
204 changes: 204 additions & 0 deletions geotrek/cirkwi/tests/data/circuits.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<circuits version="2">
<circuit date_creation="1295825772" date_modification="1295828456" id_circuit="10925"
id_utilisateur="1">
<informations>
<information langue="fr">
<titre>Le patrimoine de Plancoët</titre>
<description>Laissez-vous guider par ce chemin</description>
<medias>
<images>
<image nature="13">
<legende>Le patrimoine de Plancoët</legende>
<url>https://example.net/a_picture.jpg</url>
<credit>Manon</credit>
</image>
<image nature="3">
<url>https://example.net/a_picture.jpg</url>
</image>
</images>
<videos>
<video>
<code>
<iframe width='500' height='360'
src='https://www.youtube.com/embed/bI2z9VGhd0A' />
</code>
<credit>Valentin</credit>
</video>
</videos>
<sons>
<son>
<url>https://www.cirkwi.com/mp3/6302_fr.mp3</url>
<credit>Valentin</credit>
</son>
</sons>
</medias>
<informations_complementaires>
<information_complementaire>
<titre>Horaires</titre>
<description>Tous les jours sauf le Dimanche</description>
</information_complementaire>
</informations_complementaires>
<tags_publics>
<tag_public id="31" nom="style_randonnee" />
<tag_public id="36" nom="public_famille" />
</tags_publics>
<pdfs_perso>
<pdf_perso>https://www.mon-site.com/lien_vers_mon_pdf.pdf</pdf_perso>
</pdfs_perso>
</information>
<information langue="en">
<titre>Title en</titre>
<description>Description en</description>
<medias>
<images>
<image nature="12">
<legende>Le patrimoine de Plancoët</legende>
<url>https://example.net/a_picture.jpg</url>
<credit>Manon</credit>
</image>
</images>
</medias>
</information>
</informations>
<sens_circuit id="1" label="aller_simple">
<adresse>
<num>74</num>
<rue>chemin du sars barras</rue>
<ville>Plancoët</ville>
<cp>22130</cp>
<pays code_pays="FR">France</pays>
<position>
<lat>48.5223295900644</lat>
<lng>-2.23556272792814</lng>
</position>
</adresse>
<distance>3400</distance>
<denivele_positif>354</denivele_positif>
<denivele_negatif>-354</denivele_negatif>
<tags_prives>
<tag_prive id="31" nom="bretagne_randonnee" />
<tag_prive id="39" nom="bretagne_tourisme" />
</tags_prives>
<ddrs>
<ddr id_ddr="45" />
<ddr id_ddr="53" />
</ddrs>
<locomotions>
<locomotion type="voiture" id_locomotion="2" difficulte="1" duree="45" jours="0" />
<locomotion type="vtt" id_locomotion="3" difficulte="3" duree="129" />
</locomotions>
<fichier_trace
url="https://demo-admin.geotrek.fr/static/boucle-du-pic-des-trois-seigneurs.gpx"
info_parcours="false" />
<infos_parcours>
<info_parcours numero="1" date_modification="1365413342" id_info_parcours="357">
<categories>
<categorie nom="Intersection" id_categorie="15" />
<categorie nom="Lieu dangereux" id_categorie="11" />
</categories>
<informations>
<information langue="fr">
<titre>Tour de lancienne église Sainte-Gertrude</titre>
<description>
A Tenneville, ce site reposant vous fera découvrir une église
classée dont il ne reste qu’une partie du bâtiment originel. Nous
vous laissons le plaisir de...\r\nClassé le 1985\r\n
</description>
<medias>
<images>
<image nature="12">
<legende>Le patrimoine de Plancoët</legende>
<url>https://example.net/a_picture.jpg</url>
</image>
</images>
</medias>
</information>
</informations>
<adresse>
<pays code_pays="BE">Belgique</pays>
<position>
<lat>48.7125946</lat>
<lng>6.1442926</lng>
</position>
<altitude>468.0</altitude>
</adresse>
<dates>
<date recursivite_annuelle="true">
<date_debut>1365123600</date_debut>
<date_fin>1365130800</date_fin>
</date>
<date recursivite_annuelle="false">
<date_debut>1365123601</date_debut>
<date_fin>1365130802</date_fin>
</date>
</dates>
</info_parcours>
</infos_parcours>
<pois>
<poi numero="1" date_modification="1365413311" id_poi="35237">
<categories>
<categorie nom="Monuments et Architecture" id_categorie="15" />
<categorie nom="Eglise" id_categorie="11" />
</categories>
<informations>
<information langue="fr">
<titre>Tour de lancienne église Sainte-Gertrude</titre>
<description>
A Tenneville, ce site reposant vous fera découvrir une église
classée dont il ne reste qu’une partie du bâtiment originel. Nous
vous laissons le plaisir de...\r\nClassé le 1985\r\n
</description>
<medias>
<images>
<image>
<url>https://example.net/a_picture.jpg</url>
</image>
</images>
</medias>
</information>
</informations>
<adresse>
<num>1</num>
<rue>route de Bastogne</rue>
<ville>Tenneville</ville>
<cp>6970</cp>
<pays code_pays="BE">Belgique</pays>
<position>
<lat>48.7125946</lat>
<lng>6.1442926</lng>
</position>
<altitude>468.0</altitude>
</adresse>
<dates>
<date recursivite_annuelle="true">
<date_debut>1365123600</date_debut>
<date_fin>1365130800</date_fin>
</date>
<date recursivite_annuelle="false">
<date_debut>1365123601</date_debut>
<date_fin>1365130802</date_fin>
</date>
</dates>
<qrcodelink>
https://ws.cirkwi.com/link/jek3dd35kn
</qrcodelink>
<diffusion cirkwi="1" mobile="1" reseau="1" partage="2" />
</poi>
</pois>
<dates>
<date recursivite_annuelle="true">
<date_debut>1365123600</date_debut>
<date_fin>1365130800</date_fin>
</date>
<date recursivite_annuelle="false">
<date_debut>1365123601</date_debut>
<date_fin>1365130802</date_fin>
</date>
</dates>
<qrcodelink>
https://ws.cirkwi.com/link/g54rzr72zve
</qrcodelink>
<diffusion cirkwi="1" mobile="1" reseau="1" />
</sens_circuit>
</circuit>
</circuits>
Loading

0 comments on commit 6add793

Please sign in to comment.