From 274777a2e63dbd196fe2ed5a41557e3bc07407bf Mon Sep 17 00:00:00 2001 From: daveoconnor Date: Fri, 4 Oct 2024 10:23:25 -0700 Subject: [PATCH] Track user last login option and fixes (#1312) This also hides the pop up notifications after 6 seconds For review, a review and merge of #1311 on which this is built would make this easier to review. --- templates/account/login.html | 13 ++----- templates/base.html | 36 +++++++++++++++++++ templates/includes/_footer.html | 2 +- templates/partials/messages.html | 2 +- templates/socialaccount/login.html | 9 ++--- .../socialaccount/snippets/provider_list.html | 4 +-- templates/users/profile.html | 5 ++- users/constants.py | 1 + users/forms.py | 2 +- .../0013_user_indicate_last_login_method.py | 21 +++++++++++ users/models.py | 4 +++ users/signals.py | 14 ++++++++ users/tests/fixtures.py | 1 + users/tests/test_forms.py | 2 ++ 14 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 users/constants.py create mode 100644 users/migrations/0013_user_indicate_last_login_method.py diff --git a/templates/account/login.html b/templates/account/login.html index 2cf329fc..edaafb79 100644 --- a/templates/account/login.html +++ b/templates/account/login.html @@ -12,9 +12,7 @@ {% block content %} -
+
@@ -57,13 +55,9 @@

- Or Log In with Email Last Log In + Or Log In with Email Last Log In

-
diff --git a/users/constants.py b/users/constants.py new file mode 100644 index 00000000..865c3f46 --- /dev/null +++ b/users/constants.py @@ -0,0 +1 @@ +LOGIN_METHOD_SESSION_FIELD_NAME = "boost_login_method" diff --git a/users/forms.py b/users/forms.py index b4da0e69..d58f00d1 100644 --- a/users/forms.py +++ b/users/forms.py @@ -79,7 +79,7 @@ class PreferencesForm(forms.ModelForm): class UserProfileForm(forms.ModelForm): class Meta: model = User - fields = ["email", "first_name", "last_name"] + fields = ["email", "first_name", "last_name", "indicate_last_login_method"] class CustomClearableFileInput(forms.ClearableFileInput): diff --git a/users/migrations/0013_user_indicate_last_login_method.py b/users/migrations/0013_user_indicate_last_login_method.py new file mode 100644 index 00000000..86806e4d --- /dev/null +++ b/users/migrations/0013_user_indicate_last_login_method.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.15 on 2024-10-02 18:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("users", "0012_user_can_update_image"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="indicate_last_login_method", + field=models.BooleanField( + default=False, + help_text="Indicate on the login page the last login method used.", + ), + ), + ] diff --git a/users/models.py b/users/models.py index 43fe2b87..f2f885f2 100644 --- a/users/models.py +++ b/users/models.py @@ -242,6 +242,10 @@ class User(BaseUser): "a user's ability to update their own profile photo, uncheck this box." ), ) + indicate_last_login_method = models.BooleanField( + default=False, + help_text="Indicate on the login page the last login method used.", + ) def save_image_from_github(self, avatar_url): response = requests.get(avatar_url) diff --git a/users/signals.py b/users/signals.py index f56d9c67..db98cbd1 100644 --- a/users/signals.py +++ b/users/signals.py @@ -1,8 +1,11 @@ +from allauth.account.signals import user_logged_in from django.dispatch import receiver from django.db.models.signals import post_save from allauth.socialaccount.models import SocialAccount +from users.constants import LOGIN_METHOD_SESSION_FIELD_NAME + GITHUB = "github" @@ -26,3 +29,14 @@ def import_social_profile_data(sender, instance, created, **kwargs): if avatar_url: instance.user.save_image_from_github(avatar_url) + + +@receiver(user_logged_in) +def user_logged_in_handler(request, user, **kwargs): + # We trigger this here as well as on the profile update in case there are two users + # on one machine, we need to reflag for the cookie update + try: + method = request.session["account_authentication_methods"][0].get("provider") + except (KeyError, IndexError): + method = None + request.session[LOGIN_METHOD_SESSION_FIELD_NAME] = method or "email" diff --git a/users/tests/fixtures.py b/users/tests/fixtures.py index b50519bd..84c4c4c4 100644 --- a/users/tests/fixtures.py +++ b/users/tests/fixtures.py @@ -15,6 +15,7 @@ def user(db): email="user@example.com", first_name="Regular", last_name="User", + indicate_last_login_method=False, last_login=timezone.now(), image=None, ) diff --git a/users/tests/test_forms.py b/users/tests/test_forms.py index c53e57ff..dd7b68c0 100644 --- a/users/tests/test_forms.py +++ b/users/tests/test_forms.py @@ -146,11 +146,13 @@ def test_user_profile_form(user): "first_name", "last_name", "email", + "indicate_last_login_method", } assert form.initial == { "first_name": user.first_name, "last_name": user.last_name, "email": user.email, + "indicate_last_login_method": user.indicate_last_login_method, } form = UserProfileForm(instance=user, data={"email": "test@example.com"}) assert form.is_valid()