We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Should not disable edit button if having bypass structure permission
Workaround: write url by hand (eg. "/trek/edit/1/").


--- 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/authent/models.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 """
4     Models to manage users and profiles
5 """
6 from django.db import models
7 from django.contrib.auth.models import User
8 from django.conf import settings
9 from django.utils.translation import ugettext_lazy as _
10 from django.dispatch import receiver
11 from django.contrib.auth.signals import user_logged_in
12 
13 from geotrek.common.utils import reify
14 
15 
16 class Structure(models.Model):
17     """
18     Represents an organisational structure, to which users are related.
19     """
20     name = models.CharField(max_length=256, verbose_name=_(u"Nom"))
21 
22     def __unicode__(self):
23         return self.name
24 
25     class Meta:
26         verbose_name = _(u"Structure")
27         verbose_name_plural = _(u"Structures")
28         ordering = ['name']
29         permissions = (("can_bypass_structure", _("Can by structure")),)
30 
31 
32 def default_structure():
33     """ Create default structure if necessary """
34     return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]
35 
36 
37 class StructureRelatedQuerySet(models.query.QuerySet):
38     def for_user(self, user):
39         return StructureRelatedQuerySet.queryset_for_user(self, user)
40 
41     @staticmethod
42     def queryset_for_user(queryset, user):
43         return queryset.filter(structure=user.profile.structure)
44 
45 
46 class StructureRelatedManager(models.Manager):
47     """ A simple manager to manage structure related objects"""
48     def get_queryset(self):
49         return StructureRelatedQuerySet(self.model, using=self._db)
50 
51     def for_user(self, user):
52         """ Filter by user's structure """
53         return self.get_queryset().for_user(user)
54 
55 
56 class StructureRelated(models.Model):
57     """
58     A mixin used for any entities that belong to a structure
59     """
60     structure = models.ForeignKey(Structure, default=default_structure,
61                                   verbose_name=_(u"Related structure"), db_column='structure')
62 
63     objects = models.Manager()
64     in_structure = StructureRelatedManager()
65 
66     @classmethod
67     def for_user(cls, user):
68         """ Shortcut to manager's filter by user """
69         return cls.in_structure.for_user(user)
70 
71     def same_structure(self, user):
72         """ Returns True if the user is in the same structure, False otherwise. """
73         return user.profile.structure == self.structure
74 
75     class Meta:
76         abstract = True
77         verbose_name = _(u"Related structures")
78         verbose_name_plural = _(u"Related structure")
79 
80 
81 class UserProfile(StructureRelated):
82     """
83     A custom user profile
84     """
85     user = models.OneToOneField(User, unique=True)
86 
87     language = models.CharField(_(u"Language"), max_length=10,
88                                 choices=settings.LANGUAGES,
89                                 default=settings.LANGUAGE_CODE)
90 
91     class Meta:
92         verbose_name = _(u"User's profile")
93         verbose_name_plural = _(u"User's profiles")
94 
95     def __unicode__(self):
96         return _("Profile for %s") % self.user
97 
98 User.profile = reify(lambda u: UserProfile.objects.get_or_create(user=u)[0])
99 
100 
101 @receiver(user_logged_in)
102 def lang(sender, **kwargs):
103     """ Set user's language in session when he logs in. """
104     lang_code = kwargs['user'].profile.language
105     kwargs['request'].session['django_language'] = lang_code
106
```

Path: `geotrek/authent/decorators.py`
Content:
```
1 from functools import wraps
2 
3 from django.shortcuts import redirect
4 from django.contrib import messages
5 from django.http import HttpResponseRedirect
6 from django.utils.translation import ugettext_lazy as _
7 from django.utils.decorators import available_attrs
8 
9 
10 def same_structure_required(redirect_to):
11     """
12     A decorator for class-based views. It relies on ``self.get_object()``
13     method object, and assumes decorated views to handle ``StructureRelated``
14     objects.
15     """
16     def decorator(view_func):
17         @wraps(view_func, assigned=available_attrs(view_func))
18         def _wrapped_view(self, request, *args, **kwargs):
19             result = view_func(self, request, *args, **kwargs)
20 
21             # Superuser is always allowed
22             if request.user.is_superuser:
23                 return result
24 
25             if isinstance(result, HttpResponseRedirect):
26                 return result
27 
28             can_bypass_structure = request.user.has_perm('can_bypass_structure')
29             obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)
30             if can_bypass_structure or (obj and obj.same_structure(request.user)):
31                 return result
32             messages.warning(request, _(u'Access to the requested resource is restricted by structure. You have been redirected.'))
33 
34             return redirect(redirect_to, *args, **kwargs)
35         return _wrapped_view
36     return decorator
37
```
--- 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()

```

