From ae889093807fff4c80420379400f948b817dc8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kol=C3=A1=C5=99?= Date: Sat, 11 Nov 2023 12:17:43 +0100 Subject: [PATCH] feat(memberships): added smart after-login router --- fiesta/apps/accounts/urls.py | 2 ++ fiesta/apps/accounts/views/index.py | 32 ++++++++++++++++++++--------- fiesta/fiesta/settings/auth.py | 4 +--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fiesta/apps/accounts/urls.py b/fiesta/apps/accounts/urls.py index 1073d0a4..f6436a5f 100644 --- a/fiesta/apps/accounts/urls.py +++ b/fiesta/apps/accounts/urls.py @@ -3,6 +3,7 @@ from django.urls import path from django.views.generic import RedirectView +from apps.accounts.views.index import AfterLoginRedirectView from apps.accounts.views.membership import MyMembershipsView, NewSectionMembershipFormView from apps.accounts.views.profile import MyProfileDetailView, MyProfileUpdateView, ProfileFinishFormView @@ -21,4 +22,5 @@ path("membership", MyMembershipsView.as_view(), name="membership"), # path("", TemplateView.as_view(template_name="accounts/index.html")), # path("login", LoginViewNotInSectionSpace.as_view(), name="login"), + path("after-login", AfterLoginRedirectView.as_view(), name="after-login"), ] diff --git a/fiesta/apps/accounts/views/index.py b/fiesta/apps/accounts/views/index.py index 817c43fc..d57a375c 100644 --- a/fiesta/apps/accounts/views/index.py +++ b/fiesta/apps/accounts/views/index.py @@ -1,12 +1,24 @@ from __future__ import annotations -# class BuddySystemEntrance(EnsureInSectionSpaceViewMixin, PluginConfigurationViewMixin, TemplateView): -# def get(self, request, *args, **kwargs): -# if self.request.membership.is_international: -# return HttpResponseRedirect(reverse("buddy_system:new-request")) -# -# c: BuddySystemConfiguration = self.configuration -# if c.matching_policy_instance.can_member_match: -# return HttpResponseRedirect(reverse("buddy_system:matching-requests")) -# -# return HttpResponseRedirect(reverse("buddy_system:index")) +from django.urls import reverse +from django.views.generic import RedirectView + +from apps.accounts.templatetags.memberships import section_membership_activation_url +from apps.plugins.views import PluginConfigurationViewMixin +from apps.sections.models import SectionMembership +from apps.sections.views.mixins.section_space import EnsureNotInSectionSpaceViewMixin + + +class AfterLoginRedirectView( + EnsureNotInSectionSpaceViewMixin, + PluginConfigurationViewMixin, + RedirectView, +): + def get_redirect_url(self): + active_memberships = self.request.all_memberships.filter(state=SectionMembership.State.ACTIVE) + if active_memberships.count() == 1: + m: SectionMembership = active_memberships.first() + + return section_membership_activation_url(dict(request=self.request), m) + + return reverse("accounts:membership") diff --git a/fiesta/fiesta/settings/auth.py b/fiesta/fiesta/settings/auth.py index 00dcfe77..8f9938a4 100644 --- a/fiesta/fiesta/settings/auth.py +++ b/fiesta/fiesta/settings/auth.py @@ -79,9 +79,7 @@ class AuthConfigMixin: SOCIALACCOUNT_ADAPTER = "apps.accounts.adapters.SocialAccountAdapter" # general django urls LOGIN_URL = "/auth/login" - # TODO: do redirect to special url with smart router, which redirects to section - # (if user has only one section) - LOGIN_REDIRECT_URL = "/" + LOGIN_REDIRECT_URL = "/accounts/after-login" # fixme: verify it ACCOUNT_LOGIN_ON_PASSWORD_RESET = True # False by default