From 34cf522d891b9c19a77c32fd0aa2302416fcfc94 Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Fri, 27 Mar 2026 01:00:45 +0545 Subject: [PATCH] feat: migrate legacy email verification data to django-allauth - Add data migration (0022) to transfer `UserProfile.email_verified` boolean to `account.EmailAddress` table. - Update `is_trustworthy` test cases in `test_user.py` to utilize `EmailAddress` mock data. - Clean up legacy fields in `test-user-data.json` fixture to prevent test runner crashes. - Addresses maintainer feedback for PR #2260. Refs #2258 --- .gitignore | 4 --- wger/core/api/views.py | 8 ++--- wger/core/fixtures/test-user-data.json | 24 -------------- ...022_move_email_verified_to_emailaddress.py | 31 +++++++++++++++++++ wger/core/models/profile.py | 8 ----- ...signup_message.html => email_confirm.html} | 2 +- wger/core/tests/test_user.py | 19 +++++++----- wger/urls.py | 6 +--- 8 files changed, 47 insertions(+), 55 deletions(-) create mode 100644 wger/core/migrations/0022_move_email_verified_to_emailaddress.py rename wger/core/templates/account/{email/email_confirmation_signup_message.html => email_confirm.html} (88%) diff --git a/.gitignore b/.gitignore index 713daebbb..acc5370bf 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,6 @@ settings/*_extra.py *.sqlite /CACHE -database.sqlite.orig # Sphinx documentation docs/_build/ @@ -91,6 +90,3 @@ venv-wger # macOS .DS_Store - -# feature/2258-migrate-to-django-allauth # TODO: delete this pbc260322 -/z-feature-notes-todos \ No newline at end of file diff --git a/wger/core/api/views.py b/wger/core/api/views.py index 7e83edd6c..942649e7b 100644 --- a/wger/core/api/views.py +++ b/wger/core/api/views.py @@ -136,9 +136,9 @@ class UserProfileViewSet(viewsets.ModelViewSet): request.user.email = data['email'] request.user.save() request.user.userprofile.save() - # EmailAddress.objects.add_email(request, request.user, request.user.email) + # adds new email if it is not already registered EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True) - logger.debug('adding email with verified flag and also, sends email confirmation') + logger.debug('adding new email with verified flag , send verification email') return Response(serializer.data) @@ -358,9 +358,7 @@ class UserAPIRegistrationViewSet(viewsets.ViewSet): user.userprofile.save() token = create_token(user) - # Email the user with the activation link - email_obj = EmailAddress.objects.get_for_user(request.user, request.user.email) - email_obj.send_confirmation(request) + EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True) return Response( {'message': 'api user successfully registered', 'token': token.key}, diff --git a/wger/core/fixtures/test-user-data.json b/wger/core/fixtures/test-user-data.json index cd51b2dd9..022388dd4 100644 --- a/wger/core/fixtures/test-user-data.json +++ b/wger/core/fixtures/test-user-data.json @@ -480,7 +480,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": false, "show_comments": false, "show_english_ingredients": false, "user": 1, @@ -512,7 +511,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": false, "show_comments": false, "show_english_ingredients": false, "user": 2, @@ -544,7 +542,6 @@ "model": "core.userprofile", "fields": { "is_temporary": true, - "email_verified": false, "show_comments": false, "show_english_ingredients": false, "user": 3, @@ -576,7 +573,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 4, @@ -608,7 +604,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": false, "show_comments": false, "show_english_ingredients": false, "user": 5, @@ -640,7 +635,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": false, "show_comments": false, "show_english_ingredients": false, "user": 6, @@ -672,7 +666,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 7, @@ -704,7 +697,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 8, @@ -736,7 +728,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 9, @@ -768,7 +759,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 10, @@ -800,7 +790,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 11, @@ -832,7 +821,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 12, @@ -864,7 +852,6 @@ "model": "core.userprofile", "fields": { "is_temporary": false, - "email_verified": true, "show_comments": false, "show_english_ingredients": false, "user": 13, @@ -895,7 +882,6 @@ "pk": 14, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -927,7 +913,6 @@ "pk": 15, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -959,7 +944,6 @@ "pk": 16, "model": "core.userprofile", "fields": { - "email_verified": false, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -991,7 +975,6 @@ "pk": 17, "model": "core.userprofile", "fields": { - "email_verified": false, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1023,7 +1006,6 @@ "pk": 18, "model": "core.userprofile", "fields": { - "email_verified": false, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1055,7 +1037,6 @@ "pk": 19, "model": "core.userprofile", "fields": { - "email_verified": false, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1087,7 +1068,6 @@ "pk": 20, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1119,7 +1099,6 @@ "pk": 21, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1151,7 +1130,6 @@ "pk": 22, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1183,7 +1161,6 @@ "pk": 23, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, @@ -1215,7 +1192,6 @@ "pk": 24, "model": "core.userprofile", "fields": { - "email_verified": true, "workout_reminder": 20, "height": 180, "show_english_ingredients": false, diff --git a/wger/core/migrations/0022_move_email_verified_to_emailaddress.py b/wger/core/migrations/0022_move_email_verified_to_emailaddress.py new file mode 100644 index 000000000..1de01cd70 --- /dev/null +++ b/wger/core/migrations/0022_move_email_verified_to_emailaddress.py @@ -0,0 +1,31 @@ +from django.db import migrations + +""" +Migration script to copy 'email_verified' column of UserProfile to 'verified' column of EmailAddress table +""" + +def migrate_verification_status(apps, schema_editor): + UserProfile = apps.get_model('core', 'UserProfile') + EmailAddress = apps.get_model('account', 'EmailAddress') + + for profile in UserProfile.objects.all(): + if profile.user.email: + # Create allauth table using existing userprofile fields + EmailAddress.objects.get_or_create( + user=profile.user, + email=profile.user.email, + defaults={ + 'verified': profile.email_verified, + 'primary': True + } + ) + +class Migration(migrations.Migration): + dependencies = [ + ('core', '0021_add_unit_type_to_repetitionunit'), + ('account', '0001_initial'), + ] + + operations = [ + migrations.RunPython(migrate_verification_status), + ] \ No newline at end of file diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py index e8e609583..f93fae573 100644 --- a/wger/core/models/profile.py +++ b/wger/core/models/profile.py @@ -106,14 +106,6 @@ class UserProfile(models.Model): The gym this user belongs to, if any """ - # this field still used in wger\wger\core\templates\user\preferences.html (user.preferences) - email_verified = models.OneToOneField( - EmailAddress, - editable=False, - on_delete=models.CASCADE, - ) - """Flag indicating whether the user's email has been verified""" - is_temporary = models.BooleanField(default=False, editable=False) """ Flag to mark a temporary user (demo account) diff --git a/wger/core/templates/account/email/email_confirmation_signup_message.html b/wger/core/templates/account/email_confirm.html similarity index 88% rename from wger/core/templates/account/email/email_confirmation_signup_message.html rename to wger/core/templates/account/email_confirm.html index 5349205a2..9b6bb95bd 100644 --- a/wger/core/templates/account/email/email_confirmation_signup_message.html +++ b/wger/core/templates/account/email_confirm.html @@ -8,6 +8,6 @@
{% blocktranslate %}Please click here to confirm your email{% endblocktranslate %}
{# Note: Allauth doesn't pass 'expiry' by default in the same way, we usually rely on settings #} -{% translate 'the wger Team' %}
+{% translate 'the wger Team @2026' %}