diff --git a/geotrek/authent/decorators.py b/geotrek/authent/decorators.py
--- a/geotrek/authent/decorators.py
+++ b/geotrek/authent/decorators.py
@@ -18,16 +18,11 @@
         def _wrapped_view(self, request, *args, **kwargs):
             result = view_func(self, request, *args, **kwargs)
 
-            # Superuser is always allowed
-            if request.user.is_superuser:
-                return result
-
             if isinstance(result, HttpResponseRedirect):
                 return result
 
-            can_bypass_structure = request.user.has_perm('can_bypass_structure')
             obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)
-            if can_bypass_structure or (obj and obj.same_structure(request.user)):
+            if obj.same_structure(request.user):
                 return result
             messages.warning(request, _(u'Access to the requested resource is restricted by structure. You have been redirected.'))
 
diff --git a/geotrek/authent/models.py b/geotrek/authent/models.py
--- a/geotrek/authent/models.py
+++ b/geotrek/authent/models.py
@@ -26,7 +26,7 @@
         verbose_name = _(u"Structure")
         verbose_name_plural = _(u"Structures")
         ordering = ['name']
-        permissions = (("can_bypass_structure", _("Can by structure")),)
+        permissions = (("can_bypass_structure", _("Can bypass structure")),)
 
 
 def default_structure():
@@ -69,8 +69,11 @@
         return cls.in_structure.for_user(user)
 
     def same_structure(self, user):
-        """ Returns True if the user is in the same structure, False otherwise. """
-        return user.profile.structure == self.structure
+        """ Returns True if the user is in the same structure or has
+            bypass_structure permission, False otherwise. """
+        return (user.profile.structure == self.structure or
+                user.is_superuser or
+                user.has_perm('authent.can_bypass_structure'))
 
     class Meta:
         abstract = True

