Merge pull request #185 from taigaio/bug/1736/get_object_or_404_return_500

tg-1736 #closed -> 404 instead 500 when call api/v1/projects/slug instead api/v1/project/id
remotes/origin/enhancement/email-actions
Jesús Espino 2014-12-03 11:57:36 +01:00
commit 6d948b60c0
4 changed files with 47 additions and 13 deletions

View File

@ -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.

View File

@ -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):
"""

32
taiga/base/api/utils.py Normal file
View File

@ -0,0 +1,32 @@
# Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
# 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 <http://www.gnu.org/licenses/>.
# 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

View File

@ -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")