Skip to content

Fix deletion of touristic content objects in parsers when an m2m constant field is not a list #4838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ CHANGELOG
2.116.1+dev (XXXX-XX-XX)
----------------------------

**Bug fixes**

- Fix deletion of touristic content objects in parsers when an m2m constant field is not a list

**Improvements**

- Change provider Charfield for a foreign key
- Change provider Charfield to a foreign key


2.116.1 (2025-07-15)
Expand Down
7 changes: 6 additions & 1 deletion geotrek/tourism/parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import os
from collections.abc import Iterable
from mimetypes import guess_type
from urllib.parse import urlparse

Expand Down Expand Up @@ -53,7 +54,11 @@ def get_to_delete_kwargs(self):
assert not self.separator or self.separator not in val
field = self.model._meta.get_field(dst)
natural_key = self.natural_keys[dst]
filters = {natural_key: subval for subval in val}
if isinstance(val, Iterable) and not isinstance(val, str):
values = val
else:
values = [val]
filters = {natural_key: subval for subval in values}
if not filters:
continue
if dst in ("type1", "type2"):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"features": [
{
"id": 1
},
{
"id": 2
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"features": [
{
"id": 2
}
]
}
97 changes: 96 additions & 1 deletion geotrek/tourism/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
from geotrek.common.models import Attachment, FileType
from geotrek.common.tests.factories import RecordSourceFactory, TargetPortalFactory
from geotrek.common.tests.mixins import GeotrekParserTestMixin
from geotrek.tourism.models import InformationDesk, TouristicContent, TouristicEvent
from geotrek.common.tests.test_parsers import JSONParser
from geotrek.tourism.models import (
InformationDesk,
TouristicContent,
TouristicEvent,
)
from geotrek.tourism.parsers import (
EspritParcParser,
GeotrekInformationDeskParser,
Expand All @@ -25,6 +30,7 @@
LEITouristicEventParser,
OpenStreetMapTouristicContentParser,
TouristicContentApidaeParser,
TouristicContentMixin,
TouristicContentTourInSoftParser,
TouristicContentTourInSoftParserV3,
TouristicContentTourInSoftParserV3withMedias,
Expand All @@ -40,6 +46,29 @@
)


class TestTouristicContentMixinJSONParser(TouristicContentMixin, JSONParser):
model = TouristicContent
eid = "eid"
provider = "provider"
delete = True
fields = {"eid": "id"}
constant_fields = {"geom": Point(0, 0), "category": "foo"}
natural_keys = {"category": "label", "type1": "label"}
field_options = {"type1": {"fk": "category"}}


class TestTouristicContentMixinDeleteM2MListJSONParser(
TestTouristicContentMixinJSONParser
):
m2m_constant_fields = {"type1": ["A", "B"]}


class TestTouristicContentMixinDeleteM2MStringJSONParser(
TestTouristicContentMixinJSONParser
):
m2m_constant_fields = {"type1": "A"}


class ApidaeConstantFieldContentParser(TouristicContentApidaeParser):
category = "Constant Content"
type1 = ["Type1 1", "Type1 2"]
Expand Down Expand Up @@ -244,6 +273,72 @@ def mocked_json():
self.assertEqual(TouristicContent.objects.count(), 1)


class TouristicContentMixinTests(TestCase):
@classmethod
def setUpTestData(cls):
categ = TouristicContentCategoryFactory(label="foo")
TouristicContentType1Factory(label="A", category=categ)
TouristicContentType1Factory(label="B", category=categ)

def test_delete_existing_content_when_m2m_constant_field_is_a_list(self):
filename = os.path.join(
os.path.dirname(__file__),
"data",
"touristic_content_mixin",
"touristic_content_mixin_type1_is_constant_first_run.json",
)
call_command(
"import",
"geotrek.tourism.tests.test_parsers.TestTouristicContentMixinDeleteM2MListJSONParser",
filename,
verbosity=0,
)
self.assertEqual(len(TouristicContent.objects.all()), 2)
# FIXME: type1 values are not correctly assigned - this test passes even without the fix
filename = os.path.join(
os.path.dirname(__file__),
"data",
"touristic_content_mixin",
"touristic_content_mixin_type1_is_constant_second_run.json",
)
call_command(
"import",
"geotrek.tourism.tests.test_parsers.TestTouristicContentMixinDeleteM2MListJSONParser",
filename,
verbosity=0,
)
self.assertEqual(len(TouristicContent.objects.all()), 1)

def test_delete_existing_content_when_m2m_constant_field_is_a_string(self):
filename = os.path.join(
os.path.dirname(__file__),
"data",
"touristic_content_mixin",
"touristic_content_mixin_type1_is_constant_first_run.json",
)
call_command(
"import",
"geotrek.tourism.tests.test_parsers.TestTouristicContentMixinDeleteM2MStringJSONParser",
filename,
verbosity=0,
)
self.assertEqual(len(TouristicContent.objects.all()), 2)
# FIXME: type1 values are not correctly assigned - this test passes even without the fix
filename = os.path.join(
os.path.dirname(__file__),
"data",
"touristic_content_mixin",
"touristic_content_mixin_type1_is_constant_second_run.json",
)
call_command(
"import",
"geotrek.tourism.tests.test_parsers.TestTouristicContentMixinDeleteM2MStringJSONParser",
filename,
verbosity=0,
)
self.assertEqual(len(TouristicContent.objects.all()), 1)


class ParserTests(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down