Skip to content

Commit

Permalink
💫 feat: change infrastructure condition field to ManyToMany field (#3970
Browse files Browse the repository at this point in the history
)
  • Loading branch information
juggler31 committed Jul 19, 2024
1 parent 95b1c8b commit 1197850
Show file tree
Hide file tree
Showing 26 changed files with 138 additions and 52 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ CHANGELOG
2.108.0+dev (XXXX-XX-XX)
----------------------------

**Improvements**

- Change infrastructure condition field to ManyToMany field (#3970)

**Bug fixes**

- Fix empty linetring in reorder_topology cmd (fixes #4092)
Expand Down
4 changes: 2 additions & 2 deletions geotrek/api/tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
])

INFRASTRUCTURE_DETAIL_JSON_STRUCTURE = sorted([
'id', 'accessibility', 'attachments', 'condition', 'description', 'eid', 'geometry',
'id', 'accessibility', 'attachments', 'conditions', 'description', 'eid', 'geometry',
'implantation_year', 'maintenance_difficulty', 'name', 'provider', 'structure',
'type', 'usage_difficulty', 'uuid'
])
Expand Down Expand Up @@ -453,9 +453,9 @@ def setUpTestData(cls):
type=cls.infrastructure_type,
usage_difficulty=cls.infrastructure_usagedifficulty,
maintenance_difficulty=cls.infrastructure_maintenancedifficulty,
condition=cls.infrastructure_condition,
published=True
)
cls.infrastructure.conditions.add(cls.infrastructure_condition)
cls.bladetype = signage_factory.BladeTypeFactory(
)
cls.color = signage_factory.BladeColorFactory()
Expand Down
2 changes: 1 addition & 1 deletion geotrek/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ def get_accessibility(self, obj):

class Meta:
model = infrastructure_models.Infrastructure
fields = ('id', 'accessibility', 'attachments', 'condition', 'description', 'eid', 'geometry', 'name',
fields = ('id', 'accessibility', 'attachments', 'conditions', 'description', 'eid', 'geometry', 'name',
'implantation_year', 'maintenance_difficulty', 'provider', 'structure', 'type', 'usage_difficulty', 'uuid')

class InfrastructureConditionSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
Expand Down
4 changes: 2 additions & 2 deletions geotrek/api/v2/views/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class InfrastructureViewSet(api_viewsets.GeotrekGeometricViewset):
.annotate(geom3d_transformed=Transform(F('geom_3d'), settings.API_SRID)) \
.prefetch_related('topo_object__aggregations',
Prefetch('attachments',
queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))) \
.order_by('pk')
queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure')),
'conditions').order_by('pk')


