prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Invite existing users gives an IntegrityError
  See http://sentry.support.akvo-ops.org/rsr/live/group/797/.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `akvo/rest/views/employment.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 """Akvo RSR is covered by the GNU Affero General Public License.
  3 
  4 See more details in the license.txt file located at the root folder of the Akvo RSR module.
  5 For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
  6 """
  7 
  8 from django.contrib.auth.models import Group
  9 from rest_framework.decorators import api_view, permission_classes
  10 from rest_framework.exceptions import PermissionDenied
  11 from rest_framework.permissions import IsAuthenticated
  12 from rest_framework.response import Response
  13 from akvo.rsr.models import Employment
  14 from ..serializers import EmploymentSerializer
  15 from ..viewsets import BaseRSRViewSet
  16 
  17 
  18 class EmploymentViewSet(BaseRSRViewSet):
  19 
  20     """Employment resource."""
  21 
  22     queryset = Employment.objects.select_related('organisation')
  23     serializer_class = EmploymentSerializer
  24 
  25 
  26 @api_view(['POST'])
  27 @permission_classes((IsAuthenticated, ))
  28 def approve_employment(request, pk=None):
  29     employment = Employment.objects.get(pk=pk)
  30     user = request.user
  31 
  32     if not user.has_perm('rsr.change_employment', employment):
  33         raise PermissionDenied
  34 
  35     employment.approve(user)
  36 
  37     return Response({'status': 'employment approved'})
  38 
  39 
  40 @api_view(['POST'])
  41 @permission_classes((IsAuthenticated, ))
  42 def set_group(request, pk=None, group_id=None):
  43     employment = Employment.objects.get(pk=pk)
  44     group = Group.objects.get(pk=group_id)
  45     user = request.user
  46 
  47     if not user.has_perm('rsr.change_employment', employment):
  48         raise PermissionDenied
  49 
  50     employment.group = group
  51     employment.save()
  52 
  53     return Response({'status': 'group set'})
  54
  ```
  --- 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/akvo/rest/views/employment.py b/akvo/rest/views/employment.py
  --- a/akvo/rest/views/employment.py
  +++ b/akvo/rest/views/employment.py
  @@ -6,10 +6,12 @@
   """
   
   from django.contrib.auth.models import Group
  +from django.db import IntegrityError
   from rest_framework.decorators import api_view, permission_classes
   from rest_framework.exceptions import PermissionDenied
   from rest_framework.permissions import IsAuthenticated
   from rest_framework.response import Response
  +from rest_framework import status
   from akvo.rsr.models import Employment
   from ..serializers import EmploymentSerializer
   from ..viewsets import BaseRSRViewSet
  @@ -48,6 +50,10 @@
           raise PermissionDenied
   
       employment.group = group
  -    employment.save()
  +    try:
  +        employment.save()
  +    except IntegrityError:
  +        return Response({'status': 'group not set', 'error': 'Employment already exists.'},
  +                        status=status.HTTP_400_BAD_REQUEST)
   
       return Response({'status': 'group set'})
  
