prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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.
  
  
  </issue>
  <code>
  [start of geotrek/core/forms.py]
  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 of geotrek/core/forms.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
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"}]}