From 15ced7c44f6a6c2ac569d89025c6aa24fff086fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 3 Dec 2014 10:59:11 +0100 Subject: [PATCH] tg-1736 #closed -> 404 instead 500 when call api/v1/projects/slug instead api/v1/project/id --- taiga/base/api/generics.py | 13 +----------- taiga/base/api/mixins.py | 3 ++- taiga/base/api/utils.py | 32 ++++++++++++++++++++++++++++++ tests/integration/test_projects.py | 12 +++++++++++ 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 taiga/base/api/utils.py diff --git a/taiga/base/api/generics.py b/taiga/base/api/generics.py index 6cee8f18..752ae201 100644 --- a/taiga/base/api/generics.py +++ b/taiga/base/api/generics.py @@ -22,7 +22,6 @@ import warnings from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.core.paginator import Paginator, InvalidPage from django.http import Http404 -from django.shortcuts import get_object_or_404 as _get_object_or_404 from django.utils.translation import ugettext as _ from rest_framework import exceptions @@ -31,6 +30,7 @@ from rest_framework.settings import api_settings from . import views from . import mixins +from .utils import get_object_or_404 def strict_positive_int(integer_string, cutoff=None): @@ -45,17 +45,6 @@ def strict_positive_int(integer_string, cutoff=None): return ret -def get_object_or_404(queryset, *filter_args, **filter_kwargs): - """ - Same as Django's standard shortcut, but make sure to raise 404 - if the filter_kwargs don't match the required types. - """ - try: - return _get_object_or_404(queryset, *filter_args, **filter_kwargs) - except (TypeError, ValueError): - raise Http404 - - class GenericAPIView(views.APIView): """ Base class for all other generic views. diff --git a/taiga/base/api/mixins.py b/taiga/base/api/mixins.py index 5e901648..bca89e63 100644 --- a/taiga/base/api/mixins.py +++ b/taiga/base/api/mixins.py @@ -20,7 +20,6 @@ import warnings from django.core.exceptions import ValidationError -from django.shortcuts import get_object_or_404 from django.http import Http404 from django.db import transaction as tx @@ -29,6 +28,8 @@ from rest_framework.response import Response from rest_framework.request import clone_request from rest_framework.settings import api_settings +from .utils import get_object_or_404 + def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None): """ diff --git a/taiga/base/api/utils.py b/taiga/base/api/utils.py new file mode 100644 index 00000000..433e668a --- /dev/null +++ b/taiga/base/api/utils.py @@ -0,0 +1,32 @@ +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino +# Copyright (C) 2014 David Barragán +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# This code is partially taken from django-rest-framework: +# Copyright (c) 2011-2014, Tom Christie + +from django.http import Http404 +from django.shortcuts import get_object_or_404 as _get_object_or_404 + + +def get_object_or_404(queryset, *filter_args, **filter_kwargs): + """ + Same as Django's standard shortcut, but make sure to raise 404 + if the filter_kwargs don't match the required types. + """ + try: + return _get_object_or_404(queryset, *filter_args, **filter_kwargs) + except (TypeError, ValueError): + raise Http404 diff --git a/tests/integration/test_projects.py b/tests/integration/test_projects.py index ad4ad86e..941b3a47 100644 --- a/tests/integration/test_projects.py +++ b/tests/integration/test_projects.py @@ -9,6 +9,18 @@ import pytest pytestmark = pytest.mark.django_db +def test_get_project_by_slug(client): + project = f.create_project() + url = reverse("projects-detail", kwargs={"pk": project.slug}) + + response = client.json.get(url) + assert response.status_code == 404 + + client.login(project.owner) + response = client.json.get(url) + assert response.status_code == 404 + + def test_create_project(client): user = f.create_user() url = reverse("projects-list")