We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Hosts: User cannot see all of his Host Teams
## CURRENT STATE - 
`challenge-host-teams` page only shows teams `created_by` the user.

## ISSUE - 
On `challenge-host-teams` page, user should be able to see all host teams he is a part of.

## RELATED ISSUE -
#607 Refer to this issue also. 


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `apps/hosts/views.py`
Content:
```
1 from rest_framework import permissions, status
2 from rest_framework.decorators import (api_view,
3                                        authentication_classes,
4                                        permission_classes,
5                                        throttle_classes,)
6 from rest_framework.response import Response
7 from rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)
8 from rest_framework.throttling import UserRateThrottle
9 
10 from accounts.permissions import HasVerifiedEmail
11 from base.utils import paginated_queryset
12 from .models import (ChallengeHost,
13                      ChallengeHostTeam,)
14 from .serializers import (ChallengeHostSerializer,
15                           ChallengeHostTeamSerializer,
16                           InviteHostToTeamSerializer,
17                           HostTeamDetailSerializer,)
18 
19 
20 @throttle_classes([UserRateThrottle])
21 @api_view(['GET', 'POST'])
22 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
23 @authentication_classes((ExpiringTokenAuthentication,))
24 def challenge_host_team_list(request):
25 
26     if request.method == 'GET':
27         challenge_host_teams = ChallengeHostTeam.objects.filter(created_by=request.user)
28         paginator, result_page = paginated_queryset(challenge_host_teams, request)
29         serializer = HostTeamDetailSerializer(result_page, many=True)
30         response_data = serializer.data
31         return paginator.get_paginated_response(response_data)
32 
33     elif request.method == 'POST':
34         serializer = ChallengeHostTeamSerializer(data=request.data,
35                                                  context={'request': request})
36         if serializer.is_valid():
37             serializer.save()
38             response_data = serializer.data
39             return Response(response_data, status=status.HTTP_201_CREATED)
40         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
41 
42 
43 @throttle_classes([UserRateThrottle])
44 @api_view(['GET', 'PUT', 'PATCH', 'DELETE'])
45 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
46 @authentication_classes((ExpiringTokenAuthentication,))
47 def challenge_host_team_detail(request, pk):
48     try:
49         challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)
50     except ChallengeHostTeam.DoesNotExist:
51         response_data = {'error': 'ChallengeHostTeam does not exist'}
52         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
53 
54     if request.method == 'GET':
55         serializer = HostTeamDetailSerializer(challenge_host_team)
56         response_data = serializer.data
57         return Response(response_data, status=status.HTTP_200_OK)
58 
59     elif request.method in ['PUT', 'PATCH']:
60 
61         if request.method == 'PATCH':
62             serializer = ChallengeHostTeamSerializer(challenge_host_team,
63                                                      data=request.data,
64                                                      context={'request': request},
65                                                      partial=True)
66         else:
67             serializer = ChallengeHostTeamSerializer(challenge_host_team,
68                                                      data=request.data,
69                                                      context={'request': request})
70         if serializer.is_valid():
71             serializer.save()
72             response_data = serializer.data
73             return Response(response_data, status=status.HTTP_200_OK)
74         else:
75             return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
76 
77     elif request.method == 'DELETE':
78         challenge_host_team.delete()
79         return Response(status=status.HTTP_204_NO_CONTENT)
80 
81 
82 @throttle_classes([UserRateThrottle])
83 @api_view(['GET', 'POST'])
84 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
85 @authentication_classes((ExpiringTokenAuthentication,))
86 def challenge_host_list(request, challenge_host_team_pk):
87 
88     try:
89         challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)
90     except ChallengeHostTeam.DoesNotExist:
91         response_data = {'error': 'ChallengeHostTeam does not exist'}
92         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
93 
94     if request.method == 'GET':
95         challenge_host_status = request.query_params.get('status', None)
96         filter_condition = {
97             'team_name': challenge_host_team,
98             'user': request.user
99         }
100         if challenge_host_status:
101             challenge_host_status = challenge_host_status.split(',')
102             filter_condition.update({'status__in': challenge_host_status})
103 
104         challenge_host = ChallengeHost.objects.filter(**filter_condition)
105         paginator, result_page = paginated_queryset(challenge_host, request)
106         serializer = ChallengeHostSerializer(result_page, many=True)
107         response_data = serializer.data
108         return paginator.get_paginated_response(response_data)
109 
110     elif request.method == 'POST':
111         serializer = ChallengeHostSerializer(data=request.data,
112                                              context={'challenge_host_team': challenge_host_team,
113                                                       'request': request})
114         if serializer.is_valid():
115             serializer.save()
116             response_data = serializer.data
117             return Response(response_data, status=status.HTTP_201_CREATED)
118         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
119 
120 
121 @throttle_classes([UserRateThrottle])
122 @api_view(['GET', 'PUT', 'PATCH', 'DELETE'])
123 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
124 @authentication_classes((ExpiringTokenAuthentication,))
125 def challenge_host_detail(request, challenge_host_team_pk, pk):
126     try:
127         challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)
128     except ChallengeHostTeam.DoesNotExist:
129         response_data = {'error': 'ChallengeHostTeam does not exist'}
130         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
131 
132     try:
133         challenge_host = ChallengeHost.objects.get(pk=pk)
134     except ChallengeHost.DoesNotExist:
135         response_data = {'error': 'ChallengeHost does not exist'}
136         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
137 
138     if request.method == 'GET':
139         serializer = ChallengeHostSerializer(challenge_host)
140         response_data = serializer.data
141         return Response(response_data, status=status.HTTP_200_OK)
142 
143     elif request.method in ['PUT', 'PATCH']:
144         if request.method == 'PATCH':
145             serializer = ChallengeHostSerializer(challenge_host,
146                                                  data=request.data,
147                                                  context={'challenge_host_team': challenge_host_team,
148                                                           'request': request},
149                                                  partial=True)
150         else:
151             serializer = ChallengeHostSerializer(challenge_host,
152                                                  data=request.data,
153                                                  context={'challenge_host_team': challenge_host_team,
154                                                           'request': request})
155         if serializer.is_valid():
156             serializer.save()
157             response_data = serializer.data
158             return Response(response_data, status=status.HTTP_200_OK)
159         else:
160             return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
161 
162     elif request.method == 'DELETE':
163         challenge_host.delete()
164         return Response(status=status.HTTP_204_NO_CONTENT)
165 
166 
167 @throttle_classes([UserRateThrottle])
168 @api_view(['POST'])
169 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
170 @authentication_classes((ExpiringTokenAuthentication,))
171 def create_challenge_host_team(request):
172 
173     serializer = ChallengeHostTeamSerializer(data=request.data,
174                                              context={'request': request})
175     if serializer.is_valid():
176         serializer.save()
177         response_data = serializer.data
178         challenge_host_team = serializer.instance
179         challenge_host = ChallengeHost(user=request.user,
180                                        status=ChallengeHost.SELF,
181                                        permissions=ChallengeHost.ADMIN,
182                                        team_name=challenge_host_team)
183         challenge_host.save()
184         return Response(response_data, status=status.HTTP_201_CREATED)
185     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
186 
187 
188 @throttle_classes([UserRateThrottle, ])
189 @api_view(['DELETE', ])
190 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
191 @authentication_classes((ExpiringTokenAuthentication, ))
192 def remove_self_from_challenge_host_team(request, challenge_host_team_pk):
193     """
194     A user can remove himself from the challenge host team.
195     """
196     try:
197         ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)
198     except ChallengeHostTeam.DoesNotExist:
199         response_data = {'error': 'ChallengeHostTeam does not exist'}
200         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
201     try:
202         challenge_host = ChallengeHost.objects.filter(user=request.user.id, team_name__pk=challenge_host_team_pk)
203         challenge_host.delete()
204         return Response(status=status.HTTP_204_NO_CONTENT)
205     except:
206         response_data = {'error': 'Sorry, you do not belong to this team.'}
207         return Response(response_data, status=status.HTTP_401_UNAUTHORIZED)
208 
209 
210 @throttle_classes([UserRateThrottle])
211 @api_view(['POST'])
212 @permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
213 @authentication_classes((ExpiringTokenAuthentication,))
214 def invite_host_to_team(request, pk):
215 
216     try:
217         challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)
218     except ChallengeHostTeam.DoesNotExist:
219         response_data = {'error': 'ChallengeHostTeam does not exist'}
220         return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)
221 
222     serializer = InviteHostToTeamSerializer(data=request.data,
223                                             context={'challenge_host_team': challenge_host_team,
224                                                      'request': request})
225     if serializer.is_valid():
226         serializer.save()
227         response_data = {
228             'message': 'User has been added successfully to the host team'}
229         return Response(response_data, status=status.HTTP_202_ACCEPTED)
230     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
231
```
--- 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/apps/hosts/views.py b/apps/hosts/views.py
--- a/apps/hosts/views.py
+++ b/apps/hosts/views.py
@@ -24,7 +24,8 @@
 def challenge_host_team_list(request):
 
     if request.method == 'GET':
