Skip to content
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

Sensitivity name to sensitivearea model #4403

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
4 changes: 2 additions & 2 deletions geotrek/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion geotrek/diving/templates/diving/dive_public_pdf_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ <h2>{% trans "Environmental sensitive areas" %}</h2>
{% for area in object.published_sensitive_areas %}
<div class="text-content">
{% if area.species.pictogram %}<img src="file://{{ area.species.pictogram.path }}">{% endif %}
<h3>{{ area.species.name }}</h3>
<h3>{{ area.name }}</h3>
<p>{% trans "Sensitivity period:" %} {{ area.pretty_period }}</p>
{% if area.contact %}<p>{% trans "Contact:" %} {{ area.contact|safe }}</p>{% endif %}
<p>{{ area.description|safe }}</p>
Expand Down
6 changes: 3 additions & 3 deletions geotrek/sensitivity/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()}

Expand Down Expand Up @@ -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(),
Expand 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']
Expand Down
8 changes: 7 additions & 1 deletion geotrek/sensitivity/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -112,6 +112,9 @@ msgstr ""
msgid "Species"
msgstr ""

msgid "Area name"
msgstr ""

msgid "Species or regulatory area"
msgstr ""

Expand All @@ -133,6 +136,9 @@ msgstr ""
msgid "Sensitive areas"
msgstr ""

msgid "Published name"
msgstr ""

msgid "Radius"
msgstr ""

Expand Down
10 changes: 8 additions & 2 deletions geotrek/sensitivity/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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 ""

Expand All @@ -133,6 +136,9 @@ msgstr ""
msgid "Sensitive areas"
msgstr ""

msgid "Published name"
msgstr ""

msgid "Radius"
msgstr ""

Expand Down
8 changes: 7 additions & 1 deletion geotrek/sensitivity/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -112,6 +112,9 @@ msgstr ""
msgid "Species"
msgstr ""

msgid "Area name"
msgstr ""

msgid "Species or regulatory area"
msgstr ""

Expand All @@ -133,6 +136,9 @@ msgstr ""
msgid "Sensitive areas"
msgstr ""

msgid "Published name"
msgstr ""

msgid "Radius"
msgstr ""

Expand Down
12 changes: 10 additions & 2 deletions geotrek/sensitivity/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>\n"
"Language-Team: French <https://weblate.makina-corpus.net/projects/geotrek-"
Expand Down Expand Up @@ -113,8 +113,11 @@ msgstr "ID externe"
msgid "Species"
msgstr "Espèces"

msgid "Area name"
msgstr "Nom de la zone"

msgid "Species or regulatory area"
msgstr "Espèce ou zone réglementaire"
msgstr "Espèce"

msgid "Published"
msgstr "Publiée"
Expand All @@ -134,6 +137,11 @@ msgstr "Zone de sensibilité"
msgid "Sensitive areas"
msgstr "Zones sensibles"

#, fuzzy
#| msgid "Published"
msgid "Published name"
msgstr "Publiée"

msgid "Radius"
msgstr "Rayon"

Expand Down
8 changes: 7 additions & 1 deletion geotrek/sensitivity/locale/it/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -112,6 +112,9 @@ msgstr ""
msgid "Species"
msgstr ""

msgid "Area name"
msgstr ""

msgid "Species or regulatory area"
msgstr ""

Expand All @@ -133,6 +136,9 @@ msgstr ""
msgid "Sensitive areas"
msgstr ""

msgid "Published name"
msgstr ""

msgid "Radius"
msgstr ""

Expand Down
8 changes: 7 additions & 1 deletion geotrek/sensitivity/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -112,6 +112,9 @@ msgstr ""
msgid "Species"
msgstr ""

msgid "Area name"
msgstr ""

msgid "Species or regulatory area"
msgstr ""

Expand All @@ -133,6 +136,9 @@ msgstr ""
msgid "Sensitive areas"
msgstr ""

msgid "Published name"
msgstr ""

msgid "Radius"
msgstr ""

Expand Down
40 changes: 40 additions & 0 deletions geotrek/sensitivity/migrations/0029_sensitivearea_name.py
Original file line number Diff line number Diff line change
@@ -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),
]
Original file line number Diff line number Diff line change
@@ -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),
]
29 changes: 21 additions & 8 deletions geotrek/sensitivity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -120,7 +121,7 @@ class Meta:
)

def __str__(self):
return self.species.name
return self.name

@property
def radius(self):
Expand Down Expand Up @@ -185,15 +186,27 @@ def published_langs(self):
return []

@property
def species_display(self):
s = '<a data-pk="%s" href="%s" title="%s">%s</a>' % (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'<a data-pk="{self.pk}" href="{self.get_detail_url()}" title="{self.area_name}">{self.area_name}</a>'
if self.published:
s = '<span class="badge badge-success" title="%s">&#x2606;</span> ' % _("Published") + s
s = f"""<span class="badge badge-success" title="{_('Published')}">&#x2606;</span> {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
Expand All @@ -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]))
Expand Down
3 changes: 2 additions & 1 deletion geotrek/sensitivity/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading