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
This commit is contained in:
pankaj-basnet
2026-03-27 01:00:45 +05:45
parent 4f3648782b
commit 34cf522d89
8 changed files with 47 additions and 55 deletions

4
.gitignore vendored
View File

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

View File

@@ -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},

View File

@@ -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,

View File

@@ -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),
]

View File

@@ -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)

View File

@@ -8,6 +8,6 @@
<p>{% blocktranslate %}Please click <a href="{{ activate_url }}">here</a> to confirm your email{% endblocktranslate %}</p>
{# Note: Allauth doesn't pass 'expiry' by default in the same way, we usually rely on settings #}
<p><i>{% translate 'the wger Team' %}</i></p>
<p><i>{% translate 'the wger Team @2026' %}</i></p>
</body>
</html>

View File

@@ -22,6 +22,9 @@ from django.urls import (
reverse_lazy,
)
# Third Party
from allauth.account.models import EmailAddress
# wger
from wger.core.demo import create_temporary_user
from wger.core.tests.base_testcase import (
@@ -288,7 +291,7 @@ class UserTrustworthinessTestCase(WgerTestCase):
# Get a temporary user
user = create_temporary_user(self.request)
user.userprofile.is_temporary = False
user.userprofile.email_verified = True
EmailAddress.objects.create(user=user, email=user.email, verified=True)
user.date_joined = datetime.datetime.now()
# User does not pass trustworthiness check
@@ -302,7 +305,7 @@ class UserTrustworthinessTestCase(WgerTestCase):
# Get a temporary user
user = create_temporary_user(self.request)
user.userprofile.is_temporary = False
user.userprofile.email_verified = False
EmailAddress.objects.create(user=user, email=user.email, verified=False)
user.date_joined = datetime.datetime.now() - datetime.timedelta(days=30)
# User does not pass trustworthiness check
@@ -310,29 +313,29 @@ class UserTrustworthinessTestCase(WgerTestCase):
def test_is_trustworthy_old_email(self):
"""
Tests that old accounts are not considered trustworthy
Tests that old accounts with verified email are considered trustworthy
"""
# Get a temporary user
user = create_temporary_user(self.request)
user.userprofile.is_temporary = False
user.userprofile.email_verified = True
EmailAddress.objects.create(user=user, email=user.email, verified=True)
user.date_joined = datetime.datetime.now() - datetime.timedelta(days=30)
# User does not pass trustworthiness check
# User pass trustworthiness check
self.assertTrue(user.userprofile.is_trustworthy)
def test_is_trustworthy_admin(self):
"""
Tests that superusers are always trustworthy
Tests that superusers are always trustworthy even if email is unverified
"""
# Get a temporary user
user = create_temporary_user(self.request)
user.is_superuser = True
user.userprofile.is_temporary = False
user.userprofile.email_verified = False
EmailAddress.objects.create(user=user, email=user.email, verified=False)
user.date_joined = datetime.datetime.now()
# User does not pass trustworthiness check
# User pass trustworthiness check
self.assertTrue(user.userprofile.is_trustworthy)

View File

@@ -27,7 +27,6 @@ from django.contrib.sitemaps.views import (
from django.urls import path
# Third Party
# from django_email_verification import urls as email_urls ## TODO: delete this (((pbc260321)))
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularRedocView,
@@ -357,11 +356,8 @@ urlpatterns += [
'api/v2/schema/redoc',
SpectacularRedocView.as_view(url_name='schema'),
name='api-redoc',
),
# path('email/', include(email_urls)), ## TODO: delete this (((pbc260321)))
# path('accounts/', include('allauth.urls')),
),
path('email/', include('allauth.account.urls')),
# path('accounts/', include('allauth.account.urls')),
]
#