diff --git a/geotrek/api/v2/serializers.py b/geotrek/api/v2/serializers.py index 435cc81b1b..4c3f9275f1 100644 --- a/geotrek/api/v2/serializers.py +++ b/geotrek/api/v2/serializers.py @@ -986,7 +986,7 @@ class Meta: class SensitiveAreaSerializer(DynamicFieldsMixin, TimeStampedSerializer): url = HyperlinkedIdentityField(view_name='apiv2:sensitivearea-detail') - name = serializers.SerializerMethodField() + name = serializers.CharField(source='area_name') elevation = serializers.SerializerMethodField() description = serializers.SerializerMethodField() period = serializers.SerializerMethodField() @@ -1001,7 +1001,7 @@ class SensitiveAreaSerializer(DynamicFieldsMixin, TimeStampedSerializer): attachments = AttachmentSerializer(many=True) def get_name(self, obj): - return get_translation_or_dict('name', self, obj.species) + return get_translation_or_dict('name', self, obj) def get_description(self, obj): return get_translation_or_dict('description', self, obj) diff --git a/geotrek/diving/templates/diving/dive_public_pdf_base.html b/geotrek/diving/templates/diving/dive_public_pdf_base.html index 519a8e1627..72a713f55d 100644 --- a/geotrek/diving/templates/diving/dive_public_pdf_base.html +++ b/geotrek/diving/templates/diving/dive_public_pdf_base.html @@ -157,7 +157,7 @@

{% trans "Environmental sensitive areas" %}

{% for area in object.published_sensitive_areas %}
{% if area.species.pictogram %}{% endif %} -

{{ area.species.name }}

+

{{ area.name }}

{% trans "Sensitivity period:" %} {{ area.pretty_period }}

{% if area.contact %}

{% trans "Contact:" %} {{ area.contact|safe }}

{% endif %}

{{ area.description|safe }}

diff --git a/geotrek/sensitivity/forms.py b/geotrek/sensitivity/forms.py index 97bc2bd1d9..8212f6a44a 100644 --- a/geotrek/sensitivity/forms.py +++ b/geotrek/sensitivity/forms.py @@ -20,11 +20,12 @@ class PolygonMapWidget(MapWidget): class SensitiveAreaForm(CommonForm): geomfields = ['geom'] + name = forms.CharField(max_length=250, label=_("Name"), required=False) species = forms.ModelChoiceField(queryset=Species.objects.filter(category=Species.SPECIES), label=pgettext("Singular", "Species")) class Meta: - fields = ['structure', 'species', 'published', 'description', 'contact', 'geom'] + fields = ['structure', 'species', 'name', 'published', 'description', 'contact', 'geom'] model = SensitiveArea widgets = {'geom': BubbleMapWidget()} @@ -60,7 +61,7 @@ def __init__(self, *args, **kwargs): if instance: species = instance.species kwargs['initial'] = { - 'name': species.name, + # 'name': species.name, 'elevation': species.radius, 'pictogram': species.pictogram, 'practices': species.practices.all(), @@ -79,7 +80,6 @@ def save(self, **kwargs): else: species = self.instance.species species.category = Species.REGULATORY - species.name = self.cleaned_data['name'] species.radius = self.cleaned_data['elevation'] species.pictogram = self.cleaned_data['pictogram'] species.url = self.cleaned_data['url'] diff --git a/geotrek/sensitivity/locale/de/LC_MESSAGES/django.po b/geotrek/sensitivity/locale/de/LC_MESSAGES/django.po index 7084a9a044..c6b4d58cd3 100644 --- a/geotrek/sensitivity/locale/de/LC_MESSAGES/django.po +++ b/geotrek/sensitivity/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-04-03 15:39+0000\n" +"POT-Creation-Date: 2024-12-03 16:28+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -112,6 +112,9 @@ msgstr "" msgid "Species" msgstr "" +msgid "Area name" +msgstr "" + msgid "Species or regulatory area" msgstr "" @@ -133,6 +136,9 @@ msgstr "" msgid "Sensitive areas" msgstr "" +msgid "Published name" +msgstr "" + msgid "Radius" msgstr "" diff --git a/geotrek/sensitivity/locale/en/LC_MESSAGES/django.po b/geotrek/sensitivity/locale/en/LC_MESSAGES/django.po index 7084a9a044..d47ed4eb69 100644 --- a/geotrek/sensitivity/locale/en/LC_MESSAGES/django.po +++ b/geotrek/sensitivity/locale/en/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-04-03 15:39+0000\n" +"POT-Creation-Date: 2024-12-03 16:28+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -112,9 +112,12 @@ msgstr "" msgid "Species" msgstr "" -msgid "Species or regulatory area" +msgid "Area name" msgstr "" +msgid "Species or regulatory area" +msgstr "Specie" + msgid "Published" msgstr "" @@ -133,6 +136,9 @@ msgstr "" msgid "Sensitive areas" msgstr "" +msgid "Published name" +msgstr "" + msgid "Radius" msgstr "" diff --git a/geotrek/sensitivity/locale/es/LC_MESSAGES/django.po b/geotrek/sensitivity/locale/es/LC_MESSAGES/django.po index 7084a9a044..c6b4d58cd3 100644 --- a/geotrek/sensitivity/locale/es/LC_MESSAGES/django.po +++ b/geotrek/sensitivity/locale/es/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-04-03 15:39+0000\n" +"POT-Creation-Date: 2024-12-03 16:28+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -112,6 +112,9 @@ msgstr "" msgid "Species" msgstr "" +msgid "Area name" +msgstr "" + msgid "Species or regulatory area" msgstr "" @@ -133,6 +136,9 @@ msgstr "" msgid "Sensitive areas" msgstr "" +msgid "Published name" +msgstr "" + msgid "Radius" msgstr "" diff --git a/geotrek/sensitivity/locale/fr/LC_MESSAGES/django.po b/geotrek/sensitivity/locale/fr/LC_MESSAGES/django.po index 05cde54965..689bc410fe 100644 --- a/geotrek/sensitivity/locale/fr/LC_MESSAGES/django.po +++ b/geotrek/sensitivity/locale/fr/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-04-03 15:39+0000\n" +"POT-Creation-Date: 2024-12-03 16:28+0100\n" "PO-Revision-Date: 2020-04-22 07:48+0000\n" "Last-Translator: Emmanuelle Helly \n" "Language-Team: French \n" "Language-Team: LANGUAGE \n" @@ -112,6 +112,9 @@ msgstr "" msgid "Species" msgstr "" +msgid "Area name" +msgstr "" + msgid "Species or regulatory area" msgstr "" @@ -133,6 +136,9 @@ msgstr "" msgid "Sensitive areas" msgstr "" +msgid "Published name" +msgstr "" + msgid "Radius" msgstr "" diff --git a/geotrek/sensitivity/locale/nl/LC_MESSAGES/django.po b/geotrek/sensitivity/locale/nl/LC_MESSAGES/django.po index 7084a9a044..c6b4d58cd3 100644 --- a/geotrek/sensitivity/locale/nl/LC_MESSAGES/django.po +++ b/geotrek/sensitivity/locale/nl/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-04-03 15:39+0000\n" +"POT-Creation-Date: 2024-12-03 16:28+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -112,6 +112,9 @@ msgstr "" msgid "Species" msgstr "" +msgid "Area name" +msgstr "" + msgid "Species or regulatory area" msgstr "" @@ -133,6 +136,9 @@ msgstr "" msgid "Sensitive areas" msgstr "" +msgid "Published name" +msgstr "" + msgid "Radius" msgstr "" diff --git a/geotrek/sensitivity/migrations/0029_sensitivearea_name.py b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py new file mode 100644 index 0000000000..da060f3c14 --- /dev/null +++ b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py @@ -0,0 +1,40 @@ +from django.db import migrations, models +from django.conf import settings +from geotrek.sensitivity.models import SensitiveArea, Species + + +def generate_name(apps, schema_editor): + """Populate SensitiveAreas name from Species""" + + sensitive_area = SensitiveArea + languages = settings.MODELTRANSLATION_LANGUAGES + update_fields = [ + "name", + ] + update_fields += [f"name_{lang}" for lang in languages] + for row in sensitive_area.objects.existing().filter(species__category=2): + for field in update_fields: + setattr(row, field, getattr(row.species, field)) + row.save(update_fields=update_fields) + for field in update_fields: + print(f"species.manager {row.species._meta.managers}") + print(f"row {row.species} | field {field}") + setattr(row.species, field, "") + row.species.save(update_fields=update_fields) + + +class Migration(migrations.Migration): + + dependencies = [ + ("sensitivity", "0028_alter_sensitivearea_structure"), + ] + + operations = [ + migrations.AddField( + model_name="sensitivearea", + name="name", + field=models.CharField(default="", max_length=250, verbose_name="Name"), + ), + # migrations.RunPython(update_translation_fields_func, reverse_code=migrations.RunPython.noop), + migrations.RunPython(generate_name, reverse_code=migrations.RunPython.noop), + ] diff --git a/geotrek/sensitivity/migrations/0030_sensitivearea_name_populate.py.off b/geotrek/sensitivity/migrations/0030_sensitivearea_name_populate.py.off new file mode 100644 index 0000000000..16ef72aa63 --- /dev/null +++ b/geotrek/sensitivity/migrations/0030_sensitivearea_name_populate.py.off @@ -0,0 +1,35 @@ +from django.db import migrations, models +from modeltranslation.translator import translator +from django.conf import settings +from geotrek.sensitivity.models import SensitiveArea, Species + +def generate_name(apps, schema_editor): + """Populate SensitiveAreas name from Species""" + + sensitive_area = SensitiveArea + species = Species + languages = settings.MODELTRANSLATION_LANGUAGES + update_fields = [ + "name", + ] + update_fields += [f"name_{lang}" for lang in languages] + for row in sensitive_area.objects.filter(deleted=False, species__category=2): + sp = species.objects.filter(pk=row.species.pk) + values_dict = sp.values(*update_fields)[0] + print(values_dict) + # print({field: getattr(sp.first(), field) or "" for field in update_fields}) + sensitive_area.objects.filter(pk=row.pk).update(**values_dict) + sp.update(**{field:"" for field in update_fields}) + # row.save(update_fields=update_fields) + # row.species.save(update_fields=update_fields) + + +class Migration(migrations.Migration): + + dependencies = [ + ("sensitivity", "0029_sensitivearea_name"), + ] + + operations = [ + migrations.RunPython(generate_name, reverse_code=migrations.RunPython.noop), + ] diff --git a/geotrek/sensitivity/models.py b/geotrek/sensitivity/models.py index 9b8ad7e0ac..a5b6713fdc 100644 --- a/geotrek/sensitivity/models.py +++ b/geotrek/sensitivity/models.py @@ -92,6 +92,7 @@ def pretty_practices(self): class SensitiveArea(GeotrekMapEntityMixin, StructureRelated, TimeStampedModelMixin, NoDeleteMixin, AddPropertyMixin): + name = models.CharField(max_length=250, verbose_name=_("Name"), default='undefined') geom = models.GeometryField(srid=settings.SRID) geom_buffered = models.GeometryField(srid=settings.SRID, editable=False) species = models.ForeignKey(Species, verbose_name=_("Species or regulatory area"), on_delete=models.PROTECT) @@ -120,7 +121,7 @@ class Meta: ) def __str__(self): - return self.species.name + return self.name @property def radius(self): @@ -185,15 +186,27 @@ def published_langs(self): return [] @property - def species_display(self): - s = '%s' % (self.pk, - self.get_detail_url(), - self.species.name, - self.species.name) + def area_name(self): + if self.species.category == 1: + return self.species.name if self.name == "" else self.name + else: + return self.name + + @classproperty + def area_name_verbose_name(cls): + return _("Published name") + + @property + def name_display(self): + s = f'{self.area_name}' if self.published: - s = ' ' % _("Published") + s + s = f""" {s}""" return s + @property + def species_display(self): + return self.species.name + @property def extent(self): return self.geom.transform(settings.API_SRID, clone=True).extent if self.geom else None @@ -211,7 +224,7 @@ def kml(self): geometry += (coords, ) geom = GEOSGeometry(Polygon(geometry), srid=settings.SRID) geom = geom.transform(4326, clone=True) # KML uses WGS84 - line = kml.newpolygon(name=self.species.name, + line = kml.newpolygon(name=self.name, description=plain_text(self.description), altitudemode=simplekml.AltitudeMode.relativetoground, outerboundaryis=simplify_coords(geom.coords[0])) diff --git a/geotrek/sensitivity/serializers.py b/geotrek/sensitivity/serializers.py index 21925296d5..eb189f8d68 100644 --- a/geotrek/sensitivity/serializers.py +++ b/geotrek/sensitivity/serializers.py @@ -8,7 +8,8 @@ class SensitiveAreaSerializer(DynamicFieldsMixin, rest_serializers.ModelSerializer): category = rest_serializers.CharField(source='category_display') structure = rest_serializers.SlugRelatedField('name', read_only=True) - species = rest_serializers.CharField(source='species_display') + species = rest_serializers.CharField(source='species.name') + name = rest_serializers.CharField(source='name_display') class Meta: model = sensitivity_models.SensitiveArea diff --git a/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html b/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html index bbaaa39ab0..5a8a1f623d 100644 --- a/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html +++ b/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html @@ -16,17 +16,16 @@

{% trans "Attributes" %}

{{ sensitivearea|verbose:"structure" }} {{ sensitivearea.structure }} - {% if sensitivearea.species.category == 2 %} + {% if sensitivearea.species.category == 1 %} - {{ sensitivearea.species|verbose:"name" }} - {{ sensitivearea.species.name }} - - {% else %} - - {{ sensitivearea|verbose:"species" }} - {{ sensitivearea.species }} + {{ sensitivearea|verbose:"area_name" }} + {{ sensitivearea.area_name }} {% endif %} + + {{ sensitivearea|verbose:"name" }} + {{ sensitivearea.name }} + {% trans "Period" %} {{ sensitivearea.species.pretty_period }} diff --git a/geotrek/sensitivity/tests/factories.py b/geotrek/sensitivity/tests/factories.py index 542f83b540..7de9022979 100644 --- a/geotrek/sensitivity/tests/factories.py +++ b/geotrek/sensitivity/tests/factories.py @@ -59,6 +59,7 @@ class SensitiveAreaFactory(StructureRelatedDefaultFactory): class Meta: model = models.SensitiveArea + name = "Sensitive area" species = factory.SubFactory(SpeciesFactory) geom = 'POLYGON((700000 6600000, 700000 6600003, 700003 6600003, 700003 6600000, 700000 6600000))' published = True diff --git a/geotrek/sensitivity/tests/test_views.py b/geotrek/sensitivity/tests/test_views.py index bc67bfdd7f..4338f3fe34 100644 --- a/geotrek/sensitivity/tests/test_views.py +++ b/geotrek/sensitivity/tests/test_views.py @@ -70,7 +70,7 @@ def setUp(self): def test_species_name_shown_in_detail_page(self): url = reverse("sensitivity:sensitivearea_detail", kwargs={"pk": self.area.pk}) response = self.client.get(url) - self.assertContains(response, self.area.species.name) + self.assertContains(response, self.area.name) class SensitiveAreaOpenAirViewsTest(TestCase): diff --git a/geotrek/sensitivity/translation.py b/geotrek/sensitivity/translation.py index 5ede7ce578..cf4947ccde 100644 --- a/geotrek/sensitivity/translation.py +++ b/geotrek/sensitivity/translation.py @@ -16,7 +16,7 @@ class SpeciesTO(TranslationOptions): class SensitiveAreaTO(TranslationOptions): - fields = ('description', ) + fields = ('name', 'description', ) translator.register(Rule, RuleTO) diff --git a/geotrek/sensitivity/views.py b/geotrek/sensitivity/views.py index 959290bd75..b9e8050bb7 100644 --- a/geotrek/sensitivity/views.py +++ b/geotrek/sensitivity/views.py @@ -27,8 +27,8 @@ class SensitiveAreaList(CustomColumnsMixin, MapEntityList): queryset = SensitiveArea.objects.existing() filterform = SensitiveAreaFilterSet - mandatory_columns = ['id', 'species'] - default_extra_columns = ['category'] + mandatory_columns = ['id', 'name'] + default_extra_columns = ['species','category'] class SensitiveAreaFormatList(MapEntityFormat, SensitiveAreaList): diff --git a/geotrek/trekking/templates/trekking/trek_public_pdf_base.html b/geotrek/trekking/templates/trekking/trek_public_pdf_base.html index 4bc3c1e9bc..b806f4ffc1 100644 --- a/geotrek/trekking/templates/trekking/trek_public_pdf_base.html +++ b/geotrek/trekking/templates/trekking/trek_public_pdf_base.html @@ -380,7 +380,7 @@

{% trans "Environmental sensitive areas" %}

{% for area in object.published_sensitive_areas %}
{% if area.species.pictogram %}{% endif %} -

{{ area.species.name }}

+

{{ area.name }}

{% trans "Sensitivity period:" %} {{ area.pretty_period }}

{% if area.contact %}

{% trans "Contact:" %} {{ area.contact|safe }}

{% endif %}

{{ area.description|safe }}