Skip to content

Commit

Permalink
Implementa redirects nas views
Browse files Browse the repository at this point in the history
  • Loading branch information
berinhard committed Oct 16, 2020
1 parent 15e39cf commit 44fc411
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
18 changes: 15 additions & 3 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import Sequence

from django.shortcuts import get_object_or_404
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
from rest_framework import viewsets
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
Expand All @@ -9,7 +10,7 @@
from api.versioning import check_api_version_redirect
from core.filters import parse_querystring
from core.forms import get_table_dynamic_form
from core.models import Dataset, Table
from core.models import Dataset, Table, DataUrlRedirect
from core.templatetags.utils import obfuscate

from . import paginators
Expand All @@ -21,7 +22,14 @@ class DatasetViewSet(viewsets.ModelViewSet):

@check_api_version_redirect
def retrieve(self, request, slug):
obj = get_object_or_404(self.get_queryset(), slug=slug)
try:
obj = self.get_queryset().get(slug=slug)
except Dataset.DoesNotExist:
redirect_url = DataUrlRedirect.redirect_from(request)
if redirect_url:
return redirect(redirect_url)
raise Http404

serializer = DatasetDetailSerializer(obj, context=self.get_serializer_context(),)
return Response(serializer.data)

Expand Down Expand Up @@ -92,6 +100,10 @@ def handle_exception(self, exc):
if isinstance(exc, InvalidFiltersException):
return Response(exc.errors_list, status=400)
else:
if isinstance(exc, Http404):
redirect_url = DataUrlRedirect.redirect_from(self.request)
if redirect_url:
return redirect(redirect_url)
return super().handle_exception(exc)

@check_api_version_redirect
Expand Down
3 changes: 2 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ def redirect_map(self):
return {reverse(n, args=[self.dataset_prev]): reverse(n, args=[self.dataset_dest]) for n in dataset_url_names}

@classmethod
def redirect_from(cls, path):
def redirect_from(cls, request):
path = request.path
redirects = {}

for data_url_redirect in cls.objects.all().iterator():
Expand Down
2 changes: 2 additions & 0 deletions core/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import patch

from django.conf import settings
from django.core.management import call_command
from django.test import TestCase, override_settings
from django.urls import reverse
Expand Down Expand Up @@ -129,6 +130,7 @@ class DataRedirectsTests(TestCase):
client_class = TrafficControlClient

def setUp(self):
self.client.force_login(baker.make(settings.AUTH_USER_MODEL))
self.dataset_redirects = [
baker.make(DataUrlRedirect, dataset_prev="gastos_diretos", dataset_dest="govbr"),
baker.make(DataUrlRedirect, dataset_prev="bens_candidatos", dataset_dest="bem_declarado"),
Expand Down
19 changes: 16 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from django.core.paginator import Paginator
from django.db.models import Q
from django.http import StreamingHttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.shortcuts import redirect, render
from django.urls import reverse

from core.filters import parse_querystring
from core.forms import ContactForm, DatasetSearchForm, get_table_dynamic_form
from core.middlewares import disable_non_logged_user_cache
from core.models import Dataset, Table
from core.models import Dataset, Table, DataUrlRedirect
from core.templatetags.utils import obfuscate
from core.util import cached_http_get_json
from data_activities_log.activites import recent_activities
Expand Down Expand Up @@ -109,6 +109,10 @@ def dataset_detail(request, slug, tablename=""):
try:
dataset = Dataset.objects.get(slug=slug)
except Dataset.DoesNotExist:
redirect_url = DataUrlRedirect.redirect_from(request)
if redirect_url:
return redirect(redirect_url)

context = {"message": "Dataset does not exist"}
return render(request, "404.html", context, status=404)

Expand Down Expand Up @@ -233,7 +237,16 @@ def contributors(request):


def dataset_files_detail(request, slug):
dataset = get_object_or_404(Dataset, slug=slug)
try:
dataset = Dataset.objects.get(slug=slug)
except Dataset.DoesNotExist:
redirect_url = DataUrlRedirect.redirect_from(request)
if redirect_url:
return redirect(redirect_url)

context = {"message": "Dataset does not exist"}
return render(request, "404.html", context, status=404)

try:
all_files = dataset.all_files
except ObjectDoesNotExist:
Expand Down

0 comments on commit 44fc411

Please sign in to comment.