Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add account adapter

Roland Geider4873f939c069
diff --git a/settings/settings_global.py b/settings/settings_global.py
index 3cb4b6cc0..cddb2a6e6 100644
--- a/settings/settings_global.py
+++ b/settings/settings_global.py
@@ -215,6 +215,13 @@ STATICFILES_DIRS = (('node', os.path.join(BASE_DIR, '..', 'node_modules')),)
EMAIL_SUBJECT_PREFIX = '[wger] '
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
+#
+# django-allauth
+#
+ACCOUNT_CONFIRM_EMAIL_ON_GET = True
+EMAIL_CONFIRMATION_EXPIRE_DAYS = 2
+ACCOUNT_ADAPTER = 'wger.core.account_adapter.WgerAccountAdapter'
+
#
# Login
#
diff --git a/wger/config/models/gym_config.py b/wger/config/models/gym_config.py
index e029a1f03..41623f144 100644
--- a/wger/config/models/gym_config.py
+++ b/wger/config/models/gym_config.py
@@ -86,4 +86,3 @@ class GymConfig(models.Model):
logger.debug(f'Creating GymUserConfig for user {user.username}')
return super(GymConfig, self).save(*args, **kwargs)
-
diff --git a/wger/core/account_adapter.py b/wger/core/account_adapter.py
new file mode 100644
index 000000000..5ba9b7c39
--- /dev/null
+++ b/wger/core/account_adapter.py
@@ -0,0 +1,42 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Standard Library
+from datetime import timedelta
+
+# Django
+from django.utils import timezone
+
+# Third Party
+from allauth.account import app_settings
+from allauth.account.adapter import DefaultAccountAdapter
+
+
+class WgerAccountAdapter(DefaultAccountAdapter):
+ """Wger Account Adapter for allauth"""
+
+ def send_mail(self, template_prefix, email, context):
+ """
+ Add token expiration info to the email context.
+
+ Note that we add this to the context of all emails, even if this
+ doesn't make sense for some of them. However, this is acceptable since it's
+ only the template context and we currently only use the verification email,
+ which needs this information.
+ """
+
+ expire_days = app_settings.EMAIL_CONFIRMATION_EXPIRE_DAYS
+ context['expire_hours'] = expire_days * 24
+ context['expire_date'] = timezone.now() + timedelta(days=expire_days)
+ super().send_mail(template_prefix, email, context)
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.py
index b6d40b3c0..31ab7b7a6 100644
--- a/wger/core/api/serializers.py
+++ b/wger/core/api/serializers.py
@@ -23,11 +23,11 @@ from django.contrib.auth.password_validation import validate_password
from django.http import HttpRequest
# Third Party
+from allauth.account.models import EmailAddress
from lingua import LanguageDetectorBuilder
from rest_framework import serializers
from rest_framework.fields import empty
from rest_framework.validators import UniqueValidator
-from allauth.account.models import EmailAddress
# wger
from wger.core.models import (
@@ -87,16 +87,13 @@ class UserprofileSerializer(serializers.ModelSerializer):
'ro_access',
'num_days_weight_reminder',
)
-
+
def get_email_verified(self, obj):
"""
Check Allauth's EmailAddress table for the verification status
"""
try:
- email_obj = EmailAddress.objects.get_for_user(
- user=obj.user,
- email=obj.user.email
- )
+ email_obj = EmailAddress.objects.get_for_user(user=obj.user, email=obj.user.email)
return email_obj.verified
except EmailAddress.DoesNotExist:
return False
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index a7223cacb..34642cc38 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -137,7 +137,12 @@ class UserProfileViewSet(viewsets.ModelViewSet):
request.user.save()
request.user.userprofile.save()
# adds new email if it is not already registered
- EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True)
+ EmailAddress.objects.add_email(
+ request,
+ request.user,
+ request.user.email,
+ confirm=True,
+ )
logger.debug('adding new email with verified flag , send verification email')
return Response(serializer.data)
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index f93fae573..674b987eb 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -402,8 +402,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']
email_obj = EmailAddress.objects.get_for_user(user=self.user, email=self.user.email)
-
- return days_since_joined.days > minimum_account_age and email_obj.verified
+
+ return days_since_joined.days > minimum_account_age and email_obj.verified
@property
def weight(self):
diff --git a/wger/core/views/user.py b/wger/core/views/user.py
index d124af9bd..d64209b42 100644
--- a/wger/core/views/user.py
+++ b/wger/core/views/user.py
@@ -66,6 +66,7 @@ from django.views.generic import (
)
# Third Party
+from allauth.account.models import EmailAddress
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
ButtonHolder,
@@ -74,7 +75,6 @@ from crispy_forms.layout import (
Row,
Submit,
)
-from allauth.account.models import EmailAddress
from rest_framework.authtoken.models import Token
# wger
@@ -330,7 +330,12 @@ def preferences(request):
# If the user changes the email, it is no longer verified
if user_email != email_form.instance.email:
logger.debug('adding email with verified flag and also, sends email confirmation')
- EmailAddress.objects.add_email(request, request.user, request.user.email, confirm=True)
+ EmailAddress.objects.add_email(
+ request,
+ request.user,
+ request.user.email,
+ confirm=True,
+ )
request.user.userprofile.save()
# Save as normal
diff --git a/wger/urls.py b/wger/urls.py
index bd955edf6..4d2bcb530 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -356,8 +356,8 @@ urlpatterns += [
'api/v2/schema/redoc',
SpectacularRedocView.as_view(url_name='schema'),
name='api-redoc',
- ),
- path('email/', include('allauth.account.urls')),
+ ),
+ path('email/', include('allauth.account.urls')),
]
#
diff --git a/wger/utils/helpers.py b/wger/utils/helpers.py
index 1567cd864..837ca6787 100644
--- a/wger/utils/helpers.py
+++ b/wger/utils/helpers.py
@@ -36,7 +36,7 @@ from django.shortcuts import get_object_or_404
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
-# TODO: add log statements here ??
+
logger = logging.getLogger(__name__)