diff --git a/consultation_analyser/consultations/urls.py b/consultation_analyser/consultations/urls.py index f2880096..a16073ac 100644 --- a/consultation_analyser/consultations/urls.py +++ b/consultation_analyser/consultations/urls.py @@ -1,8 +1,9 @@ -from django.urls import include, path +from django.urls import path from magic_link import urls as magic_link_urls from .views import consultations, pages, questions, responses, root, schema, sessions + urlpatterns = [ # public urls path("", root.root, name="root"), @@ -44,5 +45,8 @@ # authentication path("sign-in/", sessions.new, name="sign_in"), path("sign-out/", sessions.destroy, name="sign_out"), - path("magic-link/", include(magic_link_urls), name="magic_link"), + # path("magic-link/", include(magic_link_urls), name="magic_link"), + # path("magic-link/", sessions.MagicLinkView.as_view(), name="magic_link2"), + path("magic-link//", sessions.MagicLinkView.as_view(), name="magic_link") ] + diff --git a/consultation_analyser/consultations/views/sessions.py b/consultation_analyser/consultations/views/sessions.py index de8fe241..47786ba1 100644 --- a/consultation_analyser/consultations/views/sessions.py +++ b/consultation_analyser/consultations/views/sessions.py @@ -4,6 +4,7 @@ from django.contrib.auth import logout from django.http import HttpRequest from django.shortcuts import redirect, render +import magic_link.views from magic_link.models import MagicLink from django.contrib.auth.decorators import login_not_required @@ -13,18 +14,22 @@ from consultation_analyser.hosting_environment import HostingEnvironment -@login_not_required def send_magic_link_if_email_exists(request: HttpRequest, email: str) -> None: + print("send magic link") try: user = User.objects.get(email=email) + print(f"user: {user}") link = MagicLink.objects.create(user=user, redirect_to="/") + print(f"link: {link}") magic_link = request.build_absolute_uri(link.get_absolute_url()) + print(f"magic_link: {magic_link}") if HostingEnvironment.is_local(): logger = logging.getLogger("django.server") logger.info(f"##################### Sending magic link to {email}: {magic_link}") else: send_magic_link_email(email, magic_link) except User.DoesNotExist: + print("no such user") pass @@ -37,8 +42,11 @@ def new(request: HttpRequest): form = NewSessionForm() else: form = NewSessionForm(request.POST) + print("form.is_valid") + print(form.is_valid()) if form.is_valid(): email = form.cleaned_data["email"] + print(email) send_magic_link_if_email_exists(request, email) return render(request, "magic_link/link_sent.html") @@ -50,3 +58,9 @@ def destroy(request: HttpRequest): logout(request) messages.success(request, "You have signed out") return redirect("/") + + + +@login_not_required +class MagicLinkView(magic_link.views.MagicLinkView): + pass diff --git a/consultation_analyser/middleware.py b/consultation_analyser/middleware.py index 64a75317..9e5d33d5 100644 --- a/consultation_analyser/middleware.py +++ b/consultation_analyser/middleware.py @@ -1,9 +1,5 @@ -from urllib.parse import urlparse - from django.http import HttpResponseNotFound, Http404 from django.contrib.auth.middleware import LoginRequiredMiddleware -from django.shortcuts import resolve_url -from django.contrib.auth.views import redirect_to_login class SupportAppStaffRequiredMiddleware: @@ -15,10 +11,12 @@ def __init__(self, get_response): def __call__(self, request): response = self.get_response(request) if request.path.startswith("/support/"): - if not request.user.is_authenticated: - return HttpResponseNotFound() - elif not request.user.is_staff: - return HttpResponseNotFound() + if not request.user.is_staff: + raise Http404 + # if not request.user.is_authenticated: + # return HttpResponseNotFound() + # elif not request.user.is_staff: + # return HttpResponseNotFound() return response @@ -26,4 +24,4 @@ def __call__(self, request): class LoginRequiredMiddleware404(LoginRequiredMiddleware): """Require login, 404 if no access.""" def handle_no_permission(self, request, view_func): - return Http404() + raise Http404