prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  ADMIN - Tronçon bouclant sur lui-même
  Impossible de saisir le CIRCUIT DES LACS correctement.
  Renvoie souvent une 504 BAD GATEWAY quand on enregistre. L'itinéraire a pourtant été modifié mais différemment de la façon dont il a été saisi. A creuser.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `geotrek/core/forms.py`
  Content:
  ```
  1 from django.utils.translation import ugettext_lazy as _
  2 
  3 import floppyforms as forms
  4 
  5 from geotrek.common.forms import CommonForm
  6 from .models import Path
  7 from .helpers import PathHelper
  8 from .fields import TopologyField, SnappedLineStringField
  9 
  10 
  11 class TopologyForm(CommonForm):
  12     """
  13     This form is a bit specific :
  14 
  15         We use a field (topology) in order to edit the whole instance.
  16         Thus, at init, we load the instance into field, and at save, we
  17         save the field into the instance.
  18 
  19     The geom field is fully ignored, since we edit a topology.
  20     """
  21     topology = TopologyField(label="")
  22 
  23     def __init__(self, *args, **kwargs):
  24         super(TopologyForm, self).__init__(*args, **kwargs)
  25         if self.instance and self.instance.pk:
  26             self.fields['topology'].initial = self.instance
  27 
  28     def clean(self, *args, **kwargs):
  29         data = super(TopologyForm, self).clean()
  30         # geom is computed at db-level and never edited
  31         if 'geom' in self.errors:
  32             del self.errors['geom']
  33         return data
  34 
  35     def save(self, *args, **kwargs):
  36         topology = self.cleaned_data.pop('topology')
  37         instance = super(TopologyForm, self).save(*args, **kwargs)
  38         instance.mutate(topology)
  39         return instance
  40 
  41     geomfields = ['topology']
  42 
  43     class Meta(CommonForm.Meta):
  44         fields = CommonForm.Meta.fields + ['topology']
  45 
  46     MEDIA_JS = ("core/dijkstra.js",
  47                 "core/leaflet-geomutils.js",
  48                 "core/multipath.js",
  49                 "core/topology_helper.js") + CommonForm.MEDIA_JS
  50 
  51 
  52 class PathForm(CommonForm):
  53     geom = SnappedLineStringField()
  54 
  55     reverse_geom = forms.BooleanField(required=False,
  56                                       label=_("Reverse path"),
  57                                       help_text=_("The path will be reversed once saved"))
  58 
  59     geomfields = ['geom']
  60 
  61     class Meta(CommonForm.Meta):
  62         model = Path
  63         fields = CommonForm.Meta.fields + \
  64             ['structure',
  65              'name', 'stake', 'comfort', 'trail', 'departure', 'arrival', 'comments',
  66              'datasource', 'networks', 'usages', 'valid', 'reverse_geom', 'geom']
  67 
  68     def __init__(self, *args, **kwargs):
  69         super(PathForm, self).__init__(*args, **kwargs)
  70         self.fields['geom'].label = ''
  71 
  72     def clean_geom(self):
  73         geom = self.cleaned_data['geom']
  74         if geom is None:
  75             raise forms.ValidationError(_("Invalid snapped geometry."))
  76         if not geom.simple:
  77             raise forms.ValidationError(_("Geometry is not simple."))
  78         if not PathHelper.disjoint(geom, self.cleaned_data.get('pk') or -1):
  79             raise forms.ValidationError(_("Geometry overlaps another."))
  80         return geom
  81 
  82     def save(self, commit=True):
  83         path = super(PathForm, self).save(commit=False)
  84 
  85         if self.cleaned_data.get('reverse_geom'):
  86             path.reverse()
  87 
  88         if commit:
  89             path.save()
  90             self.save_m2m()
  91 
  92         return path
  93
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/geotrek/core/forms.py b/geotrek/core/forms.py
  --- a/geotrek/core/forms.py
  +++ b/geotrek/core/forms.py
  @@ -44,7 +44,6 @@
           fields = CommonForm.Meta.fields + ['topology']
   
       MEDIA_JS = ("core/dijkstra.js",
  -                "core/leaflet-geomutils.js",
                   "core/multipath.js",
                   "core/topology_helper.js") + CommonForm.MEDIA_JS
  
verification_info: {"golden_diff": "diff --git a/geotrek/core/forms.py b/geotrek/core/forms.py\n--- a/geotrek/core/forms.py\n+++ b/geotrek/core/forms.py\n@@ -44,7 +44,6 @@\n         fields = CommonForm.Meta.fields + ['topology']\n \n     MEDIA_JS = (\"core/dijkstra.js\",\n-                \"core/leaflet-geomutils.js\",\n                 \"core/multipath.js\",\n                 \"core/topology_helper.js\") + CommonForm.MEDIA_JS\n", "issue": "ADMIN - Tron\u00e7on bouclant sur lui-m\u00eame\nImpossible de saisir le CIRCUIT DES LACS correctement.\nRenvoie souvent une 504 BAD GATEWAY quand on enregistre. L'itin\u00e9raire a pourtant \u00e9t\u00e9 modifi\u00e9 mais diff\u00e9remment de la fa\u00e7on dont il a \u00e9t\u00e9 saisi. A creuser.\n\n", "before_files": [{"content": "from django.utils.translation import ugettext_lazy as _\n\nimport floppyforms as forms\n\nfrom geotrek.common.forms import CommonForm\nfrom .models import Path\nfrom .helpers import PathHelper\nfrom .fields import TopologyField, SnappedLineStringField\n\n\nclass TopologyForm(CommonForm):\n    \"\"\"\n    This form is a bit specific :\n\n        We use a field (topology) in order to edit the whole instance.\n        Thus, at init, we load the instance into field, and at save, we\n        save the field into the instance.\n\n    The geom field is fully ignored, since we edit a topology.\n    \"\"\"\n    topology = TopologyField(label=\"\")\n\n    def __init__(self, *args, **kwargs):\n        super(TopologyForm, self).__init__(*args, **kwargs)\n        if self.instance and self.instance.pk:\n            self.fields['topology'].initial = self.instance\n\n    def clean(self, *args, **kwargs):\n        data = super(TopologyForm, self).clean()\n        # geom is computed at db-level and never edited\n        if 'geom' in self.errors:\n            del self.errors['geom']\n        return data\n\n    def save(self, *args, **kwargs):\n        topology = self.cleaned_data.pop('topology')\n        instance = super(TopologyForm, self).save(*args, **kwargs)\n        instance.mutate(topology)\n        return instance\n\n    geomfields = ['topology']\n\n    class Meta(CommonForm.Meta):\n        fields = CommonForm.Meta.fields + ['topology']\n\n    MEDIA_JS = (\"core/dijkstra.js\",\n                \"core/leaflet-geomutils.js\",\n                \"core/multipath.js\",\n                \"core/topology_helper.js\") + CommonForm.MEDIA_JS\n\n\nclass PathForm(CommonForm):\n    geom = SnappedLineStringField()\n\n    reverse_geom = forms.BooleanField(required=False,\n                                      label=_(\"Reverse path\"),\n                                      help_text=_(\"The path will be reversed once saved\"))\n\n    geomfields = ['geom']\n\n    class Meta(CommonForm.Meta):\n        model = Path\n        fields = CommonForm.Meta.fields + \\\n            ['structure',\n             'name', 'stake', 'comfort', 'trail', 'departure', 'arrival', 'comments',\n             'datasource', 'networks', 'usages', 'valid', 'reverse_geom', 'geom']\n\n    def __init__(self, *args, **kwargs):\n        super(PathForm, self).__init__(*args, **kwargs)\n        self.fields['geom'].label = ''\n\n    def clean_geom(self):\n        geom = self.cleaned_data['geom']\n        if geom is None:\n            raise forms.ValidationError(_(\"Invalid snapped geometry.\"))\n        if not geom.simple:\n            raise forms.ValidationError(_(\"Geometry is not simple.\"))\n        if not PathHelper.disjoint(geom, self.cleaned_data.get('pk') or -1):\n            raise forms.ValidationError(_(\"Geometry overlaps another.\"))\n        return geom\n\n    def save(self, commit=True):\n        path = super(PathForm, self).save(commit=False)\n\n        if self.cleaned_data.get('reverse_geom'):\n            path.reverse()\n\n        if commit:\n            path.save()\n            self.save_m2m()\n\n        return path\n", "path": "geotrek/core/forms.py"}], "after_files": [{"content": "from django.utils.translation import ugettext_lazy as _\n\nimport floppyforms as forms\n\nfrom geotrek.common.forms import CommonForm\nfrom .models import Path\nfrom .helpers import PathHelper\nfrom .fields import TopologyField, SnappedLineStringField\n\n\nclass TopologyForm(CommonForm):\n    \"\"\"\n    This form is a bit specific :\n\n        We use a field (topology) in order to edit the whole instance.\n        Thus, at init, we load the instance into field, and at save, we\n        save the field into the instance.\n\n    The geom field is fully ignored, since we edit a topology.\n    \"\"\"\n    topology = TopologyField(label=\"\")\n\n    def __init__(self, *args, **kwargs):\n        super(TopologyForm, self).__init__(*args, **kwargs)\n        if self.instance and self.instance.pk:\n            self.fields['topology'].initial = self.instance\n\n    def clean(self, *args, **kwargs):\n        data = super(TopologyForm, self).clean()\n        # geom is computed at db-level and never edited\n        if 'geom' in self.errors:\n            del self.errors['geom']\n        return data\n\n    def save(self, *args, **kwargs):\n        topology = self.cleaned_data.pop('topology')\n        instance = super(TopologyForm, self).save(*args, **kwargs)\n        instance.mutate(topology)\n        return instance\n\n    geomfields = ['topology']\n\n    class Meta(CommonForm.Meta):\n        fields = CommonForm.Meta.fields + ['topology']\n\n    MEDIA_JS = (\"core/dijkstra.js\",\n                \"core/multipath.js\",\n                \"core/topology_helper.js\") + CommonForm.MEDIA_JS\n\n\nclass PathForm(CommonForm):\n    geom = SnappedLineStringField()\n\n    reverse_geom = forms.BooleanField(required=False,\n                                      label=_(\"Reverse path\"),\n                                      help_text=_(\"The path will be reversed once saved\"))\n\n    geomfields = ['geom']\n\n    class Meta(CommonForm.Meta):\n        model = Path\n        fields = CommonForm.Meta.fields + \\\n            ['structure',\n             'name', 'stake', 'comfort', 'trail', 'departure', 'arrival', 'comments',\n             'datasource', 'networks', 'usages', 'valid', 'reverse_geom', 'geom']\n\n    def __init__(self, *args, **kwargs):\n        super(PathForm, self).__init__(*args, **kwargs)\n        self.fields['geom'].label = ''\n\n    def clean_geom(self):\n        geom = self.cleaned_data['geom']\n        if geom is None:\n            raise forms.ValidationError(_(\"Invalid snapped geometry.\"))\n        if not geom.simple:\n            raise forms.ValidationError(_(\"Geometry is not simple.\"))\n        if not PathHelper.disjoint(geom, self.cleaned_data.get('pk') or -1):\n            raise forms.ValidationError(_(\"Geometry overlaps another.\"))\n        return geom\n\n    def save(self, commit=True):\n        path = super(PathForm, self).save(commit=False)\n\n        if self.cleaned_data.get('reverse_geom'):\n            path.reverse()\n\n        if commit:\n            path.save()\n            self.save_m2m()\n\n        return path\n", "path": "geotrek/core/forms.py"}]}