From 9673ed100bbb5ac750fd422d991a5eb03438be96 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 14 Dec 2020 14:51:24 -0500 Subject: [PATCH] Check for path key using .get to avoid KeyError on collections (#603) (#607) * Fix KeyError when namespace name is not provided Issue: AAH-195 * Fix error when routing does not provide `path` key Issue: AAH-195 (cherry picked from commit d9e98f25a6d6bc6d732ed78655bba7eef1c42996) Co-authored-by: Bruno Rocha --- CHANGES/195.bugfix | 1 + galaxy_ng/app/api/ui/viewsets/collection.py | 9 +++++++-- galaxy_ng/app/api/v3/viewsets/namespace.py | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 CHANGES/195.bugfix diff --git a/CHANGES/195.bugfix b/CHANGES/195.bugfix new file mode 100644 index 0000000000..1442d43b53 --- /dev/null +++ b/CHANGES/195.bugfix @@ -0,0 +1 @@ +Fix KeyError lookup in namespace and collection viewset diff --git a/galaxy_ng/app/api/ui/viewsets/collection.py b/galaxy_ng/app/api/ui/viewsets/collection.py index 14240d0bad..034aab12ed 100644 --- a/galaxy_ng/app/api/ui/viewsets/collection.py +++ b/galaxy_ng/app/api/ui/viewsets/collection.py @@ -1,5 +1,6 @@ from django.db.models import Exists, OuterRef, Q from django.core.exceptions import ObjectDoesNotExist +from django.http import Http404 from django.shortcuts import get_object_or_404 from django_filters import filters from django_filters.rest_framework import filterset, DjangoFilterBackend, OrderingFilter @@ -50,8 +51,12 @@ class CollectionViewSet( def get_queryset(self): """Returns a CollectionVersions queryset for specified distribution.""" - distro_content = self.get_distro_content(self.kwargs["path"]) - repo_version = self.get_repository_version(self.kwargs["path"]) + path = self.kwargs.get('path') + if path is None: + raise Http404("Distribution base path is required") + + distro_content = self.get_distro_content(path) + repo_version = self.get_repository_version(path) versions = CollectionVersion.objects.filter(pk__in=distro_content).values_list( "collection_id", diff --git a/galaxy_ng/app/api/v3/viewsets/namespace.py b/galaxy_ng/app/api/v3/viewsets/namespace.py index 2dc16eef5f..8dda6da8fd 100644 --- a/galaxy_ng/app/api/v3/viewsets/namespace.py +++ b/galaxy_ng/app/api/v3/viewsets/namespace.py @@ -53,8 +53,8 @@ def get_serializer_class(self): @transaction.atomic def create(self, request, *args, **kwargs): """Override to validate for name duplication before serializer validation.""" - name = request.data['name'] - if models.Namespace.objects.filter(name=name).exists(): + name = request.data.get('name') + if name and models.Namespace.objects.filter(name=name).exists(): # Ensures error raised is 409, not 400. raise ConflictError( detail={'name': f'A namespace named {name} already exists.'}