Skip to content

Commit

Permalink
🔥 [CLEAN] Clean GeotrekSiteParser test file (refs #3569)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chatewgne committed Feb 6, 2024
1 parent 3bb1e3a commit 9133fda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 457 deletions.
19 changes: 17 additions & 2 deletions geotrek/outdoor/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,49 @@ def get_id_from_mapping(self, mapping, value):
return None

def init_outdoor_category(self, category, model, join_field=None, extra_fields={}):
# Get categories as JSON response
response = self.request_or_retry(f"{self.url}/api/v2/outdoor_{category}")
results = response.json().get('results', [])

# Init mapping variable for this category if it does not exist
if category not in self.field_options.keys():
self.field_options[category] = {}
if "mapping" not in self.field_options[category].keys():
self.field_options[category]["mapping"] = {}

# Iter over category JSON results
for result in results:

# Extract label in default language from JSON
label = result["name"]
if isinstance(label, dict):
if label[settings.MODELTRANSLATION_DEFAULT_LANGUAGE]:
replaced_label = self.replace_mapping(label[settings.MODELTRANSLATION_DEFAULT_LANGUAGE], f'outdoor_{category}')
else:
if label:
replaced_label = self.replace_mapping(label, f'outdoor_{category}')
replaced_label = self.replace_mapping(label, f'outdoor_{category}')

# Extract other category attributes in default language from JSON
fields = {}
for field in extra_fields:
if isinstance(result[field], dict):
if result[field][settings.MODELTRANSLATION_DEFAULT_LANGUAGE]:
fields[field] = result[field][settings.MODELTRANSLATION_DEFAULT_LANGUAGE]
else:
fields[field] = result[field]

# Extract field that will become a ForeignKey from JSON response, using mapping
if join_field:
mapping_key = self.replace_fields.get(join_field, join_field)
mapped_value = self.get_id_from_mapping(self.field_options[mapping_key]["mapping"], result[join_field])
if not mapped_value:
continue # Ignore some results if related category was not retrieved
continue # Ignore some results if related category was not retrieved
fields[f"{join_field}_id"] = mapped_value

# Create or update object given all the fields that we extracted above
category_obj, _ = model.objects.update_or_create(**{'name': replaced_label}, defaults=fields)

# Remember this object in mapping for next call
self.field_options[category]["mapping"][category_obj.pk] = result['id']

def __init__(self, *args, **kwargs):
Expand Down
Loading

0 comments on commit 9133fda

Please sign in to comment.