We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Abstract jQuery.ajax calls
Lots of jQuery.ajax calls in JavaScript code, that repeat same structure and part of the same request arguments.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/tcms/comments/views.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 import logging
4 
5 from django.conf import settings
6 from django.contrib.auth.mixins import PermissionRequiredMixin
7 from django.http import JsonResponse
8 from django.shortcuts import render
9 from django.views import generic
10 from django.views.decorators.http import require_POST
11 
12 import django_comments.signals
13 from django_comments.views.moderation import perform_delete
14 
15 from tcms.comments.models import post_comment
16 from tcms.comments.exceptions import InvalidCommentPostRequest
17 from tcms.core.responses import JsonResponseBadRequest
18 
19 log = logging.getLogger(__name__)
20 
21 
22 @require_POST
23 def post(request, template_name='comments/comments.html'):
24     """Post a comment"""
25     data = request.POST.copy()
26     try:
27         target, _ = post_comment(
28             data, request.user, request.META.get('REMOTE_ADDR'))
29     except InvalidCommentPostRequest as e:
30         target = e.target
31     return render(request, template_name, context={'object': target})
32 
33 
34 class DeleteCommentView(PermissionRequiredMixin, generic.View):
35     """Delete comment from given objects"""
36 
37     permission_required = 'django_comments.can_moderate'
38 
39     def post(self, request):
40         comments = django_comments.get_model().objects.filter(
41             pk__in=request.POST.getlist('comment_id'),
42             site__pk=settings.SITE_ID,
43             is_removed=False,
44             user_id=request.user.id
45         )
46 
47         if not comments:
48             return JsonResponseBadRequest({
49                 'message': 'No incoming comment id exists.'
50             })
51 
52         # Flag the comment as deleted instead of actually deleting it.
53         for comment in comments:
54             perform_delete(request, comment)
55 
56         return JsonResponse({})
57
```
--- 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/src/tcms/comments/views.py b/src/tcms/comments/views.py
--- a/src/tcms/comments/views.py
+++ b/src/tcms/comments/views.py
@@ -5,7 +5,6 @@
 from django.conf import settings
 from django.contrib.auth.mixins import PermissionRequiredMixin
 from django.http import JsonResponse
-from django.shortcuts import render
 from django.views import generic
 from django.views.decorators.http import require_POST
 
@@ -27,8 +26,10 @@
         target, _ = post_comment(
             data, request.user, request.META.get('REMOTE_ADDR'))
     except InvalidCommentPostRequest as e:
-        target = e.target
-    return render(request, template_name, context={'object': target})
+        msg = f'Fail to add comment to object {e.target}'
+        log.exception(msg)
+        return JsonResponseBadRequest({'message': msg})
+    return JsonResponse({})
 
 
 class DeleteCommentView(PermissionRequiredMixin, generic.View):

{"golden_diff": "diff --git a/src/tcms/comments/views.py b/src/tcms/comments/views.py\n--- a/src/tcms/comments/views.py\n+++ b/src/tcms/comments/views.py\n@@ -5,7 +5,6 @@\n from django.conf import settings\n from django.contrib.auth.mixins import PermissionRequiredMixin\n from django.http import JsonResponse\n-from django.shortcuts import render\n from django.views import generic\n from django.views.decorators.http import require_POST\n \n@@ -27,8 +26,10 @@\n         target, _ = post_comment(\n             data, request.user, request.META.get('REMOTE_ADDR'))\n     except InvalidCommentPostRequest as e:\n-        target = e.target\n-    return render(request, template_name, context={'object': target})\n+        msg = f'Fail to add comment to object {e.target}'\n+        log.exception(msg)\n+        return JsonResponseBadRequest({'message': msg})\n+    return JsonResponse({})\n \n \n class DeleteCommentView(PermissionRequiredMixin, generic.View):\n", "issue": "Abstract jQuery.ajax calls\nLots of jQuery.ajax calls in JavaScript code, that repeat same structure and part of the same request arguments.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\nimport logging\n\nfrom django.conf import settings\nfrom django.contrib.auth.mixins import PermissionRequiredMixin\nfrom django.http import JsonResponse\nfrom django.shortcuts import render\nfrom django.views import generic\nfrom django.views.decorators.http import require_POST\n\nimport django_comments.signals\nfrom django_comments.views.moderation import perform_delete\n\nfrom tcms.comments.models import post_comment\nfrom tcms.comments.exceptions import InvalidCommentPostRequest\nfrom tcms.core.responses import JsonResponseBadRequest\n\nlog = logging.getLogger(__name__)\n\n\n@require_POST\ndef post(request, template_name='comments/comments.html'):\n    \"\"\"Post a comment\"\"\"\n    data = request.POST.copy()\n    try:\n        target, _ = post_comment(\n            data, request.user, request.META.get('REMOTE_ADDR'))\n    except InvalidCommentPostRequest as e:\n        target = e.target\n    return render(request, template_name, context={'object': target})\n\n\nclass DeleteCommentView(PermissionRequiredMixin, generic.View):\n    \"\"\"Delete comment from given objects\"\"\"\n\n    permission_required = 'django_comments.can_moderate'\n\n    def post(self, request):\n        comments = django_comments.get_model().objects.filter(\n            pk__in=request.POST.getlist('comment_id'),\n            site__pk=settings.SITE_ID,\n            is_removed=False,\n            user_id=request.user.id\n        )\n\n        if not comments:\n            return JsonResponseBadRequest({\n                'message': 'No incoming comment id exists.'\n            })\n\n        # Flag the comment as deleted instead of actually deleting it.\n        for comment in comments:\n            perform_delete(request, comment)\n\n        return JsonResponse({})\n", "path": "src/tcms/comments/views.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\nimport logging\n\nfrom django.conf import settings\nfrom django.contrib.auth.mixins import PermissionRequiredMixin\nfrom django.http import JsonResponse\nfrom django.views import generic\nfrom django.views.decorators.http import require_POST\n\nimport django_comments.signals\nfrom django_comments.views.moderation import perform_delete\n\nfrom tcms.comments.models import post_comment\nfrom tcms.comments.exceptions import InvalidCommentPostRequest\nfrom tcms.core.responses import JsonResponseBadRequest\n\nlog = logging.getLogger(__name__)\n\n\n@require_POST\ndef post(request, template_name='comments/comments.html'):\n    \"\"\"Post a comment\"\"\"\n    data = request.POST.copy()\n    try:\n        target, _ = post_comment(\n            data, request.user, request.META.get('REMOTE_ADDR'))\n    except InvalidCommentPostRequest as e:\n        msg = f'Fail to add comment to object {e.target}'\n        log.exception(msg)\n        return JsonResponseBadRequest({'message': msg})\n    return JsonResponse({})\n\n\nclass DeleteCommentView(PermissionRequiredMixin, generic.View):\n    \"\"\"Delete comment from given objects\"\"\"\n\n    permission_required = 'django_comments.can_moderate'\n\n    def post(self, request):\n        comments = django_comments.get_model().objects.filter(\n            pk__in=request.POST.getlist('comment_id'),\n            site__pk=settings.SITE_ID,\n            is_removed=False,\n            user_id=request.user.id\n        )\n\n        if not comments:\n            return JsonResponseBadRequest({\n                'message': 'No incoming comment id exists.'\n            })\n\n        # Flag the comment as deleted instead of actually deleting it.\n        for comment in comments:\n            perform_delete(request, comment)\n\n        return JsonResponse({})\n", "path": "src/tcms/comments/views.py"}]}