Skip to content

Commit

Permalink
Check for path key using .get to avoid KeyError on collections (#603) (
Browse files Browse the repository at this point in the history
…#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 d9e98f2)

Co-authored-by: Bruno Rocha <[email protected]>
  • Loading branch information
patchback[bot] and rochacbruno authored Dec 14, 2020
1 parent afabf1b commit 9673ed1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/195.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix KeyError lookup in namespace and collection viewset
9 changes: 7 additions & 2 deletions galaxy_ng/app/api/ui/viewsets/collection.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions galaxy_ng/app/api/v3/viewsets/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'}
Expand Down

0 comments on commit 9673ed1

Please sign in to comment.