just test exceptions for a wrong value in query
This commit is contained in:
parent
79e577acd2
commit
b8ad1961d1
@ -26,6 +26,27 @@ class ProjectViewSet(ModelViewSet):
|
|||||||
queryset = Project.objects.filter(active=True)
|
queryset = Project.objects.filter(active=True)
|
||||||
|
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""
|
||||||
|
add a filter on contributor or author in querystring
|
||||||
|
"""
|
||||||
|
if self.request.GET.get('contributor'):
|
||||||
|
requested_contributor = self.request.GET.get('contributor')
|
||||||
|
try:
|
||||||
|
user = User.objects.get(username=requested_contributor)
|
||||||
|
return Project.objects.filter(contributors=user)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
return Response(f"{requested_contributor} doesn't exist",
|
||||||
|
status=status.HTTP_404_NOT_FOUND)
|
||||||
|
if self.request.GET.get('author'):
|
||||||
|
requested_author = self.request.GET.get('author')
|
||||||
|
try:
|
||||||
|
user = User.objects.get(username=requested_author)
|
||||||
|
return Project.objects.filter(author=user)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
return Response(f"{requested_author} doesn't exist",
|
||||||
|
status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def retrieve(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
check if requestor is in the project's contributor
|
check if requestor is in the project's contributor
|
||||||
@ -57,6 +78,16 @@ class ProjectViewSet(ModelViewSet):
|
|||||||
if contributor_serializer.is_valid():
|
if contributor_serializer.is_valid():
|
||||||
contributor_serializer.save()
|
contributor_serializer.save()
|
||||||
|
|
||||||
|
@action(detail=False, methods=['get'])
|
||||||
|
def test(self, request):
|
||||||
|
requested_user = self.request.GET.get('author')
|
||||||
|
try:
|
||||||
|
user = User.objects.get(username=requested_user)
|
||||||
|
return Response(UserListSerializer(user).data)
|
||||||
|
except:
|
||||||
|
return Response(f"{requested_user} isn't a valid user")
|
||||||
|
|
||||||
|
|
||||||
@action(detail=True, methods=['patch'], permission_classes=[IsContributor])
|
@action(detail=True, methods=['patch'], permission_classes=[IsContributor])
|
||||||
def contributor(self, request, pk):
|
def contributor(self, request, pk):
|
||||||
"""Add a contributor to a project
|
"""Add a contributor to a project
|
||||||
|
Loading…
x
Reference in New Issue
Block a user