-        challenge_host_teams = ChallengeHostTeam.objects.filter(created_by=request.user)
+        challenge_host_team_ids = ChallengeHost.objects.filter(user=request.user).values_list('team_name', flat=True)
+        challenge_host_teams = ChallengeHostTeam.objects.filter(id__in=challenge_host_team_ids)
         paginator, result_page = paginated_queryset(challenge_host_teams, request)
         serializer = HostTeamDetailSerializer(result_page, many=True)
         response_data = serializer.data

{"golden_diff": "diff --git a/apps/hosts/views.py b/apps/hosts/views.py\n--- a/apps/hosts/views.py\n+++ b/apps/hosts/views.py\n@@ -24,7 +24,8 @@\n def challenge_host_team_list(request):\n \n     if request.method == 'GET':\n-        challenge_host_teams = ChallengeHostTeam.objects.filter(created_by=request.user)\n+        challenge_host_team_ids = ChallengeHost.objects.filter(user=request.user).values_list('team_name', flat=True)\n+        challenge_host_teams = ChallengeHostTeam.objects.filter(id__in=challenge_host_team_ids)\n         paginator, result_page = paginated_queryset(challenge_host_teams, request)\n         serializer = HostTeamDetailSerializer(result_page, many=True)\n         response_data = serializer.data\n", "issue": "Hosts: User cannot see all of his Host Teams\n## CURRENT STATE - \r\n`challenge-host-teams` page only shows teams `created_by` the user.\r\n\r\n## ISSUE - \r\nOn `challenge-host-teams` page, user should be able to see all host teams he is a part of.\r\n\r\n## RELATED ISSUE -\r\n#607 Refer to this issue also. \r\n\n", "before_files": [{"content": "from rest_framework import permissions, status\nfrom rest_framework.decorators import (api_view,\n                                       authentication_classes,\n                                       permission_classes,\n                                       throttle_classes,)\nfrom rest_framework.response import Response\nfrom rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)\nfrom rest_framework.throttling import UserRateThrottle\n\nfrom accounts.permissions import HasVerifiedEmail\nfrom base.utils import paginated_queryset\nfrom .models import (ChallengeHost,\n                     ChallengeHostTeam,)\nfrom .serializers import (ChallengeHostSerializer,\n                          ChallengeHostTeamSerializer,\n                          InviteHostToTeamSerializer,\n                          HostTeamDetailSerializer,)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_team_list(request):\n\n    if request.method == 'GET':\n        challenge_host_teams = ChallengeHostTeam.objects.filter(created_by=request.user)\n        paginator, result_page = paginated_queryset(challenge_host_teams, request)\n        serializer = HostTeamDetailSerializer(result_page, many=True)\n        response_data = serializer.data\n        return paginator.get_paginated_response(response_data)\n\n    elif request.method == 'POST':\n        serializer = ChallengeHostTeamSerializer(data=request.data,\n                                                 context={'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_201_CREATED)\n        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'PUT', 'PATCH', 'DELETE'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_team_detail(request, pk):\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        serializer = HostTeamDetailSerializer(challenge_host_team)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n\n    elif request.method in ['PUT', 'PATCH']:\n\n        if request.method == 'PATCH':\n            serializer = ChallengeHostTeamSerializer(challenge_host_team,\n                                                     data=request.data,\n                                                     context={'request': request},\n                                                     partial=True)\n        else:\n            serializer = ChallengeHostTeamSerializer(challenge_host_team,\n                                                     data=request.data,\n                                                     context={'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_200_OK)\n        else:\n            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n    elif request.method == 'DELETE':\n        challenge_host_team.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_list(request, challenge_host_team_pk):\n\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        challenge_host_status = request.query_params.get('status', None)\n        filter_condition = {\n            'team_name': challenge_host_team,\n            'user': request.user\n        }\n        if challenge_host_status:\n            challenge_host_status = challenge_host_status.split(',')\n            filter_condition.update({'status__in': challenge_host_status})\n\n        challenge_host = ChallengeHost.objects.filter(**filter_condition)\n        paginator, result_page = paginated_queryset(challenge_host, request)\n        serializer = ChallengeHostSerializer(result_page, many=True)\n        response_data = serializer.data\n        return paginator.get_paginated_response(response_data)\n\n    elif request.method == 'POST':\n        serializer = ChallengeHostSerializer(data=request.data,\n                                             context={'challenge_host_team': challenge_host_team,\n                                                      'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_201_CREATED)\n        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'PUT', 'PATCH', 'DELETE'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_detail(request, challenge_host_team_pk, pk):\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    try:\n        challenge_host = ChallengeHost.objects.get(pk=pk)\n    except ChallengeHost.DoesNotExist:\n        response_data = {'error': 'ChallengeHost does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        serializer = ChallengeHostSerializer(challenge_host)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n\n    elif request.method in ['PUT', 'PATCH']:\n        if request.method == 'PATCH':\n            serializer = ChallengeHostSerializer(challenge_host,\n                                                 data=request.data,\n                                                 context={'challenge_host_team': challenge_host_team,\n                                                          'request': request},\n                                                 partial=True)\n        else:\n            serializer = ChallengeHostSerializer(challenge_host,\n                                                 data=request.data,\n                                                 context={'challenge_host_team': challenge_host_team,\n                                                          'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_200_OK)\n        else:\n            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n    elif request.method == 'DELETE':\n        challenge_host.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef create_challenge_host_team(request):\n\n    serializer = ChallengeHostTeamSerializer(data=request.data,\n                                             context={'request': request})\n    if serializer.is_valid():\n        serializer.save()\n        response_data = serializer.data\n        challenge_host_team = serializer.instance\n        challenge_host = ChallengeHost(user=request.user,\n                                       status=ChallengeHost.SELF,\n                                       permissions=ChallengeHost.ADMIN,\n                                       team_name=challenge_host_team)\n        challenge_host.save()\n        return Response(response_data, status=status.HTTP_201_CREATED)\n    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle, ])\n@api_view(['DELETE', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication, ))\ndef remove_self_from_challenge_host_team(request, challenge_host_team_pk):\n    \"\"\"\n    A user can remove himself from the challenge host team.\n    \"\"\"\n    try:\n        ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n    try:\n        challenge_host = ChallengeHost.objects.filter(user=request.user.id, team_name__pk=challenge_host_team_pk)\n        challenge_host.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n    except:\n        response_data = {'error': 'Sorry, you do not belong to this team.'}\n        return Response(response_data, status=status.HTTP_401_UNAUTHORIZED)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef invite_host_to_team(request, pk):\n\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    serializer = InviteHostToTeamSerializer(data=request.data,\n                                            context={'challenge_host_team': challenge_host_team,\n                                                     'request': request})\n    if serializer.is_valid():\n        serializer.save()\n        response_data = {\n            'message': 'User has been added successfully to the host team'}\n        return Response(response_data, status=status.HTTP_202_ACCEPTED)\n    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n", "path": "apps/hosts/views.py"}], "after_files": [{"content": "from rest_framework import permissions, status\nfrom rest_framework.decorators import (api_view,\n                                       authentication_classes,\n                                       permission_classes,\n                                       throttle_classes,)\nfrom rest_framework.response import Response\nfrom rest_framework_expiring_authtoken.authentication import (ExpiringTokenAuthentication,)\nfrom rest_framework.throttling import UserRateThrottle\n\nfrom accounts.permissions import HasVerifiedEmail\nfrom base.utils import paginated_queryset\nfrom .models import (ChallengeHost,\n                     ChallengeHostTeam,)\nfrom .serializers import (ChallengeHostSerializer,\n                          ChallengeHostTeamSerializer,\n                          InviteHostToTeamSerializer,\n                          HostTeamDetailSerializer,)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_team_list(request):\n\n    if request.method == 'GET':\n        challenge_host_team_ids = ChallengeHost.objects.filter(user=request.user).values_list('team_name', flat=True)\n        challenge_host_teams = ChallengeHostTeam.objects.filter(id__in=challenge_host_team_ids)\n        paginator, result_page = paginated_queryset(challenge_host_teams, request)\n        serializer = HostTeamDetailSerializer(result_page, many=True)\n        response_data = serializer.data\n        return paginator.get_paginated_response(response_data)\n\n    elif request.method == 'POST':\n        serializer = ChallengeHostTeamSerializer(data=request.data,\n                                                 context={'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_201_CREATED)\n        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'PUT', 'PATCH', 'DELETE'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_team_detail(request, pk):\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        serializer = HostTeamDetailSerializer(challenge_host_team)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n\n    elif request.method in ['PUT', 'PATCH']:\n\n        if request.method == 'PATCH':\n            serializer = ChallengeHostTeamSerializer(challenge_host_team,\n                                                     data=request.data,\n                                                     context={'request': request},\n                                                     partial=True)\n        else:\n            serializer = ChallengeHostTeamSerializer(challenge_host_team,\n                                                     data=request.data,\n                                                     context={'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_200_OK)\n        else:\n            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n    elif request.method == 'DELETE':\n        challenge_host_team.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_list(request, challenge_host_team_pk):\n\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        challenge_host_status = request.query_params.get('status', None)\n        filter_condition = {\n            'team_name': challenge_host_team,\n            'user': request.user\n        }\n        if challenge_host_status:\n            challenge_host_status = challenge_host_status.split(',')\n            filter_condition.update({'status__in': challenge_host_status})\n\n        challenge_host = ChallengeHost.objects.filter(**filter_condition)\n        paginator, result_page = paginated_queryset(challenge_host, request)\n        serializer = ChallengeHostSerializer(result_page, many=True)\n        response_data = serializer.data\n        return paginator.get_paginated_response(response_data)\n\n    elif request.method == 'POST':\n        serializer = ChallengeHostSerializer(data=request.data,\n                                             context={'challenge_host_team': challenge_host_team,\n                                                      'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_201_CREATED)\n        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['GET', 'PUT', 'PATCH', 'DELETE'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef challenge_host_detail(request, challenge_host_team_pk, pk):\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    try:\n        challenge_host = ChallengeHost.objects.get(pk=pk)\n    except ChallengeHost.DoesNotExist:\n        response_data = {'error': 'ChallengeHost does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    if request.method == 'GET':\n        serializer = ChallengeHostSerializer(challenge_host)\n        response_data = serializer.data\n        return Response(response_data, status=status.HTTP_200_OK)\n\n    elif request.method in ['PUT', 'PATCH']:\n        if request.method == 'PATCH':\n            serializer = ChallengeHostSerializer(challenge_host,\n                                                 data=request.data,\n                                                 context={'challenge_host_team': challenge_host_team,\n                                                          'request': request},\n                                                 partial=True)\n        else:\n            serializer = ChallengeHostSerializer(challenge_host,\n                                                 data=request.data,\n                                                 context={'challenge_host_team': challenge_host_team,\n                                                          'request': request})\n        if serializer.is_valid():\n            serializer.save()\n            response_data = serializer.data\n            return Response(response_data, status=status.HTTP_200_OK)\n        else:\n            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n    elif request.method == 'DELETE':\n        challenge_host.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef create_challenge_host_team(request):\n\n    serializer = ChallengeHostTeamSerializer(data=request.data,\n                                             context={'request': request})\n    if serializer.is_valid():\n        serializer.save()\n        response_data = serializer.data\n        challenge_host_team = serializer.instance\n        challenge_host = ChallengeHost(user=request.user,\n                                       status=ChallengeHost.SELF,\n                                       permissions=ChallengeHost.ADMIN,\n                                       team_name=challenge_host_team)\n        challenge_host.save()\n        return Response(response_data, status=status.HTTP_201_CREATED)\n    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n\n\n@throttle_classes([UserRateThrottle, ])\n@api_view(['DELETE', ])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication, ))\ndef remove_self_from_challenge_host_team(request, challenge_host_team_pk):\n    \"\"\"\n    A user can remove himself from the challenge host team.\n    \"\"\"\n    try:\n        ChallengeHostTeam.objects.get(pk=challenge_host_team_pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n    try:\n        challenge_host = ChallengeHost.objects.filter(user=request.user.id, team_name__pk=challenge_host_team_pk)\n        challenge_host.delete()\n        return Response(status=status.HTTP_204_NO_CONTENT)\n    except:\n        response_data = {'error': 'Sorry, you do not belong to this team.'}\n        return Response(response_data, status=status.HTTP_401_UNAUTHORIZED)\n\n\n@throttle_classes([UserRateThrottle])\n@api_view(['POST'])\n@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))\n@authentication_classes((ExpiringTokenAuthentication,))\ndef invite_host_to_team(request, pk):\n\n    try:\n        challenge_host_team = ChallengeHostTeam.objects.get(pk=pk)\n    except ChallengeHostTeam.DoesNotExist:\n        response_data = {'error': 'ChallengeHostTeam does not exist'}\n        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)\n\n    serializer = InviteHostToTeamSerializer(data=request.data,\n                                            context={'challenge_host_team': challenge_host_team,\n                                                     'request': request})\n    if serializer.is_valid():\n        serializer.save()\n        response_data = {\n            'message': 'User has been added successfully to the host team'}\n        return Response(response_data, status=status.HTTP_202_ACCEPTED)\n    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)\n", "path": "apps/hosts/views.py"}]}