From 0882df88697a94b0a9095122e5c5d56f878e7de6 Mon Sep 17 00:00:00 2001 From: sabzo Date: Thu, 29 Sep 2022 15:51:56 -0400 Subject: [PATCH 1/6] initial work to correct bad requests --- perma_web/api/urls.py | 10 ++-------- perma_web/api/views.py | 9 ++++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/perma_web/api/urls.py b/perma_web/api/urls.py index 2c8945b73..6fd7ac4f8 100644 --- a/perma_web/api/urls.py +++ b/perma_web/api/urls.py @@ -8,13 +8,6 @@ from . import views - -# helpers -class RedirectWithRootDomain(RedirectView): - def get_redirect_url(self, *args, **kwargs): - url = super(RedirectWithRootDomain, self).get_redirect_url(*args, **kwargs) - return self.request.scheme + '://' + settings.HOST + url - # list views that should appear in the HTML version of the API root root_view = APIRootView.as_view(api_root_dict={ 'folders': 'folders', @@ -91,7 +84,8 @@ def get_redirect_url(self, *args, **kwargs): ])), # redirect plain api.perma.cc/ and perma.cc/api/ to docs: - url(r'^$', RedirectWithRootDomain.as_view(url='/docs/developer')) + # url(r'^$', RedirectWithRootDomain.as_view(url='/docs/developer')) + url(r'^$', views.DeveloperDocsView.as_view()) ] ### error handlers ### diff --git a/perma_web/api/views.py b/perma_web/api/views.py index 23ae1a54d..47c99de4e 100644 --- a/perma_web/api/views.py +++ b/perma_web/api/views.py @@ -157,6 +157,14 @@ def get(self, request, pk, format=None): return self.simple_get(request, pk) +### DEVELOPER DOCS VIEWS ### +class DeveloperDocsView(BaseView): + def get(self, request, format=None): + """ Redirect to Developer Docs. """ + # for testing if this view gets called for a '/' request + print('ok') + return Response(status=status.HTTP_204_NO_CONTENT) + ### FOLDER VIEWS ### # /folders @@ -504,7 +512,6 @@ def post(self, request, format=None): run_next_capture.delay() return Response(serializer.data, status=status.HTTP_201_CREATED) - raise_invalid_capture_job(capture_job, serializer.errors) From e5f4d0720a1bbd58e04d692764d0f26f8d2f99be Mon Sep 17 00:00:00 2001 From: sabzo Date: Thu, 29 Sep 2022 22:44:43 -0400 Subject: [PATCH 2/6] change BaseView to APIView for / get request --- perma_web/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perma_web/api/views.py b/perma_web/api/views.py index 47c99de4e..4c4b17828 100644 --- a/perma_web/api/views.py +++ b/perma_web/api/views.py @@ -158,7 +158,7 @@ def get(self, request, pk, format=None): ### DEVELOPER DOCS VIEWS ### -class DeveloperDocsView(BaseView): +class DeveloperDocsView(APIView): def get(self, request, format=None): """ Redirect to Developer Docs. """ # for testing if this view gets called for a '/' request From 37dd0eeb4f37b76599aae67c85f76e0521ed2173 Mon Sep 17 00:00:00 2001 From: sabzo Date: Thu, 29 Sep 2022 23:43:49 -0400 Subject: [PATCH 3/6] redirect on GET request, and return 405 on other http methods --- perma_web/api/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/perma_web/api/views.py b/perma_web/api/views.py index 4c4b17828..6e92b60fe 100644 --- a/perma_web/api/views.py +++ b/perma_web/api/views.py @@ -22,6 +22,7 @@ url_is_invalid_unicode from .serializers import FolderSerializer, CaptureJobSerializer, LinkSerializer, AuthenticatedLinkSerializer, \ LinkUserSerializer, OrganizationSerializer, LinkBatchSerializer, DetailedLinkBatchSerializer +from django.conf import settings ### BASE VIEW ### @@ -162,8 +163,7 @@ class DeveloperDocsView(APIView): def get(self, request, format=None): """ Redirect to Developer Docs. """ # for testing if this view gets called for a '/' request - print('ok') - return Response(status=status.HTTP_204_NO_CONTENT) + return HttpResponseRedirect(redirect_to=self.request.scheme + '://' + settings.HOST + '/docs/developer') ### FOLDER VIEWS ### From fab8ea27e219a2001673395d5cc9b9660290cc1f Mon Sep 17 00:00:00 2001 From: sabzo Date: Thu, 29 Sep 2022 23:46:42 -0400 Subject: [PATCH 4/6] remove unused RedirectView --- perma_web/api/urls.py | 1 - 1 file changed, 1 deletion(-) diff --git a/perma_web/api/urls.py b/perma_web/api/urls.py index 6fd7ac4f8..68033a3a0 100644 --- a/perma_web/api/urls.py +++ b/perma_web/api/urls.py @@ -1,7 +1,6 @@ from django.conf import settings from django.conf.urls import url, include from django.http import HttpResponse -from django.views.generic import RedirectView from rest_framework.routers import APIRootView from perma.urls import guid_pattern From 9602a0ed28993dc2de13ee7617bc157bea0d0508 Mon Sep 17 00:00:00 2001 From: Sabelo Mhlambi Date: Thu, 6 Oct 2022 14:35:47 -0500 Subject: [PATCH 5/6] remove unnecessary commas --- perma_web/api/urls.py | 1 - perma_web/api/views.py | 1 - 2 files changed, 2 deletions(-) diff --git a/perma_web/api/urls.py b/perma_web/api/urls.py index 68033a3a0..a610667a5 100644 --- a/perma_web/api/urls.py +++ b/perma_web/api/urls.py @@ -83,7 +83,6 @@ ])), # redirect plain api.perma.cc/ and perma.cc/api/ to docs: - # url(r'^$', RedirectWithRootDomain.as_view(url='/docs/developer')) url(r'^$', views.DeveloperDocsView.as_view()) ] diff --git a/perma_web/api/views.py b/perma_web/api/views.py index 6e92b60fe..d5d71906e 100644 --- a/perma_web/api/views.py +++ b/perma_web/api/views.py @@ -162,7 +162,6 @@ def get(self, request, pk, format=None): class DeveloperDocsView(APIView): def get(self, request, format=None): """ Redirect to Developer Docs. """ - # for testing if this view gets called for a '/' request return HttpResponseRedirect(redirect_to=self.request.scheme + '://' + settings.HOST + '/docs/developer') ### FOLDER VIEWS ### From 376bb8d5d08f880acdd9cf86e9a77741149d25f3 Mon Sep 17 00:00:00 2001 From: Sabelo Mhlambi Date: Tue, 11 Oct 2022 06:10:13 -0500 Subject: [PATCH 6/6] redirect to generated developer docs url than hard coded developer docs url --- perma_web/api/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/perma_web/api/views.py b/perma_web/api/views.py index d5d71906e..87f3d6a6b 100644 --- a/perma_web/api/views.py +++ b/perma_web/api/views.py @@ -23,7 +23,7 @@ from .serializers import FolderSerializer, CaptureJobSerializer, LinkSerializer, AuthenticatedLinkSerializer, \ LinkUserSerializer, OrganizationSerializer, LinkBatchSerializer, DetailedLinkBatchSerializer from django.conf import settings - +from django.urls import reverse ### BASE VIEW ### @@ -161,8 +161,10 @@ def get(self, request, pk, format=None): ### DEVELOPER DOCS VIEWS ### class DeveloperDocsView(APIView): def get(self, request, format=None): - """ Redirect to Developer Docs. """ - return HttpResponseRedirect(redirect_to=self.request.scheme + '://' + settings.HOST + '/docs/developer') + """ reverse to Developer Docs to fetch correct url (view) named as 'dev_docs' """ + absolute_url_to_redirect_to = f"{ self.request.scheme }://{ settings.HOST }{ reverse('dev_docs', urlconf='perma.urls') }" + """ Redirect to Dev Docs """ + return HttpResponseRedirect(absolute_url_to_redirect_to) ### FOLDER VIEWS ###