Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

integrate allauth add_email and verification into user preferences (#2258)

pankaj-basnetbc8740e9b7b8
diff --git a/.gitignore b/.gitignore
index 6004fb395..713daebbb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -50,6 +50,8 @@ settings/*_extra.py
*.sqlite
/CACHE
+database.sqlite.orig
+
# Sphinx documentation
docs/_build/
@@ -90,5 +92,5 @@ venv-wger
# macOS
.DS_Store
-# feature/2258-migrate-to-django-allauth
+# feature/2258-migrate-to-django-allauth # TODO: delete this pbc260322
/z-feature-notes-todos
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index 13fbf3a50..f41623f57 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -32,6 +32,7 @@ dependencies = [
"celery[redis]~=5.6.0",
"crispy-bootstrap5~=2025.6",
"django-activity-stream~=2.0.0",
+ "django-allauth~=65.15.0",
"django-axes[ipware]~=8.3.1",
"django-bootstrap-breadcrumbs2==1.0.0",
"django-cors-headers~=4.9.0",
diff --git a/settings/settings_global.py b/settings/settings_global.py
index 375f895f3..3cb4b6cc0 100644
--- a/settings/settings_global.py
+++ b/settings/settings_global.py
@@ -118,6 +118,7 @@ INSTALLED_APPS = [
# Prometheus
'django_prometheus',
+ # Django-allauth
'allauth',
'allauth.account',
]
@@ -166,10 +167,7 @@ AUTHENTICATION_BACKENDS = (
'axes.backends.AxesStandaloneBackend', # should be the first one in the list
'wger.core.backends.AuthProxyUserBackend',
'django.contrib.auth.backends.ModelBackend',
- # `allauth` specific authentication methods, such as login by email (Docs)
- 'allauth.account.auth_backends.AuthenticationBackend',
'wger.utils.helpers.EmailAuthBackend',
-
)
TEMPLATES = [
@@ -215,7 +213,7 @@ STATICFILES_DIRS = (('node', os.path.join(BASE_DIR, '..', 'node_modules')),)
# Email
#
EMAIL_SUBJECT_PREFIX = '[wger] '
-# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
+EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
#
# Login
@@ -521,23 +519,6 @@ EXPOSE_PROMETHEUS_METRICS = False
PROMETHEUS_URL_PATH = 'super-secret-path'
-#
-# Django email verification
-#
-# def email_verified_callback(user): # TODO: delete this function (((pbc260321)))
-# user.userprofile.email_verified = True
-# user.userprofile.save()
-
-
-# EMAIL_MAIL_CALLBACK = email_verified_callback # TODO: delete this (((pbc260321)))
-EMAIL_FROM_ADDRESS = WGER_SETTINGS['EMAIL_FROM']
-EMAIL_MAIL_SUBJECT = 'Confirm your email'
-EMAIL_MAIL_HTML = 'email_verification/email_body_html.tpl'
-EMAIL_MAIL_PLAIN = 'email_verification/email_body_txt.tpl'
-EMAIL_MAIL_TOKEN_LIFE = 60 * 60
-EMAIL_MAIL_PAGE_TEMPLATE = 'email_verification/confirm_template.html'
-EMAIL_PAGE_DOMAIN = 'http://localhost:8000/'
-
#
# Django-activity stream
#
diff --git a/wger/config/models/gym_config.py b/wger/config/models/gym_config.py
index 342dd5a9e..e029a1f03 100644
--- a/wger/config/models/gym_config.py
+++ b/wger/config/models/gym_config.py
@@ -87,5 +87,3 @@ class GymConfig(models.Model):
return super(GymConfig, self).save(*args, **kwargs)
-# TODO
-# D:\src\PY\WGER-2026-PULL-\DJANGO-\CODE-\wger\wger\config\models\gym_config.py
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.py
index 1fff63ff3..b6d40b3c0 100644
--- a/wger/core/api/serializers.py
+++ b/wger/core/api/serializers.py
@@ -93,7 +93,7 @@ class UserprofileSerializer(serializers.ModelSerializer):
Check Allauth's EmailAddress table for the verification status
"""
try:
- email_obj = EmailAddress.objects.get(
+ email_obj = EmailAddress.objects.get_for_user(
user=obj.user,
email=obj.user.email
)
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index cbdea04cb..7e83edd6c 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -25,7 +25,7 @@ from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
# Third Party
-# from django_email_verification import send_email ## TODO: delete this (((pbc260321)))
+from allauth.account.models import EmailAddress
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import (
OpenApiParameter,
@@ -135,9 +135,10 @@ class UserProfileViewSet(viewsets.ModelViewSet):
if data.get('email') and request.user.email != data['email']:
request.user.email = data['email']
request.user.save()
- request.user.userprofile.email_verified = False
request.user.userprofile.save()
- logger.debug('resetting verified flag')
+ # EmailAddress.objects.add_email(request, request.user, request.user.email)
+ EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True)
+ logger.debug('adding email with verified flag and also, sends email confirmation')
return Response(serializer.data)
@@ -156,11 +157,11 @@ class UserProfileViewSet(viewsets.ModelViewSet):
Return the username
"""
- profile = request.user.userprofile
- if profile.email_verified:
+ email_obj = EmailAddress.objects.get_for_user(request.user, request.user.email)
+ if email_obj.verified:
return Response({'status': 'verified', 'message': 'This email is already verified'})
-
- # send_email(request.user) ## TODO: delete this (((pbc260321)))
+
+ email_obj.send_confirmation(request)
return Response(
{'status': 'sent', 'message': f'A verification email was sent to {request.user.email}'}
)
@@ -358,7 +359,8 @@ class UserAPIRegistrationViewSet(viewsets.ViewSet):
token = create_token(user)
# Email the user with the activation link
- # send_email(user) ## TODO: delete this (((pbc260321)))
+ email_obj = EmailAddress.objects.get_for_user(request.user, request.user.email)
+ email_obj.send_confirmation(request)
return Response(
{'message': 'api user successfully registered', 'token': token.key},
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index f70fd1142..67bb94593 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -103,9 +103,6 @@ class UserProfile(models.Model):
The gym this user belongs to, if any
"""
- email_verified = models.BooleanField(default=False)
- """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)
@@ -402,7 +399,8 @@ by the US Department of Agriculture. It is extremely complete, with around
days_since_joined = datetime.date.today() - self.user.date_joined.date()
minimum_account_age = settings.WGER_SETTINGS['MIN_ACCOUNT_AGE_TO_TRUST']
- return days_since_joined.days > minimum_account_age and self.email_verified
+ return days_since_joined.days > minimum_account_age
+ # return days_since_joined.days > minimum_account_age and self.email_verified
@property
def weight(self):
@@ -558,6 +556,3 @@ by the US Department of Agriculture. It is extremely complete, with around
Returns the object that has owner information
"""
return self
-
-# TODO
-# D:\src\PY\WGER-2026-PULL-\DJANGO-\CODE-\wger\wger\core\models\profile.py
\ No newline at end of file
diff --git a/wger/core/templates/email_verification/confirm_template.html b/wger/core/templates/email_verification/confirm_template.html
deleted file mode 100644
index 4fb7ff35b..000000000
--- a/wger/core/templates/email_verification/confirm_template.html
+++ /dev/null
@@ -1,24 +0,0 @@
-{% extends "base.html" %}
-{% load i18n %}
-
-{% block title %}{% translate "Email verification" %}{% endblock %}
-
-{# #}
-{# Header #}
-{# #}
-{% block header %}
-{% endblock %}
-
-
-{# #}
-{# Content #}
-{# #}
-{% block content %}
-{% if success %}
- {% blocktranslate trimmed with username=user.username %}
- {{ username }}, your email was verified
- {% endblocktranslate %}
-{% else %}
- {% translate "Error, invalid verification token" %}
-{% endif %}
-{% endblock %}
diff --git a/wger/core/templates/email_verification/email_body_html.tpl b/wger/core/templates/email_verification/email_body_html.tpl
deleted file mode 100644
index 9e6e2a036..000000000
--- a/wger/core/templates/email_verification/email_body_html.tpl
+++ /dev/null
@@ -1,16 +0,0 @@
-{% load i18n %}
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta charset="UTF-8">
- <title>{% translate 'Email Confirmation' %}</title>
-</head>
-<body>
-<p>{% blocktranslate with username=user.username %}You are almost there, {{ username }}!{% endblocktranslate %}</p>
-<br>
-<p>{% blocktranslate %}Please click <a href="{{ link }}">here</a> to confirm your email{% endblocktranslate %}</p>
-<p>{% blocktranslate with time=expiry|time:"TIME_FORMAT" %}The token expires on {{ time }}{% endblocktranslate %}</p>
-
-<p><i>{% translate 'the wger Team' %}</i></p>
-</body>
-</html>
diff --git a/wger/core/templates/email_verification/email_body_txt.tpl b/wger/core/templates/email_verification/email_body_txt.tpl
deleted file mode 100644
index a1c7b6085..000000000
--- a/wger/core/templates/email_verification/email_body_txt.tpl
+++ /dev/null
@@ -1,8 +0,0 @@
-{% load i18n %}
-
-{% blocktranslate with username=user.username %}You are almost there, {{ username }}!{% endblocktranslate %}
-{% blocktranslate %}Please click the following link to confirm your email: {{ link }}{% endblocktranslate %}
-{% blocktranslate with time=expiry|time:"TIME_FORMAT" %}The token expires on {{ time }}{% endblocktranslate %}
-
-
-{% translate 'the wger Team' %}
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index bb3591354..38a2120b4 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -74,7 +74,7 @@ from crispy_forms.layout import (
Row,
Submit,
)
-# from django_email_verification import send_email # TODO: delete this function (((pbc260321)))
+from allauth.account.models import EmailAddress
from rest_framework.authtoken.models import Token
# wger
@@ -275,7 +275,7 @@ def registration(request):
django_login(request, user)
# Email the user with the activation link
- # send_email(user) # TODO: delete this function (((pbc260321)))
+ EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True)
# Redirect to the dashboard
messages.success(request, _('You were successfully registered'))
@@ -327,8 +327,8 @@ def preferences(request):
if email_form.is_valid() and redirect:
# If the user changes the email, it is no longer verified
if user_email != email_form.instance.email:
- logger.debug('resetting verified flag')
- request.user.userprofile.email_verified = False
+ logger.debug('adding email with verified flag and also, sends email confirmation')
+ EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True)
request.user.userprofile.save()
# Save as normal
@@ -657,8 +657,9 @@ class WgerPasswordResetConfirmView(PasswordResetConfirmView):
@login_required
def confirm_email(request):
- if not request.user.userprofile.email_verified:
- # send_email(request.user) # TODO: delete this function (((pbc260321)))
+ email_obj = EmailAddress.objects.get_for_user(request.user, request.user.email)
+ if not email_obj.verified:
+ email_obj.send_confirmation(request)
messages.success(
request, _('A verification email was sent to %(email)s') % {'email': request.user.email}
)
diff --git a/wger/urls.py b/wger/urls.py
index 2cbda8558..4edbcd3ea 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -358,8 +358,10 @@ urlpatterns += [
SpectacularRedocView.as_view(url_name='schema'),
name='api-redoc',
),
- # path('email/', include(email_urls)), ## TODO: delete this (((pbc260321)))
- path('accounts/', include('allauth.account.urls')),
+ # 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')),
]
#