Skip to content

Commit

Permalink
be more explicit on magic link so that we can use the decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
nmenezes0 committed Aug 13, 2024
1 parent 8e1651a commit a03773b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
8 changes: 6 additions & 2 deletions consultation_analyser/consultations/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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/<uuid:token>/", sessions.MagicLinkView.as_view(), name="magic_link")
]

16 changes: 15 additions & 1 deletion consultation_analyser/consultations/views/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


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

Expand All @@ -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
16 changes: 7 additions & 9 deletions consultation_analyser/middleware.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -15,15 +11,17 @@ 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



class LoginRequiredMiddleware404(LoginRequiredMiddleware):
"""Require login, 404 if no access."""
def handle_no_permission(self, request, view_func):
return Http404()
raise Http404

0 comments on commit a03773b

Please sign in to comment.