{"golden_diff": "diff --git a/geotrek/authent/decorators.py b/geotrek/authent/decorators.py\n--- a/geotrek/authent/decorators.py\n+++ b/geotrek/authent/decorators.py\n@@ -18,16 +18,11 @@\n         def _wrapped_view(self, request, *args, **kwargs):\n             result = view_func(self, request, *args, **kwargs)\n \n-            # Superuser is always allowed\n-            if request.user.is_superuser:\n-                return result\n-\n             if isinstance(result, HttpResponseRedirect):\n                 return result\n \n-            can_bypass_structure = request.user.has_perm('can_bypass_structure')\n             obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)\n-            if can_bypass_structure or (obj and obj.same_structure(request.user)):\n+            if obj.same_structure(request.user):\n                 return result\n             messages.warning(request, _(u'Access to the requested resource is restricted by structure. You have been redirected.'))\n \ndiff --git a/geotrek/authent/models.py b/geotrek/authent/models.py\n--- a/geotrek/authent/models.py\n+++ b/geotrek/authent/models.py\n@@ -26,7 +26,7 @@\n         verbose_name = _(u\"Structure\")\n         verbose_name_plural = _(u\"Structures\")\n         ordering = ['name']\n-        permissions = ((\"can_bypass_structure\", _(\"Can by structure\")),)\n+        permissions = ((\"can_bypass_structure\", _(\"Can bypass structure\")),)\n \n \n def default_structure():\n@@ -69,8 +69,11 @@\n         return cls.in_structure.for_user(user)\n \n     def same_structure(self, user):\n-        \"\"\" Returns True if the user is in the same structure, False otherwise. \"\"\"\n-        return user.profile.structure == self.structure\n+        \"\"\" Returns True if the user is in the same structure or has\n+            bypass_structure permission, False otherwise. \"\"\"\n+        return (user.profile.structure == self.structure or\n+                user.is_superuser or\n+                user.has_perm('authent.can_bypass_structure'))\n \n     class Meta:\n         abstract = True\n", "issue": "Should not disable edit button if having bypass structure permission\nWorkaround: write url by hand (eg. \"/trek/edit/1/\").\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n\"\"\"\n    Models to manage users and profiles\n\"\"\"\nfrom django.db import models\nfrom django.contrib.auth.models import User\nfrom django.conf import settings\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.dispatch import receiver\nfrom django.contrib.auth.signals import user_logged_in\n\nfrom geotrek.common.utils import reify\n\n\nclass Structure(models.Model):\n    \"\"\"\n    Represents an organisational structure, to which users are related.\n    \"\"\"\n    name = models.CharField(max_length=256, verbose_name=_(u\"Nom\"))\n\n    def __unicode__(self):\n        return self.name\n\n    class Meta:\n        verbose_name = _(u\"Structure\")\n        verbose_name_plural = _(u\"Structures\")\n        ordering = ['name']\n        permissions = ((\"can_bypass_structure\", _(\"Can by structure\")),)\n\n\ndef default_structure():\n    \"\"\" Create default structure if necessary \"\"\"\n    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]\n\n\nclass StructureRelatedQuerySet(models.query.QuerySet):\n    def for_user(self, user):\n        return StructureRelatedQuerySet.queryset_for_user(self, user)\n\n    @staticmethod\n    def queryset_for_user(queryset, user):\n        return queryset.filter(structure=user.profile.structure)\n\n\nclass StructureRelatedManager(models.Manager):\n    \"\"\" A simple manager to manage structure related objects\"\"\"\n    def get_queryset(self):\n        return StructureRelatedQuerySet(self.model, using=self._db)\n\n    def for_user(self, user):\n        \"\"\" Filter by user's structure \"\"\"\n        return self.get_queryset().for_user(user)\n\n\nclass StructureRelated(models.Model):\n    \"\"\"\n    A mixin used for any entities that belong to a structure\n    \"\"\"\n    structure = models.ForeignKey(Structure, default=default_structure,\n                                  verbose_name=_(u\"Related structure\"), db_column='structure')\n\n    objects = models.Manager()\n    in_structure = StructureRelatedManager()\n\n    @classmethod\n    def for_user(cls, user):\n        \"\"\" Shortcut to manager's filter by user \"\"\"\n        return cls.in_structure.for_user(user)\n\n    def same_structure(self, user):\n        \"\"\" Returns True if the user is in the same structure, False otherwise. \"\"\"\n        return user.profile.structure == self.structure\n\n    class Meta:\n        abstract = True\n        verbose_name = _(u\"Related structures\")\n        verbose_name_plural = _(u\"Related structure\")\n\n\nclass UserProfile(StructureRelated):\n    \"\"\"\n    A custom user profile\n    \"\"\"\n    user = models.OneToOneField(User, unique=True)\n\n    language = models.CharField(_(u\"Language\"), max_length=10,\n                                choices=settings.LANGUAGES,\n                                default=settings.LANGUAGE_CODE)\n\n    class Meta:\n        verbose_name = _(u\"User's profile\")\n        verbose_name_plural = _(u\"User's profiles\")\n\n    def __unicode__(self):\n        return _(\"Profile for %s\") % self.user\n\nUser.profile = reify(lambda u: UserProfile.objects.get_or_create(user=u)[0])\n\n\n@receiver(user_logged_in)\ndef lang(sender, **kwargs):\n    \"\"\" Set user's language in session when he logs in. \"\"\"\n    lang_code = kwargs['user'].profile.language\n    kwargs['request'].session['django_language'] = lang_code\n", "path": "geotrek/authent/models.py"}, {"content": "from functools import wraps\n\nfrom django.shortcuts import redirect\nfrom django.contrib import messages\nfrom django.http import HttpResponseRedirect\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.utils.decorators import available_attrs\n\n\ndef same_structure_required(redirect_to):\n    \"\"\"\n    A decorator for class-based views. It relies on ``self.get_object()``\n    method object, and assumes decorated views to handle ``StructureRelated``\n    objects.\n    \"\"\"\n    def decorator(view_func):\n        @wraps(view_func, assigned=available_attrs(view_func))\n        def _wrapped_view(self, request, *args, **kwargs):\n            result = view_func(self, request, *args, **kwargs)\n\n            # Superuser is always allowed\n            if request.user.is_superuser:\n                return result\n\n            if isinstance(result, HttpResponseRedirect):\n                return result\n\n            can_bypass_structure = request.user.has_perm('can_bypass_structure')\n            obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)\n            if can_bypass_structure or (obj and obj.same_structure(request.user)):\n                return result\n            messages.warning(request, _(u'Access to the requested resource is restricted by structure. You have been redirected.'))\n\n            return redirect(redirect_to, *args, **kwargs)\n        return _wrapped_view\n    return decorator\n", "path": "geotrek/authent/decorators.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n\"\"\"\n    Models to manage users and profiles\n\"\"\"\nfrom django.db import models\nfrom django.contrib.auth.models import User\nfrom django.conf import settings\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.dispatch import receiver\nfrom django.contrib.auth.signals import user_logged_in\n\nfrom geotrek.common.utils import reify\n\n\nclass Structure(models.Model):\n    \"\"\"\n    Represents an organisational structure, to which users are related.\n    \"\"\"\n    name = models.CharField(max_length=256, verbose_name=_(u\"Nom\"))\n\n    def __unicode__(self):\n        return self.name\n\n    class Meta:\n        verbose_name = _(u\"Structure\")\n        verbose_name_plural = _(u\"Structures\")\n        ordering = ['name']\n        permissions = ((\"can_bypass_structure\", _(\"Can bypass structure\")),)\n\n\ndef default_structure():\n    \"\"\" Create default structure if necessary \"\"\"\n    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]\n\n\nclass StructureRelatedQuerySet(models.query.QuerySet):\n    def for_user(self, user):\n        return StructureRelatedQuerySet.queryset_for_user(self, user)\n\n    @staticmethod\n    def queryset_for_user(queryset, user):\n        return queryset.filter(structure=user.profile.structure)\n\n\nclass StructureRelatedManager(models.Manager):\n    \"\"\" A simple manager to manage structure related objects\"\"\"\n    def get_queryset(self):\n        return StructureRelatedQuerySet(self.model, using=self._db)\n\n    def for_user(self, user):\n        \"\"\" Filter by user's structure \"\"\"\n        return self.get_queryset().for_user(user)\n\n\nclass StructureRelated(models.Model):\n    \"\"\"\n    A mixin used for any entities that belong to a structure\n    \"\"\"\n    structure = models.ForeignKey(Structure, default=default_structure,\n                                  verbose_name=_(u\"Related structure\"), db_column='structure')\n\n    objects = models.Manager()\n    in_structure = StructureRelatedManager()\n\n    @classmethod\n    def for_user(cls, user):\n        \"\"\" Shortcut to manager's filter by user \"\"\"\n        return cls.in_structure.for_user(user)\n\n    def same_structure(self, user):\n        \"\"\" Returns True if the user is in the same structure or has\n            bypass_structure permission, False otherwise. \"\"\"\n        return (user.profile.structure == self.structure or\n                user.is_superuser or\n                user.has_perm('authent.can_bypass_structure'))\n\n    class Meta:\n        abstract = True\n        verbose_name = _(u\"Related structures\")\n        verbose_name_plural = _(u\"Related structure\")\n\n\nclass UserProfile(StructureRelated):\n    \"\"\"\n    A custom user profile\n    \"\"\"\n    user = models.OneToOneField(User, unique=True)\n\n    language = models.CharField(_(u\"Language\"), max_length=10,\n                                choices=settings.LANGUAGES,\n                                default=settings.LANGUAGE_CODE)\n\n    class Meta:\n        verbose_name = _(u\"User's profile\")\n        verbose_name_plural = _(u\"User's profiles\")\n\n    def __unicode__(self):\n        return _(\"Profile for %s\") % self.user\n\nUser.profile = reify(lambda u: UserProfile.objects.get_or_create(user=u)[0])\n\n\n@receiver(user_logged_in)\ndef lang(sender, **kwargs):\n    \"\"\" Set user's language in session when he logs in. \"\"\"\n    lang_code = kwargs['user'].profile.language\n    kwargs['request'].session['django_language'] = lang_code\n", "path": "geotrek/authent/models.py"}, {"content": "from functools import wraps\n\nfrom django.shortcuts import redirect\nfrom django.contrib import messages\nfrom django.http import HttpResponseRedirect\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.utils.decorators import available_attrs\n\n\ndef same_structure_required(redirect_to):\n    \"\"\"\n    A decorator for class-based views. It relies on ``self.get_object()``\n    method object, and assumes decorated views to handle ``StructureRelated``\n    objects.\n    \"\"\"\n    def decorator(view_func):\n        @wraps(view_func, assigned=available_attrs(view_func))\n        def _wrapped_view(self, request, *args, **kwargs):\n            result = view_func(self, request, *args, **kwargs)\n\n            if isinstance(result, HttpResponseRedirect):\n                return result\n\n            obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)\n            if obj.same_structure(request.user):\n                return result\n            messages.warning(request, _(u'Access to the requested resource is restricted by structure. You have been redirected.'))\n\n            return redirect(redirect_to, *args, **kwargs)\n        return _wrapped_view\n    return decorator\n", "path": "geotrek/authent/decorators.py"}]}