verification_info: {"golden_diff": "diff --git a/akvo/rest/views/employment.py b/akvo/rest/views/employment.py\n--- a/akvo/rest/views/employment.py\n+++ b/akvo/rest/views/employment.py\n@@ -6,10 +6,12 @@\n \"\"\"\n \n from django.contrib.auth.models import Group\n+from django.db import IntegrityError\n from rest_framework.decorators import api_view, permission_classes\n from rest_framework.exceptions import PermissionDenied\n from rest_framework.permissions import IsAuthenticated\n from rest_framework.response import Response\n+from rest_framework import status\n from akvo.rsr.models import Employment\n from ..serializers import EmploymentSerializer\n from ..viewsets import BaseRSRViewSet\n@@ -48,6 +50,10 @@\n         raise PermissionDenied\n \n     employment.group = group\n-    employment.save()\n+    try:\n+        employment.save()\n+    except IntegrityError:\n+        return Response({'status': 'group not set', 'error': 'Employment already exists.'},\n+                        status=status.HTTP_400_BAD_REQUEST)\n \n     return Response({'status': 'group set'})\n", "issue": "Invite existing users gives an IntegrityError\nSee http://sentry.support.akvo-ops.org/rsr/live/group/797/.\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Akvo RSR is covered by the GNU Affero General Public License.\n\nSee more details in the license.txt file located at the root folder of the Akvo RSR module.\nFor additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\"\"\"\n\nfrom django.contrib.auth.models import Group\nfrom rest_framework.decorators import api_view, permission_classes\nfrom rest_framework.exceptions import PermissionDenied\nfrom rest_framework.permissions import IsAuthenticated\nfrom rest_framework.response import Response\nfrom akvo.rsr.models import Employment\nfrom ..serializers import EmploymentSerializer\nfrom ..viewsets import BaseRSRViewSet\n\n\nclass EmploymentViewSet(BaseRSRViewSet):\n\n    \"\"\"Employment resource.\"\"\"\n\n    queryset = Employment.objects.select_related('organisation')\n    serializer_class = EmploymentSerializer\n\n\n@api_view(['POST'])\n@permission_classes((IsAuthenticated, ))\ndef approve_employment(request, pk=None):\n    employment = Employment.objects.get(pk=pk)\n    user = request.user\n\n    if not user.has_perm('rsr.change_employment', employment):\n        raise PermissionDenied\n\n    employment.approve(user)\n\n    return Response({'status': 'employment approved'})\n\n\n@api_view(['POST'])\n@permission_classes((IsAuthenticated, ))\ndef set_group(request, pk=None, group_id=None):\n    employment = Employment.objects.get(pk=pk)\n    group = Group.objects.get(pk=group_id)\n    user = request.user\n\n    if not user.has_perm('rsr.change_employment', employment):\n        raise PermissionDenied\n\n    employment.group = group\n    employment.save()\n\n    return Response({'status': 'group set'})\n", "path": "akvo/rest/views/employment.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Akvo RSR is covered by the GNU Affero General Public License.\n\nSee more details in the license.txt file located at the root folder of the Akvo RSR module.\nFor additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\"\"\"\n\nfrom django.contrib.auth.models import Group\nfrom django.db import IntegrityError\nfrom rest_framework.decorators import api_view, permission_classes\nfrom rest_framework.exceptions import PermissionDenied\nfrom rest_framework.permissions import IsAuthenticated\nfrom rest_framework.response import Response\nfrom rest_framework import status\nfrom akvo.rsr.models import Employment\nfrom ..serializers import EmploymentSerializer\nfrom ..viewsets import BaseRSRViewSet\n\n\nclass EmploymentViewSet(BaseRSRViewSet):\n\n    \"\"\"Employment resource.\"\"\"\n\n    queryset = Employment.objects.select_related('organisation')\n    serializer_class = EmploymentSerializer\n\n\n@api_view(['POST'])\n@permission_classes((IsAuthenticated, ))\ndef approve_employment(request, pk=None):\n    employment = Employment.objects.get(pk=pk)\n    user = request.user\n\n    if not user.has_perm('rsr.change_employment', employment):\n        raise PermissionDenied\n\n    employment.approve(user)\n\n    return Response({'status': 'employment approved'})\n\n\n@api_view(['POST'])\n@permission_classes((IsAuthenticated, ))\ndef set_group(request, pk=None, group_id=None):\n    employment = Employment.objects.get(pk=pk)\n    group = Group.objects.get(pk=group_id)\n    user = request.user\n\n    if not user.has_perm('rsr.change_employment', employment):\n        raise PermissionDenied\n\n    employment.group = group\n    try:\n        employment.save()\n    except IntegrityError:\n        return Response({'status': 'group not set', 'error': 'Employment already exists.'},\n                        status=status.HTTP_400_BAD_REQUEST)\n\n    return Response({'status': 'group set'})\n", "path": "akvo/rest/views/employment.py"}]}