Skip to content

Commit

Permalink
Merge pull request #3128 from GeotrekCE/add_settings_confirmation_del…
Browse files Browse the repository at this point in the history
…etion_path

Add setting ALLOW_PATH_DELETION_TOPOLOGY
  • Loading branch information
LePetitTim authored Jun 7, 2022
2 parents d7ff666 + 9084cfb commit cdb55e8
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
**New features**

- Add filter valid geometries on topologies (#2515)[3.1]
- Add setting `ALLOW_PATH_DELETION_TOPOLOGY` which protect or not against deletion of path with topologies linked to it (#2515)[3.3.1]


2.83.0 (2022-05-01)
Expand Down
7 changes: 7 additions & 0 deletions docs/install/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,13 @@ If false, no mail will be sent to the sender of any feedback on Geotrek-rando we
Use booklet for PDF. During the synchro, pois details will be removed, and the pages will be merged.
It is possible to customize the pdf, with trek_public_booklet_pdf.html.

::

ALLOW_PATH_DELETION_TOPOLOGY = True

If false, it forbid to delete a path when at least one topology is linked to this path.


**Custom SQL**

Put your custom SQL in a file name ``/opt/geotrek-admin/var/conf/extra_sql/<app name>/<pre or post>_<script name>.sql``
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\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 @@ -131,6 +131,9 @@ msgstr ""
msgid "Paths"
msgstr ""

msgid "You can't delete this path, some topologies are linked with this path"
msgstr ""

msgid "Trails"
msgstr ""

Expand Down Expand Up @@ -231,6 +234,15 @@ msgstr ""
msgid "Different topologies are linked with this path"
msgstr ""

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr ""

msgid "No, back to edit view"
msgstr ""

msgid "GPX"
msgstr ""

Expand Down Expand Up @@ -264,6 +276,9 @@ msgstr ""
msgid "Cancel"
msgstr ""

msgid "path"
msgstr ""

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr ""
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\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 @@ -131,6 +131,9 @@ msgstr ""
msgid "Paths"
msgstr ""

msgid "You can't delete this path, some topologies are linked with this path"
msgstr ""

msgid "Trails"
msgstr ""

Expand Down Expand Up @@ -231,6 +234,15 @@ msgstr ""
msgid "Different topologies are linked with this path"
msgstr ""

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr ""

msgid "No, back to edit view"
msgstr ""

msgid "GPX"
msgstr ""

Expand Down Expand Up @@ -264,6 +276,9 @@ msgstr ""
msgid "Cancel"
msgstr ""

msgid "path"
msgstr ""

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr ""
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\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 @@ -131,6 +131,9 @@ msgstr ""
msgid "Paths"
msgstr ""

msgid "You can't delete this path, some topologies are linked with this path"
msgstr ""

msgid "Trails"
msgstr ""

Expand Down Expand Up @@ -231,6 +234,15 @@ msgstr ""
msgid "Different topologies are linked with this path"
msgstr ""

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr ""

msgid "No, back to edit view"
msgstr ""

msgid "GPX"
msgstr ""

Expand Down Expand Up @@ -264,6 +276,9 @@ msgstr ""
msgid "Cancel"
msgstr ""

msgid "path"
msgstr ""

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr ""
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\n"
"PO-Revision-Date: 2020-09-23 07:10+0000\n"
"Last-Translator: Emmanuelle Helly <[email protected]>\n"
"Language-Team: French <https://weblate.makina-corpus.net/projects/geotrek-"
Expand Down Expand Up @@ -132,6 +132,9 @@ msgstr "Tronçon"
msgid "Paths"
msgstr "Tronçons"

msgid "You can't delete this path, some topologies are linked with this path"
msgstr "Vous ne pouvez pas supprimer ce tronçon, des topologies sont liés à ce tronçon"

msgid "Trails"
msgstr "Sentiers"

Expand Down Expand Up @@ -232,6 +235,15 @@ msgstr "Non, retour à la liste"
msgid "Different topologies are linked with this path"
msgstr "Différentes Topologies sont liées à ce tronçon"

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr "Vous ne pouvez pas supprimer <strong>%(object)s</strong>, des topologies sont lié à ce tronçon. Modifié ces topologies avant de supprimer ce tronçon"

msgid "No, back to edit view"
msgstr "Non, retour à la l'édition"

msgid "GPX"
msgstr "GPX"

Expand Down Expand Up @@ -265,6 +277,9 @@ msgstr "Fusionner les tronçons"
msgid "Cancel"
msgstr "Annuler"

msgid "path"
msgstr "tronçon"

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr "L'accès à cette ressource est restreint. Vous avez été redirigé."
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\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 @@ -131,6 +131,9 @@ msgstr ""
msgid "Paths"
msgstr ""

msgid "You can't delete this path, some topologies are linked with this path"
msgstr ""

msgid "Trails"
msgstr ""

Expand Down Expand Up @@ -231,6 +234,15 @@ msgstr ""
msgid "Different topologies are linked with this path"
msgstr ""

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr ""

msgid "No, back to edit view"
msgstr ""

msgid "GPX"
msgstr ""

Expand Down Expand Up @@ -264,6 +276,9 @@ msgstr ""
msgid "Cancel"
msgstr ""

msgid "path"
msgstr ""

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr ""
Expand Down
17 changes: 16 additions & 1 deletion geotrek/core/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: 2022-01-11 16:02+0000\n"
"POT-Creation-Date: 2022-06-03 09:53+0000\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 @@ -131,6 +131,9 @@ msgstr ""
msgid "Paths"
msgstr ""

msgid "You can't delete this path, some topologies are linked with this path"
msgstr ""

msgid "Trails"
msgstr ""

Expand Down Expand Up @@ -231,6 +234,15 @@ msgstr ""
msgid "Different topologies are linked with this path"
msgstr ""

#, python-format
msgid ""
"You can't delete <strong>%(object)s</strong>, some topologies are linked "
"with this path. Modify these topologies before deleting this path."
msgstr ""

msgid "No, back to edit view"
msgstr ""

msgid "GPX"
msgstr ""

Expand Down Expand Up @@ -264,6 +276,9 @@ msgstr ""
msgid "Cancel"
msgstr ""

msgid "path"
msgstr ""

msgid ""
"Access to the requested resource is restricted. You have been redirected."
msgstr ""
Expand Down
8 changes: 6 additions & 2 deletions geotrek/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.gis.geos import Point, fromstr, LineString, GEOSGeometry
from django.contrib.postgres.indexes import GistIndex
from django.db import connection, connections, DEFAULT_DB_ALIAS
from django.db.models import ProtectedError
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as _
from modelcluster.fields import ParentalKey
Expand Down Expand Up @@ -243,11 +244,14 @@ def save(self, *args, **kwargs):
def delete(self, *args, **kwargs):
if not settings.TREKKING_TOPOLOGY_ENABLED:
return super().delete(*args, **kwargs)
topologies = list(self.topology_set.filter())
topologies = self.topology_set.all()
if topologies.exists() and not settings.ALLOW_PATH_DELETION_TOPOLOGY:
raise ProtectedError(_("You can't delete this path, some topologies are linked with this path"), self)
topologies_list = list(topologies)
r = super().delete(*args, **kwargs)
if not Path.objects.exists():
return r
for topology in topologies:
for topology in topologies_list:
if isinstance(topology.geom, Point):
closest = self.closest(topology.geom, self)
position, offset = closest.interpolate(topology.geom)
Expand Down
22 changes: 19 additions & 3 deletions geotrek/core/templates/core/path_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
{% extends "mapentity/mapentity_confirm_delete.html" %}
{% load static i18n %}
{% load static i18n geotrek_tags %}

{% block mainpanel %}
{{ block.super }}

{% settings_value "ALLOW_PATH_DELETION_TOPOLOGY" as allowed_path_deletion_topology %}
<div class="div-center">
<div class="alert alert-block">
{% if topologies_by_model|length and not allowed_path_deletion_topology %}
<h4 class="alert-heading">Warning!</h4>
{% blocktrans %}You can't delete <strong>{{ object }}</strong>, some topologies are linked with this path. Modify these topologies before deleting this path.{% endblocktrans %}
{% else %}
<h4 class="alert-heading">Warning!</h4>
{% blocktrans %}Do you really wish to delete <strong>{{ object }}</strong> ?{% endblocktrans %}
{% endif %}
</div>
{% if allowed_path_deletion_topology or not topologies_by_model|length %}
<form method="POST" action="{{ object.get_delete_url }}">
{% csrf_token %}
<input type="submit" class="btn btn-danger" value="{% trans "Yes, delete" %}"></input>
<a class="btn btn-outline-secondary" href="{{ object.get_update_url }}">{% trans "No, back to edit view" %}</a>
</form>
{% endif %}
<br>
{% if topologies_by_model|length %}
<div class="panel-heading">{% trans 'Different topologies are linked with this path' %} :</div>
{% endif %}
Expand Down
28 changes: 27 additions & 1 deletion geotrek/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
from unittest import skipIf

from django.test import TestCase
from django.test.utils import override_settings
from django.conf import settings
from django.contrib.gis.geos import LineString, Point
from django.db import IntegrityError
from django.db.models import ProtectedError

from geotrek.common.utils import dbnow
from geotrek.authent.tests.factories import StructureFactory, UserFactory
from geotrek.authent.models import Structure
from geotrek.core.tests.factories import (ComfortFactory, PathFactory, StakeFactory, TrailFactory)
from geotrek.core.models import Path
from geotrek.core.models import Path, Trail


@skipIf(not settings.TREKKING_TOPOLOGY_ENABLED, 'Test with dynamic segmentation only')
Expand Down Expand Up @@ -93,6 +95,30 @@ def test_extent(self):
self.assertAlmostEqual(lng_max, 3.0013039767202154)
self.assertAlmostEqual(lat_max, 46.50090044234927)

@skipIf(not settings.TREKKING_TOPOLOGY_ENABLED, 'Test with dynamic segmentation only')
def test_delete_protected_allow_path(self):
p1 = PathFactory.create()
p2 = PathFactory.create()
t = TrailFactory.create(paths=[p1, p2])

# Everything should be all right before delete
self.assertFalse(t.deleted)
self.assertEqual(t.aggregations.count(), 2)

p1.delete()
t = Trail.objects.get(pk=t.pk)
self.assertFalse(t.deleted)
self.assertEqual(t.aggregations.count(), 1)

with override_settings(ALLOW_PATH_DELETION_TOPOLOGY=False):
with self.assertRaisesRegex(ProtectedError,
"You can't delete this path, some topologies are linked with this path"):
p2.delete()

t = Trail.objects.get(pk=t.pk)
self.assertFalse(t.deleted)
self.assertEqual(t.aggregations.count(), 1)


@skipIf(not settings.TREKKING_TOPOLOGY_ENABLED, 'Test with dynamic segmentation only')
class InterpolateTest(TestCase):
Expand Down
Loading

0 comments on commit cdb55e8

Please sign in to comment.