class InfrastructureTypeViewSet(api_viewsets.GeotrekViewSet):
Expand Down
2 changes: 1 addition & 1 deletion geotrek/infrastructure/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class InfrastructureFilterSet(AltimetryAllGeometriesFilterSet, ValidTopologyFilt
class Meta(StructureRelatedFilterSet.Meta):
model = Infrastructure
fields = StructureRelatedFilterSet.Meta.fields + [
'category', 'type', 'condition', 'implantation_year',
'category', 'type', 'conditions', 'implantation_year',
'intervention_year', 'published', 'provider', 'access'
]

Expand Down
4 changes: 2 additions & 2 deletions geotrek/infrastructure/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InfrastructureForm(BaseInfrastructureForm):
'description',
'accessibility',
'type',
'condition',
'conditions',
'access',
'implantation_year',
'usage_difficulty',
Expand All @@ -54,4 +54,4 @@ class InfrastructureForm(BaseInfrastructureForm):

class Meta(BaseInfrastructureForm.Meta):
model = Infrastructure
fields = BaseInfrastructureForm.Meta.fields + ['accessibility', 'maintenance_difficulty', 'usage_difficulty', 'condition']
fields = BaseInfrastructureForm.Meta.fields + ['accessibility', 'maintenance_difficulty', 'usage_difficulty', 'conditions']
4 changes: 2 additions & 2 deletions geotrek/infrastructure/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-07-18 14:36+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -108,7 +108,7 @@ msgstr ""
msgid "Accessibility"
msgstr ""

msgid "Condition"
msgid "Conditions"
msgstr ""

msgid "Infrastructures"
Expand Down
4 changes: 2 additions & 2 deletions geotrek/infrastructure/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-07-18 14:36+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -108,7 +108,7 @@ msgstr ""
msgid "Accessibility"
msgstr ""

msgid "Condition"
msgid "Conditions"
msgstr ""

msgid "Infrastructures"
Expand Down
4 changes: 2 additions & 2 deletions geotrek/infrastructure/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-07-18 14:36+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -108,7 +108,7 @@ msgstr ""
msgid "Accessibility"
msgstr ""

msgid "Condition"
msgid "Conditions"
msgstr ""

msgid "Infrastructures"
Expand Down
6 changes: 3 additions & 3 deletions geotrek/infrastructure/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: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-03 15:39+0000\n"
"POT-Creation-Date: 2024-07-18 14:36+0000\n"
"PO-Revision-Date: 2020-04-22 07:48+0000\n"
"Last-Translator: Emmanuelle Helly <emmanuelle.helly@makina-corpus.com>\n"
"Language-Team: French <https://weblate.makina-corpus.net/projects/geotrek-"
Expand Down Expand Up @@ -109,8 +109,8 @@ msgstr "Niveau de dangerosité de l'aménagement pour les usagers"
msgid "Accessibility"
msgstr "Accessibilité"

msgid "Condition"
msgstr "État"
msgid "Conditions"
msgstr "États"

msgid "Infrastructures"
msgstr "Aménagements"
Expand Down
4 changes: 2 additions & 2 deletions geotrek/infrastructure/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-07-18 14:36+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -108,7 +108,7 @@ msgstr ""
msgid "Accessibility"
msgstr ""

msgid "Condition"
msgid "Conditions"
msgstr ""

msgid "Infrastructures"
Expand Down
4 changes: 2 additions & 2 deletions geotrek/infrastructure/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-07-18 14:36+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -108,7 +108,7 @@ msgstr ""
msgid "Accessibility"
msgstr ""

msgid "Condition"
msgid "Conditions"
msgstr ""

msgid "Infrastructures"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def create_infrastructure(self, geometry, name, type, category, use_structure,
fields_without_eid = {
'type': infra_type,
'name': name,
'condition': condition_type,
'structure': structure,
'description': description,
'implantation_year': year
Expand All @@ -227,6 +226,8 @@ def create_infrastructure(self, geometry, name, type, category, use_structure,
eid=eid,
defaults=fields_without_eid
)
if condition_type:
infra.conditions.add(condition_type)
if verbosity > 0 and not created:
self.stdout.write("Update : %s with eid %s" % (name, eid))
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.25 on 2024-04-25 12:25

from django.db import migrations, models


def copy_condition_to_conditions(apps, schema_editor):
Infrastructure = apps.get_model('infrastructure', 'Infrastructure')

# Copy InfrastructureCondition ForeignKey to ManyToMany field
for infrastructure in Infrastructure.objects.all():
if infrastructure.condition:
infrastructure.conditions.add(infrastructure.condition)


class Migration(migrations.Migration):

dependencies = [
('infrastructure', '0038_rename_access_tmp_infrastructure_access'),
]

operations = [
migrations.AddField(
model_name='infrastructure',
name='conditions',
field=models.ManyToManyField(blank=True, related_name='infrastructures', to='infrastructure.InfrastructureCondition', verbose_name='Conditions'),
),
migrations.RunPython(copy_condition_to_conditions, reverse_code=migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.25 on 2024-04-25 13:27

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('infrastructure', '0039_infrastructure_conditions'),
]

operations = [
migrations.RemoveField(
model_name='infrastructure',
name='condition',
),
]
18 changes: 15 additions & 3 deletions geotrek/infrastructure/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ class Infrastructure(BaseInfrastructure, GeotrekMapEntityMixin):
on_delete=models.PROTECT,
related_name='infrastructures_set')
accessibility = models.TextField(verbose_name=_("Accessibility"), blank=True)
condition = models.ForeignKey(InfrastructureCondition,
verbose_name=_("Condition"), blank=True, null=True,
on_delete=models.PROTECT)
conditions = models.ManyToManyField(
InfrastructureCondition,
related_name='infrastructures',
verbose_name=_("Conditions"),
blank=True)

geometry_types_allowed = ["LINESTRING", "POINT"]

class Meta:
Expand Down Expand Up @@ -213,6 +216,15 @@ def tourism_infrastructures(cls, tourism_obj, queryset=None):
def outdoor_infrastructures(cls, outdoor_obj, queryset=None):
return intersecting(qs=queryset_or_model(queryset, cls), obj=outdoor_obj)

@property
def conditions_display(self):
if hasattr(self, "conditions_list"):
# Use conditions prefetched
conditions_list = self.conditions_list
else:
conditions_list = self.conditions.select_related('structure').all()
return ", ".join([str(c) for c in conditions_list])

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
for trek in self.treks.all():
Expand Down
2 changes: 1 addition & 1 deletion geotrek/infrastructure/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Meta:
class InfrastructureSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
name = serializers.CharField(source='name_display')
type = serializers.CharField(source='type_display')
condition = serializers.SlugRelatedField('label', read_only=True)
conditions = serializers.CharField(source='conditions_display')
cities = serializers.CharField(source='cities_display')
structure = serializers.SlugRelatedField('name', read_only=True)
usage_difficulty = serializers.SlugRelatedField('label', read_only=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ <h3>{% trans "Attributes" %}</h3>
<th>{{ object|verbose:"type" }}</th>
<td>{{ object.type }}</td>
</tr>
<tr>
<th>{{ object|verbose:"condition" }}</th>
<td>{{ object.condition }}</td>
<tr>
<th>{{ object|verbose:"conditions" }}</th>
<td>
<span> {{ object.conditions_display }} </span>
</td>
</tr>
<tr>
<th>{{ object|verbose:"description" }}</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CREATE VIEW {{ schema_geotrek }}.v_infrastructures AS WITH v_infra AS
{% endfor %}
CONCAT (e.min_elevation, 'm') AS altitude,
t.implantation_year,
t.condition_id,
t.access_id,
t.structure_id,
t.type_id,
Expand All @@ -44,7 +43,7 @@ SELECT a.id,
a.name_{{ lang }} AS "Name {{ lang }}",
{% endfor %}
b.label AS "Type",
c.label AS "Condition",
c.labels AS "Conditions",
{% for lang in MODELTRANSLATION_LANGUAGES %}
a.description_{{ lang }} AS "Description {{ lang }}",
{% endfor %}
Expand All @@ -71,7 +70,19 @@ SELECT a.id,
a.geom
FROM v_infra a
LEFT JOIN infrastructure_infrastructuretype b ON a.type_id = b.id
LEFT JOIN infrastructure_infrastructurecondition c ON a.condition_id = c.id

LEFT JOIN ( WITH infrastructure_condition AS (
SELECT a_1.infrastructurecondition_id,
b_2.label AS labels,
a_1.infrastructure_id
FROM infrastructure_infrastructurecondition b_2
JOIN infrastructure_infrastructure_conditions a_1 ON a_1.infrastructurecondition_id = b_2.id
)
SELECT array_to_string(array_agg(infrastructure_condition.labels), ', '::text, '_'::text)::character varying AS labels,
infrastructure_condition.infrastructure_id
FROM infrastructure_condition
GROUP BY infrastructure_condition.infrastructure_id) c ON a.topo_object_id = c.infrastructure_id

LEFT JOIN infrastructure_infrastructureusagedifficultylevel d ON a.usage_difficulty_id = d.id
LEFT JOIN infrastructure_infrastructuremaintenancedifficultylevel e ON a.maintenance_difficulty_id = e.id
LEFT JOIN common_accessmean j ON a.access_id = j.id
Expand Down
Binary file modified geotrek/infrastructure/tests/data/infrastructure.dbf
Binary file not shown.
Binary file not shown.
19 changes: 13 additions & 6 deletions geotrek/infrastructure/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,37 @@ class Meta:
label = factory.Sequence(lambda n: "Maintenance level %s" % n)


class InfrastructureFactory(TopologyFactory):
class InfrastructureFactoryMixin():
@factory.post_generation
def conditions(obj, create, extracted=None, **kwargs):
if create:
if extracted:
obj.conditions.set(extracted)
else:
obj.conditions.add(InfrastructureConditionFactory.create())


class InfrastructureFactory(TopologyFactory, InfrastructureFactoryMixin):
class Meta:
model = models.Infrastructure
name = factory.Sequence(lambda n: "Infrastructure %s" % n)
type = factory.SubFactory(InfrastructureTypeFactory)
condition = factory.SubFactory(InfrastructureConditionFactory)
published = True
usage_difficulty = factory.SubFactory(InfrastructureUsageDifficultyLevelFactory)
maintenance_difficulty = factory.SubFactory(InfrastructureMaintenanceDifficultyLevelFactory)


class PointInfrastructureFactory(PointTopologyFactory):
class PointInfrastructureFactory(PointTopologyFactory, InfrastructureFactoryMixin):
class Meta:
model = models.Infrastructure
name = factory.Sequence(lambda n: "Infrastructure %s" % n)
type = factory.SubFactory(InfrastructureTypeFactory)
condition = factory.SubFactory(InfrastructureConditionFactory)
published = True


class InfrastructureNoPictogramFactory(TopologyFactory):
class InfrastructureNoPictogramFactory(TopologyFactory, InfrastructureFactoryMixin):
class Meta:
model = models.Infrastructure
name = factory.Sequence(lambda n: "Infrastructure %s" % n)
type = factory.SubFactory(InfrastructureTypeNoPictogramFactory)
condition = factory.SubFactory(InfrastructureConditionFactory)
published = True
Loading

0 comments on commit 1197850

Please sign in to comment.