From 733e5840eaef6eebcf8b7a7acaf0256c56a283d6 Mon Sep 17 00:00:00 2001 From: lpofredc Date: Tue, 14 Feb 2023 17:02:31 +0100 Subject: [PATCH 1/3] WIP: name to sensitivearea (#3483) --- geotrek/api/v2/serializers.py | 2 +- .../diving/dive_public_pdf_base.html | 2 +- geotrek/sensitivity/forms.py | 6 ++-- .../migrations/0027_sensitivearea_name.py | 33 +++++++++++++++++++ geotrek/sensitivity/models.py | 5 +-- geotrek/sensitivity/serializers.py | 2 +- .../sensitivearea_detail_attributes.html | 13 ++++---- geotrek/sensitivity/tests/factories.py | 1 + geotrek/sensitivity/tests/test_functional.py | 3 +- geotrek/sensitivity/tests/test_views.py | 14 ++++---- geotrek/sensitivity/translation.py | 2 +- .../trekking/trek_public_pdf_base.html | 2 +- 12 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 geotrek/sensitivity/migrations/0027_sensitivearea_name.py diff --git a/geotrek/api/v2/serializers.py b/geotrek/api/v2/serializers.py index 0ac5f62712..addd484683 100644 --- a/geotrek/api/v2/serializers.py +++ b/geotrek/api/v2/serializers.py @@ -916,7 +916,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 86abaa0c7e..66bb523629 100644 --- a/geotrek/sensitivity/forms.py +++ b/geotrek/sensitivity/forms.py @@ -20,7 +20,7 @@ class SensitiveAreaForm(CommonForm): 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()} @@ -55,7 +55,7 @@ def __init__(self, *args, **kwargs): if kwargs['instance']: species = kwargs['instance'].species kwargs['initial'] = { - 'name': species.name, + # 'name': species.name, 'elevation': species.radius, 'pictogram': species.pictogram, 'practices': species.practices.all(), @@ -73,7 +73,7 @@ def save(self): else: species = self.instance.species species.category = Species.REGULATORY - species.name = self.cleaned_data['name'] + # 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/migrations/0027_sensitivearea_name.py b/geotrek/sensitivity/migrations/0027_sensitivearea_name.py new file mode 100644 index 0000000000..9c7b8fc61f --- /dev/null +++ b/geotrek/sensitivity/migrations/0027_sensitivearea_name.py @@ -0,0 +1,33 @@ +# Generated by Django 3.2.16 on 2023-02-09 09:27 + +from django.db import migrations, models + + +def gen_name(apps, schema_editor): + """Populate SensitiveAreas name from Species + """ + sensitive_area = apps.get_model("sensitivity", 'SensitiveArea') + for row in sensitive_area.objects.all(): + row.name = row.species.name + row.save(update_fields=['name']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('sensitivity', '0026_alter_sensitivearea_geom_buffered'), + ] + + operations = [ + migrations.AddField( + model_name='sensitivearea', + name='name', + field=models.CharField(default=None, null=True, max_length=250, verbose_name='Name'), + ), + migrations.RunPython(gen_name, reverse_code=migrations.RunPython.noop), + migrations.AlterField( + model_name='sensitivearea', + name='name', + field=models.CharField(default='undefined', max_length=250, verbose_name='Name'), + ), + ] diff --git a/geotrek/sensitivity/models.py b/geotrek/sensitivity/models.py index c74ffa1120..02dbdee72e 100644 --- a/geotrek/sensitivity/models.py +++ b/geotrek/sensitivity/models.py @@ -76,6 +76,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) @@ -100,7 +101,7 @@ class Meta: ) def __str__(self): - return self.species.name + return self.name @property def radius(self): @@ -191,7 +192,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 697aa1bfcf..211c405f45 100644 --- a/geotrek/sensitivity/serializers.py +++ b/geotrek/sensitivity/serializers.py @@ -57,7 +57,7 @@ def get_kml_url(self, obj): class Meta: model = sensitivity_models.SensitiveArea - fields = ('id', 'species', 'description', 'contact', 'published', 'publication_date', 'kml_url', 'attachments') + fields = ('id', 'species', 'name', 'description', 'contact', 'published', 'publication_date', 'kml_url', 'attachments') class SensitiveAreaAPIGeojsonSerializer(GeoFeatureModelSerializer, SensitiveAreaAPISerializer): diff --git a/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html b/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html index b27c853ce3..951bf3b43f 100644 --- a/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html +++ b/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html @@ -10,17 +10,16 @@

{% trans "Attributes" %}

{{ sensitivearea|verbose:"structure" }} {{ sensitivearea.structure }} - {% if sensitivearea.species.category == 2 %} - - {{ sensitivearea.species|verbose:"name" }} - {{ sensitivearea.species.name }} - - {% else %} + {% if sensitivearea.species.category == 1 %} {{ sensitivearea|verbose:"species" }} - {{ sensitivearea.species }} + {{ sensitivearea.name }} {% endif %} + + {{ sensitivearea.species|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 dbe4f296ba..d1ce94852b 100644 --- a/geotrek/sensitivity/tests/factories.py +++ b/geotrek/sensitivity/tests/factories.py @@ -48,6 +48,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_functional.py b/geotrek/sensitivity/tests/test_functional.py index e2021c61da..75968301df 100644 --- a/geotrek/sensitivity/tests/test_functional.py +++ b/geotrek/sensitivity/tests/test_functional.py @@ -30,6 +30,7 @@ def get_expected_geojson_attrs(self): def get_expected_json_attrs(self): return { + 'name': 'Sensitive area', 'attachments': [], 'contact': 'toto@tata.com', 'description': 'Blabla', @@ -110,6 +111,7 @@ def get_expected_geojson_attrs(self): def get_expected_json_attrs(self): return { + 'name': 'Sensitive area', 'attachments': [], 'contact': 'toto@tata.com', 'description': 'Blabla', @@ -173,7 +175,6 @@ def test_crud_status(self): response = self.client.get(obj.get_update_url()) self.assertEqual(response.status_code, 200) - self._post_update_form(obj) response = self.client.get(obj.get_delete_url()) diff --git a/geotrek/sensitivity/tests/test_views.py b/geotrek/sensitivity/tests/test_views.py index 0ebfd8fdfe..cb69b248a1 100644 --- a/geotrek/sensitivity/tests/test_views.py +++ b/geotrek/sensitivity/tests/test_views.py @@ -69,10 +69,10 @@ def login(self): def tearDown(self): self.client.logout() - def test_species_name_shown_in_detail_page(self): + def test_area_name_shown_in_detail_page(self): url = "/sensitivearea/{pk}/".format(pk=self.area.pk) response = self.client.get(url) - self.assertContains(response, self.area.species.name) + self.assertContains(response, self.area.name) @freeze_time("2020-01-01") @@ -94,7 +94,7 @@ def setUp(self): 'kml_url': 'http://testserver/api/en/sensitiveareas/{pk}.kml'.format(pk=self.pk), 'info_url': self.species.url, 'species_id': self.species.id, - "name": self.species.name, + "name": "Sensitive area", "period": [False, False, False, False, False, True, True, False, False, False, False, False], 'practices': [p.pk for p in self.species.practices.all()], 'provider': '', @@ -211,7 +211,7 @@ def test_list_bubble_sensitivearea_with_point(self): response = self.client.get(url) self.assertEqual(response.json()['count'], 1) self.assertEqual(response.json()['results'][0]['radius'], 5) - self.assertEqual(response.json()['results'][0]['name'], sensitive_area_point.species.name) + self.assertEqual(response.json()['results'][0]['name'], sensitive_area_point.name) def test_list_sportpractice(self): url = '/api/v2/sensitivearea_practice/?format=json&language=en' @@ -231,7 +231,7 @@ def test_filters_structure(self): url = '/api/v2/sensitivearea/?format=json&language=en&period=ignore&structures={}'.format(other_structure.pk) response = self.client.get(url) self.assertEqual(response.json()['count'], 1) - self.assertEqual(response.json()['results'][0]['name'], self.sensitivearea_other_structure.species.name) + self.assertEqual(response.json()['results'][0]['name'], self.sensitivearea_other_structure.name) def test_filters_no_period(self): StructureFactory.create() @@ -252,7 +252,7 @@ def test_filters_specific_period(self): url = '/api/v2/sensitivearea/?format=json&language=en&period=2,3' response = self.client.get(url) self.assertEqual(response.json()['count'], 1) - self.assertEqual(response.json()['results'][0]['name'], sensitive_area_jf.species.name) + self.assertEqual(response.json()['results'][0]['name'], sensitive_area_jf.name) def test_filters_no_period_get_month(self): sensitive_area_month = SensitiveAreaFactory.create(**{'species__period01': True}) @@ -260,4 +260,4 @@ def test_filters_no_period_get_month(self): url = '/api/v2/sensitivearea/?format=json&language=en' response = self.client.get(url) self.assertEqual(response.json()['count'], 1) - self.assertEqual(response.json()['results'][0]['name'], sensitive_area_month.species.name) + self.assertEqual(response.json()['results'][0]['name'], sensitive_area_month.name) diff --git a/geotrek/sensitivity/translation.py b/geotrek/sensitivity/translation.py index 1b67667e2d..3d67c6522b 100644 --- a/geotrek/sensitivity/translation.py +++ b/geotrek/sensitivity/translation.py @@ -13,7 +13,7 @@ class SpeciesTO(TranslationOptions): class SensitiveAreaTO(TranslationOptions): - fields = ('description', ) + fields = ('name', 'description', ) translator.register(SportPractice, SportPracticeTO) diff --git a/geotrek/trekking/templates/trekking/trek_public_pdf_base.html b/geotrek/trekking/templates/trekking/trek_public_pdf_base.html index 9df2b05bed..30f1c806f9 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 }}

From 022f2d20060721ab83fae29fa5ef89d38232d7f2 Mon Sep 17 00:00:00 2001 From: lpofredc Date: Thu, 5 Dec 2024 14:11:58 +0100 Subject: [PATCH 2/3] WIP manage sensitive area name in sensitivearea model --- geotrek/api/v2/serializers.py | 2 +- geotrek/sensitivity/forms.py | 2 +- .../locale/de/LC_MESSAGES/django.po | 8 ++++- .../locale/en/LC_MESSAGES/django.po | 10 ++++-- .../locale/es/LC_MESSAGES/django.po | 8 ++++- .../locale/fr/LC_MESSAGES/django.po | 12 +++++-- .../locale/it/LC_MESSAGES/django.po | 8 ++++- .../locale/nl/LC_MESSAGES/django.po | 8 ++++- .../migrations/0027_sensitivearea_name.py | 33 ----------------- .../migrations/0029_sensitivearea_name.py | 36 +++++++++++++++++++ .../0030_sensitivearea_name_populate.py.off | 35 ++++++++++++++++++ geotrek/sensitivity/models.py | 24 +++++++++---- geotrek/sensitivity/serializers.py | 3 +- .../sensitivearea_detail_attributes.html | 6 ++-- geotrek/sensitivity/views.py | 4 +-- 15 files changed, 144 insertions(+), 55 deletions(-) delete mode 100644 geotrek/sensitivity/migrations/0027_sensitivearea_name.py create mode 100644 geotrek/sensitivity/migrations/0029_sensitivearea_name.py create mode 100644 geotrek/sensitivity/migrations/0030_sensitivearea_name_populate.py.off diff --git a/geotrek/api/v2/serializers.py b/geotrek/api/v2/serializers.py index b9790f0d4d..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() diff --git a/geotrek/sensitivity/forms.py b/geotrek/sensitivity/forms.py index 4c9e5c3793..8212f6a44a 100644 --- a/geotrek/sensitivity/forms.py +++ b/geotrek/sensitivity/forms.py @@ -20,6 +20,7 @@ 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")) @@ -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/0027_sensitivearea_name.py b/geotrek/sensitivity/migrations/0027_sensitivearea_name.py deleted file mode 100644 index 9c7b8fc61f..0000000000 --- a/geotrek/sensitivity/migrations/0027_sensitivearea_name.py +++ /dev/null @@ -1,33 +0,0 @@ -# Generated by Django 3.2.16 on 2023-02-09 09:27 - -from django.db import migrations, models - - -def gen_name(apps, schema_editor): - """Populate SensitiveAreas name from Species - """ - sensitive_area = apps.get_model("sensitivity", 'SensitiveArea') - for row in sensitive_area.objects.all(): - row.name = row.species.name - row.save(update_fields=['name']) - - -class Migration(migrations.Migration): - - dependencies = [ - ('sensitivity', '0026_alter_sensitivearea_geom_buffered'), - ] - - operations = [ - migrations.AddField( - model_name='sensitivearea', - name='name', - field=models.CharField(default=None, null=True, max_length=250, verbose_name='Name'), - ), - migrations.RunPython(gen_name, reverse_code=migrations.RunPython.noop), - migrations.AlterField( - model_name='sensitivearea', - name='name', - field=models.CharField(default='undefined', max_length=250, verbose_name='Name'), - ), - ] diff --git a/geotrek/sensitivity/migrations/0029_sensitivearea_name.py b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py new file mode 100644 index 0000000000..70ea4c4362 --- /dev/null +++ b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py @@ -0,0 +1,36 @@ +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 = apps.get_model('sensitivity','SensitiveArea') + # 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): + for field in update_fields: + setattr(row, field, getattr(row.species,field)) + row.save(update_fields=update_fields) + for field in update_fields: + 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), + ] \ No newline at end of file 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 d65997e54d..a5b6713fdc 100644 --- a/geotrek/sensitivity/models.py +++ b/geotrek/sensitivity/models.py @@ -186,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 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 575c4a687b..5a8a1f623d 100644 --- a/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html +++ b/geotrek/sensitivity/templates/sensitivity/sensitivearea_detail_attributes.html @@ -18,12 +18,12 @@

{% trans "Attributes" %}

{% if sensitivearea.species.category == 1 %} - {{ sensitivearea|verbose:"species" }} - {{ sensitivearea.name }} + {{ sensitivearea|verbose:"area_name" }} + {{ sensitivearea.area_name }} {% endif %} - {{ sensitivearea.species|verbose:"name" }} + {{ sensitivearea|verbose:"name" }} {{ sensitivearea.name }} 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): From 30b3b6137f39a5f5d58d07b3c7ea7871be6af99e Mon Sep 17 00:00:00 2001 From: lpofredc Date: Fri, 20 Dec 2024 14:29:13 +0100 Subject: [PATCH 3/3] WIP migrate sensitivearea model --- .../migrations/0029_sensitivearea_name.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/geotrek/sensitivity/migrations/0029_sensitivearea_name.py b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py index 70ea4c4362..da060f3c14 100644 --- a/geotrek/sensitivity/migrations/0029_sensitivearea_name.py +++ b/geotrek/sensitivity/migrations/0029_sensitivearea_name.py @@ -1,24 +1,28 @@ from django.db import migrations, models -# from django.conf import settings -# from geotrek.sensitivity.models import SensitiveArea, Species +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 = apps.get_model('sensitivity','SensitiveArea') - # languages = settings.MODELTRANSLATION_LANGUAGES + 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.filter(deleted=False, species__category=2): + 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)) + setattr(row, field, getattr(row.species, field)) row.save(update_fields=update_fields) for field in update_fields: - setattr(row.species, field, '') + 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 = [ @@ -33,4 +37,4 @@ class Migration(migrations.Migration): ), # migrations.RunPython(update_translation_fields_func, reverse_code=migrations.RunPython.noop), migrations.RunPython(generate_name, reverse_code=migrations.RunPython.noop), - ] \ No newline at end of file + ]