prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Redirect anonymous users to login view instead of raising 403
  this also raises 403 if users are not logged in. this is not what we want
  
  _Originally posted by @jeriox in https://github.com/jeriox/jep/pull/48#discussion_r479789720_
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `jep/permissions.py`
  Content:
  ```
  1 import guardian.mixins
  2 from django.contrib.auth.models import Permission, Group
  3 from guardian.ctypes import get_content_type
  4 from guardian.utils import get_group_obj_perms_model
  5 
  6 
  7 def get_groups_with_perms(obj, only_with_perms_in):
  8 
  9     ctype = get_content_type(obj)
  10     group_model = get_group_obj_perms_model(obj)
  11 
  12     group_rel_name = group_model.group.field.related_query_name()
  13 
  14     if group_model.objects.is_generic():
  15         group_filters = {
  16             "%s__content_type" % group_rel_name: ctype,
  17             "%s__object_pk" % group_rel_name: obj.pk,
  18         }
  19     else:
  20         group_filters = {"%s__content_object" % group_rel_name: obj}
  21 
  22     permission_ids = Permission.objects.filter(
  23         content_type=ctype, codename__in=only_with_perms_in
  24     ).values_list("id", flat=True)
  25     group_filters.update(
  26         {"%s__permission_id__in" % group_rel_name: permission_ids,}
  27     )
  28     return Group.objects.filter(**group_filters).distinct()
  29 
  30 
  31 class CustomPermissionRequiredMixin(guardian.mixins.PermissionRequiredMixin):
  32     raise_exception = True
  33     accept_global_perms = True
  34 
  35     # FIXME redirect non logged in users and raise Permission for others
  36
  ```
  --- 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/jep/permissions.py b/jep/permissions.py
  --- a/jep/permissions.py
  +++ b/jep/permissions.py
  @@ -1,8 +1,12 @@
   import guardian.mixins
  +from django.contrib.auth import REDIRECT_FIELD_NAME
   from django.contrib.auth.models import Permission, Group
  +from django.contrib.auth.views import redirect_to_login
   from guardian.ctypes import get_content_type
   from guardian.utils import get_group_obj_perms_model
   
  +from jep import settings
  +
   
   def get_groups_with_perms(obj, only_with_perms_in):
   
  @@ -32,4 +36,10 @@
       raise_exception = True
       accept_global_perms = True
   
  -    # FIXME redirect non logged in users and raise Permission for others
  +    def on_permission_check_fail(self, request, response, obj=None):
  +        if request.user.is_authenticated:
  +            return response
  +        else:
  +            return redirect_to_login(
  +                self.request.get_full_path(), settings.LOGIN_URL, REDIRECT_FIELD_NAME
  +            )
  
verification_info: {"golden_diff": "diff --git a/jep/permissions.py b/jep/permissions.py\n--- a/jep/permissions.py\n+++ b/jep/permissions.py\n@@ -1,8 +1,12 @@\n import guardian.mixins\n+from django.contrib.auth import REDIRECT_FIELD_NAME\n from django.contrib.auth.models import Permission, Group\n+from django.contrib.auth.views import redirect_to_login\n from guardian.ctypes import get_content_type\n from guardian.utils import get_group_obj_perms_model\n \n+from jep import settings\n+\n \n def get_groups_with_perms(obj, only_with_perms_in):\n \n@@ -32,4 +36,10 @@\n     raise_exception = True\n     accept_global_perms = True\n \n-    # FIXME redirect non logged in users and raise Permission for others\n+    def on_permission_check_fail(self, request, response, obj=None):\n+        if request.user.is_authenticated:\n+            return response\n+        else:\n+            return redirect_to_login(\n+                self.request.get_full_path(), settings.LOGIN_URL, REDIRECT_FIELD_NAME\n+            )\n", "issue": "Redirect anonymous users to login view instead of raising 403\nthis also raises 403 if users are not logged in. this is not what we want\r\n\r\n_Originally posted by @jeriox in https://github.com/jeriox/jep/pull/48#discussion_r479789720_\n", "before_files": [{"content": "import guardian.mixins\nfrom django.contrib.auth.models import Permission, Group\nfrom guardian.ctypes import get_content_type\nfrom guardian.utils import get_group_obj_perms_model\n\n\ndef get_groups_with_perms(obj, only_with_perms_in):\n\n    ctype = get_content_type(obj)\n    group_model = get_group_obj_perms_model(obj)\n\n    group_rel_name = group_model.group.field.related_query_name()\n\n    if group_model.objects.is_generic():\n        group_filters = {\n            \"%s__content_type\" % group_rel_name: ctype,\n            \"%s__object_pk\" % group_rel_name: obj.pk,\n        }\n    else:\n        group_filters = {\"%s__content_object\" % group_rel_name: obj}\n\n    permission_ids = Permission.objects.filter(\n        content_type=ctype, codename__in=only_with_perms_in\n    ).values_list(\"id\", flat=True)\n    group_filters.update(\n        {\"%s__permission_id__in\" % group_rel_name: permission_ids,}\n    )\n    return Group.objects.filter(**group_filters).distinct()\n\n\nclass CustomPermissionRequiredMixin(guardian.mixins.PermissionRequiredMixin):\n    raise_exception = True\n    accept_global_perms = True\n\n    # FIXME redirect non logged in users and raise Permission for others\n", "path": "jep/permissions.py"}], "after_files": [{"content": "import guardian.mixins\nfrom django.contrib.auth import REDIRECT_FIELD_NAME\nfrom django.contrib.auth.models import Permission, Group\nfrom django.contrib.auth.views import redirect_to_login\nfrom guardian.ctypes import get_content_type\nfrom guardian.utils import get_group_obj_perms_model\n\nfrom jep import settings\n\n\ndef get_groups_with_perms(obj, only_with_perms_in):\n\n    ctype = get_content_type(obj)\n    group_model = get_group_obj_perms_model(obj)\n\n    group_rel_name = group_model.group.field.related_query_name()\n\n    if group_model.objects.is_generic():\n        group_filters = {\n            \"%s__content_type\" % group_rel_name: ctype,\n            \"%s__object_pk\" % group_rel_name: obj.pk,\n        }\n    else:\n        group_filters = {\"%s__content_object\" % group_rel_name: obj}\n\n    permission_ids = Permission.objects.filter(\n        content_type=ctype, codename__in=only_with_perms_in\n    ).values_list(\"id\", flat=True)\n    group_filters.update(\n        {\"%s__permission_id__in\" % group_rel_name: permission_ids,}\n    )\n    return Group.objects.filter(**group_filters).distinct()\n\n\nclass CustomPermissionRequiredMixin(guardian.mixins.PermissionRequiredMixin):\n    raise_exception = True\n    accept_global_perms = True\n\n    def on_permission_check_fail(self, request, response, obj=None):\n        if request.user.is_authenticated:\n            return response\n        else:\n            return redirect_to_login(\n                self.request.get_full_path(), settings.LOGIN_URL, REDIRECT_FIELD_NAME\n            )\n", "path": "jep/permissions.py"}]}