Skip to content

Commit

Permalink
Add warnings, addportal_filter, url on general parser
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Aug 4, 2022
1 parent 4d96073 commit 9ca79e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions geotrek/common/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ class GeotrekParser(AttachmentParserMixin, Parser):
m2m_replace_fields: Replace m2m fields which have not the same name in the api v2 compare to models (geom => geometry in api v2)
categories_keys_api_v2: Key in the route of the category (example: /api/v2/touristiccontent_category/) corresponding to the model field
eid_prefix: Prefix of your eid which allow to differentiate multiple GeotrekParser
portals_filter: Portals which will be use for filter in api v2 (default: No portal filter)
"""
model = None
next_url = ''
Expand All @@ -922,14 +923,16 @@ class GeotrekParser(AttachmentParserMixin, Parser):
}
bbox = None
eid_prefix = ''
portals_filter = None

def __init__(self, *args, **kwargs):
def __init__(self, url=None, portals_filter=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bbox = Polygon.from_bbox(settings.SPATIAL_EXTENT)
self.bbox.srid = settings.SRID
self.bbox.transform(4326) # WGS84
self.fields = dict((f.name, f.name) for f in self.model._meta.fields if
not isinstance(f, TranslationField) and not f.name == 'id')
self.portals_filter = portals_filter
self.url = url if url else self.url
self.fields = dict((f.name, f.name) for f in self.model._meta.fields if not isinstance(f, TranslationField) and not f.name == 'id')
self.m2m_fields = {
f.name: f.name
for f in self.model._meta.many_to_many
Expand Down Expand Up @@ -1001,8 +1004,10 @@ def filter_geom(self, src, val):

def next_row(self):
while self.next_url:
portals = self.portals_filter
params = {
'in_bbox': ','.join([str(coord) for coord in self.bbox.extent]),
'portals': ','.join(portals) if portals else ''
}
response = self.request_or_retry(self.next_url, params=params)
self.root = response.json()
Expand Down
11 changes: 8 additions & 3 deletions geotrek/trekking/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def end(self):
try:
params = {
'in_bbox': ','.join([str(coord) for coord in self.bbox.extent]),
'fields': ['children', 'id']
'fields': 'children,id'
}
response = self.request_or_retry(f"{self.next_url}", params=params)
results = response.json()['results']
Expand All @@ -136,8 +136,13 @@ def end(self):
return
order = 0
for child in value:
trek_child_instance = Trek.objects.get(eid=f"{self.eid_prefix}{child}")
OrderedTrekChild.objects.get_or_create(parent=trek_parent_instance[0], child=trek_child_instance,
try:
trek_child_instance = Trek.objects.get(eid=f"{self.eid_prefix}{child}")
except Trek.DoesNotExist:
self.add_warning(_(f"One trek has not be generated for {trek_parent_instance[0].pk}"))
continue
OrderedTrekChild.objects.get_or_create(parent=trek_parent_instance[0],
child=trek_child_instance,
order=order)
order += 1
except Exception as e:
Expand Down

0 comments on commit 9ca79e7

Please sign in to comment.